如何检测图像中小于或大于90度的边(线)?

问题描述 投票:4回答:1

在我的项目中,我需要检测一个房屋平面图的屋顶。为此,我必须使用蓝图的前立面。我计划使用open-cv HoughLines方法。使用该方法,我需要检测房子的屋顶。下面我附上了房子的图像和我的代码。

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('lol.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)

lines = cv2.HoughLines(edges,1,np.pi/180,100)
count =0
for rho,theta in lines[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))

    if( 20 < 180*theta/np.pi < 88):
        cv2.line(img, (x1, y1), (x2, y2), (255, 0, 255), 2)

    if (160 > 180 * theta / np.pi > 93):
        cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 2)

cv2.imwrite('detectP.jpg',img)
plt.imshow(img)

我使用的房屋平面图。

enter image description here

执行代码后得到的结果。

enter image description here

如你所见,检测的是整个图像。我需要它只检测线,并在角度线的上面画线,而不是整个图像。

python opencv hough-transform
1个回答
1
投票

你可以使用HoughLinesP()函数,它是概率Hough线变换.访问 https:/docs.opencv.org3.4d9db0tutorial_hough_lines.html。

© www.soinside.com 2019 - 2024. All rights reserved.