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

识别后图像:

相关推荐
小程故事多_804 小时前
Agent+Milvus,告别静态知识库,打造具备动态记忆的智能AI助手
人工智能·深度学习·ai编程·milvus
code_pgf4 小时前
Llama 3详解
人工智能·llama
ComputerInBook4 小时前
数字图像处理(4版)——第 3 章——(图像的)强度变换和空间滤波(Rafael C.Gonzalez&Richard E. Woods)
图像处理·人工智能·计算机视觉·强度变换和空间滤波
爱写代码的小朋友4 小时前
生成式人工智能(AIGC)在开放式教育问答系统中的知识表征与推理机制研究
人工智能·aigc
技术专家4 小时前
Stable Diffusion系列的详细讨论 / Detailed Discussion of the Stable Diffusion Series
人工智能·python·算法·推荐算法·1024程序员节
m0_488913014 小时前
万字长文带你梳理Llama开源家族:从Llama-1到Llama-3,看这一篇就够了!
人工智能·学习·机器学习·大模型·产品经理·llama·uml
helpme流水4 小时前
LLaMA Factory 从入门到精通,一篇讲完
人工智能·ai·语言模型·llama
段一凡-华北理工大学4 小时前
【大模型+知识图谱+工业智能体技术架构】~系列文章01:快速了解与初学入门!!!
人工智能·python·架构·知识图谱·工业智能体
Swift社区4 小时前
AI Governance:从 Policy Engine 到完整治理体系
人工智能·openclaw
田井中律.5 小时前
知识图谱(BILSTM+CRF项目完整实现)【第六章】
人工智能·知识图谱