Ale*_*Acc 3 python opencv vanishing-point straight-line-detection
我正在 Python 中处理基于 OpenCV 的项目,我必须计算/提取并直观地显示现有线条的消失点。
我的第一个任务是检测线条,使用 Canny 和 HoughLinesP 函数非常容易:
import cv2
import numpy as np
img = cv2.imread('.image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
edges = cv2.Canny(gray, 500, 460)
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 30, maxLineGap=250)
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 128), 1)
cv2.imwrite('linesDetected.jpg', img)
Run Code Online (Sandbox Code Playgroud)
但我想计算/推断所有线条的消失点,以找到(并绘制)它们相互交叉的位置,如下图所示。
我知道我需要添加一个更大的框架来绘制线条的延续,以找到交叉点(消失点),但此时我非常迷茫。
太感谢了!!
cv2.HoughTransformP如果您使用传统的Hough 变换实现,而不是概率 Hough 变换实现,则cv2.HoughTransform这些线将在参数空间 (?,?) 中表示。参数空间与实际点坐标相关,如 ?=xcos?+ysin? 在哪里 ?是原点到直线的垂直距离,以及 ? 是该垂直线与水平轴以逆时针方向测量的角度。
lines = cv2.HoughLines(edges, 1, np.pi/180, 200)
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 + 10000*(-b))
y1 = int(y0 + 10000*(a))
x2 = int(x0 - 10000*(-b))
y2 = int(y0 - 10000*(a))
cv2.line(img,(x1,y1),(x2,y2),(0,255,0),1)
Run Code Online (Sandbox Code Playgroud)
正如您在下面看到的,消失线的投影已经开始出现。
现在,如果我们使用这个特定图像的参数并跳过已经平行的垂直线,我们可以获得一组更好的消失线。
# fine tune parameters
lines = cv2.HoughLines(edges, 0.7, np.pi/120, 120, min_theta=np.pi/36, max_theta=np.pi-np.pi/36)
for line in lines:
rho,theta = line[0]
# skip near-vertical lines
if abs(theta-np.pi/90) < np.pi/9:
continue
a = np.cos(theta)
b = np.sin(theta)
x0 = a*rho
y0 = b*rho
x1 = int(x0 + 10000*(-b))
y1 = int(y0 + 10000*(a))
x2 = int(x0 - 10000*(-b))
y2 = int(y0 - 10000*(a))
cv2.line(img,(x1,y1),(x2,y2),(0,255,0),1)
Run Code Online (Sandbox Code Playgroud)
在这一步,有多种选择可以找到线的交点,消失点。我将在下面列出其中的一些。
下面,您可以看到之前的线条图像,以及运行以下代码后产生的左右斑点图像。
# delete lines
kernel = np.ones((3,3),np.uint8)
img2 = cv2.erode(img2,kernel,iterations = 1)
# strengthen intersections
kernel = np.ones((9,9),np.uint8)
img2 = cv2.dilate(img2,kernel,iterations = 1)
# close remaining blobs
kernel = np.ones((11,11),np.uint8)
img2 = cv2.erode(img2,kernel,iterations = 1)
img2 = cv2.dilate(img2,kernel,iterations = 1)
cv2.imwrite('points.jpg', img2)
Run Code Online (Sandbox Code Playgroud)
小智 -1
如果你想从图像中找到消失点,你需要画线。为此,您可以使用霍夫变换。它的作用是在图像上绘制所有可能的线条。您可以根据需要调整它的参数。它将为您提供大多数线相交的交点。虽然这是一种不完全正确的估计,但你可以说它是完美的估计。您还可以根据需要使用其他形式的霍夫。
在这种情况下,标准霍夫变换就足够了。
| 归档时间: |
|
| 查看次数: |
4462 次 |
| 最近记录: |