OpenCV | 霍夫变换检测直线

上次写的不好,这次重写一个霍夫变化的框架,里面包括灰度转换、滤波

canny边缘检测、霍夫变换等......

原始图像

python 复制代码
import cv2
import numpy as np
import matplotlib.pyplot as plt#Matplotlib是RGB
%matplotlib inline 

def detect_parking_lines(image_path):
    # 读取图像
    img = cv2.imread(image_path)

    # 灰度转换
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # 高斯滤波
    gray = cv2.GaussianBlur(gray, (5, 5), 0)

    # Canny 边缘检测
    edges = cv2.Canny(gray, 50, 150)

    # Hough 变换检测直线
    lines = cv2.HoughLines(edges, 1, np.pi / 180, 130)

    # 绘制检测到的直线
    if lines is not None:
        for line in lines:
            rho, theta = line[0]
            a = np.cos(theta)
            b = np.sin(theta)
            x0 = a * rho
            y0 = b * rho
            x1 = int(x0 + 1000 * (-b))
            y1 = int(y0 + 1000 * (a))
            x2 = int(x0 - 1000 * (-b))
            y2 = int(y0 - 1000 * (a))
            cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 2)

    # 显示结果
    cv2.imshow('Detected Parking Lines', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

# 调用函数进行车位线检测
detect_parking_lines('chessboard.jpg')

识别后图像:

相关推荐
耿雨飞5 小时前
第三章:LangChain Classic vs. 新版 LangChain —— 架构演进与迁移指南
人工智能·架构·langchain
BizViewStudio5 小时前
甄选 2026:AI 重构新媒体代运营行业的三大核心变革与落地路径
大数据·人工智能·新媒体运营·媒体
俊哥V5 小时前
AI一周事件 · 2026年4月8日至4月14日
人工智能·ai
GitCode官方6 小时前
G-Star Gathering Day 杭州站回顾
人工智能·开源·atomgit
宇擎智脑科技6 小时前
开源 AI Agent 架构设计对比:Python 单体 vs TypeScript 插件化
人工智能·openclaw·hermes agent
冷色系里的一抹暖调7 小时前
OpenClaw Docker部署避坑指南:服务启动成功但网页打不开?
人工智能·docker·容器·openclaw
曹牧7 小时前
自动编程AI落地方案‌
人工智能
天云数据7 小时前
Harness革命:企业级AI从“失控野马”到“价值引擎”的跃迁
人工智能
汽车仪器仪表相关领域7 小时前
NHVOC-70系列固定污染源挥发性有机物监测系统:精准破局工业VOCs监测痛点,赋能环保合规升级
大数据·人工智能·安全性测试
克里斯蒂亚诺·罗纳尔达7 小时前
智能体学习23——资源感知优化(Resource-Aware Optimization)
人工智能·学习