OpenCV | 霍夫变换检测直线

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

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

原始图像

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

识别后图像: 

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值