对图片进行边缘检测+霍夫直线检测

import cv2
import os
import numpy as np
image_path="自己的文件路径/,图像后缀"
depth_image_ = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)
depth_image = np.copy(depth_image_)
edges = cv2.Canny(depth_image, 50, 100, apertureSize=3)
lines = cv2.HoughLines(edges, 1, np.pi / 180, threshold=55)

# 筛选斜率较大的直线,并在原始图像上绘制
if lines is not None:
    for line in lines:
        rho, theta = line[0]
        a = np.cos(theta)
        b = np.sin(theta)

        # 计算斜率,避免除零错误
        if b != 0:
            slope = -a / b
            # 筛选斜率大于某个阈值的直线
            if slope >- 0.1  and slope<0:  # 修改斜率阈值为0.5或其他适当值
                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))
                #在(x1,y1) 到(x2,y2)之间画一条直线
                cv2.line(depth_image_, (x1, y1), (x2, y2), 100000, 2)

# 显示结果
cv2.imshow('Hough Lines',depth_image_)
cv2.waitKey(0)
cv2.destroyAllWindows()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值