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')

识别后图像:

相关推荐
大龄程序员狗哥3 小时前
第25篇:Q-Learning算法解析——强化学习中的经典“价值”学习(原理解析)
人工智能·学习·算法
陶陶然Yay3 小时前
神经网络常见层Numpy封装参考(5):其他层
人工智能·神经网络·numpy
极客老王说Agent3 小时前
2026实战指南:如何用智能体实现药品不良反应报告的自动录入?
人工智能·ai·chatgpt
imbackneverdie3 小时前
本科毕业论文怎么写?需要用到什么工具?
人工智能·考研·aigc·ai写作·学术·毕业论文·ai工具
lulu12165440783 小时前
Claude Code项目大了响应慢怎么办?Subagents、Agent Teams、Git Worktree、工作流编排四种方案深度解析
java·人工智能·python·ai编程
大橙子打游戏3 小时前
talkcozy像聊微信一样多项目同时开发
人工智能·vibecoding
deephub3 小时前
LangChain 还是 LangGraph?一个是编排一个是工具包
人工智能·langchain·大语言模型·langgraph
OidEncoder4 小时前
编码器分辨率与机械精度的关系
人工智能·算法·机器人·自动化
Championship.23.244 小时前
Harness工程深度解析:从理论到实践的完整指南
人工智能·harness
扬帆破浪5 小时前
开源免费的WPS AI 软件 察元AI文档助手:链路 002:executeAssistantFromRibbon 与任务进度窗
人工智能·开源·wps