gpt4 book ai didi

python - HoughLine 修改后的代码会检测到简单的图像,但不会检测到复杂的图像

转载 作者:太空宇宙 更新时间:2023-11-03 11:23:47 25 4
gpt4 key购买 nike

我正在做一个需要检测屋顶的项目。目前,我正在检测斜线。经过一番尝试,我想出了一个检测屋顶的解决方案。但是当我用各种类型的屋顶平面(复杂的)对其进行测试时,检测并不准确。

这是我使用的代码,

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

img = cv2.imread('frontElevation.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,80,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))

plt.scatter(x1, y1)
plt.scatter(x2, y2)

if( 20 < 180*theta/np.pi < 88):
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255),3)
plt.scatter(x1, y1)
plt.scatter(x2, y2)
if (160 > 180 * theta / np.pi > 93):
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255),3)
plt.scatter(x1, y1)
plt.scatter(x2, y2)

cv2.imwrite('detectedFront.jpg',img)

这是我使用的简单屋顶计划以及为此获得的结果,

enter image description here enter image description here

但是当我使用一个复杂的(真实的)计划时,我得到的输出是什么,

enter image description here enter image description here

我尝试了另一个代码,它给了我一个令人愉快的输出。下面我附上了代码和输出。

import cv2.cv as cv
import numpy as np
import math

im=cv.LoadImage('h1.jpg', cv.CV_LOAD_IMAGE_GRAYSCALE)

pi = math.pi #Pi value

dst = cv.CreateImage(cv.GetSize(im), 8, 1)

cv.Canny(im, dst, 200, 200)
cv.Threshold(dst, dst, 100, 255, cv.CV_THRESH_BINARY)

#---- Probabilistic ----
color_dst_proba = cv.CreateImage(cv.GetSize(im), 8, 3)
cv.CvtColor(im, color_dst_proba, cv.CV_GRAY2BGR) # idem


rho=1
theta=pi/180
thresh = 100
minLength= 120 # Values can be changed approximately to fit your image edges
maxGap= 50

lines = cv.HoughLines2(dst, cv.CreateMemStorage(0), cv.CV_HOUGH_PROBABILISTIC, rho, theta, thresh, minLength, maxGap)
for line in lines:
cv.Line(color_dst_proba, line[0], line[1], cv.CV_RGB(255, 0, 0), 3, 8)

cv.ShowImage("Hough Probabilistic", color_dst_proba)
cv.WaitKey(0)
cv.SaveImage("output.jpg",color_dst_proba)

结果,

enter image description here

这也有水平线和垂直线。为了我的目的,我只需要斜线。谁能帮我解决这个问题?提前致谢!

最佳答案

在您的第一个版本中,您有这部分代码:

for rho,theta in lines[0]:
if( 20 < 180*theta/np.pi < 88):
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255),3)
plt.scatter(x1, y1)
plt.scatter(x2, y2)
if (160 > 180 * theta / np.pi > 93):
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255),3)
plt.scatter(x1, y1)
plt.scatter(x2, y2)

在你的第二个版本中你只有

for line in lines:
cv.Line(color_dst_proba, line[0], line[1], cv.CV_RGB(255, 0, 0), 3,8)

尝试以与第一个代码示例相同的方式绘制线条。由于您使用的是概率 HoughLine 函数,因此您需要自己计算角度。

如何做到这一点的答案在 How to calculate the angle between a line and the horizontal axis? 中给出。

我对 python 不是很熟悉,但它应该是这样的:

for line in lines:
dy = line[1].y - line[0].y
dx = line[1].x - line[0].x
angle = atan2(dy,dx) * 180/np.pi
if(20 < angle < 88)
cv.Line(color_dst_proba, line[0], line[1], cv.CV_RGB(255, 0, 0),3,8)
if(160 > angle > 93)
cv.Line(color_dst_proba, line[0], line[1], cv.CV_RGB(255, 0, 0),3,8)

如果这不起作用,请尝试 https://stackoverflow.com/a/32963819/2393191计算角度。

关于python - HoughLine 修改后的代码会检测到简单的图像,但不会检测到复杂的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37827224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com