我正在寻找在手绘图像like this上连接角的线条。我用哈里斯角点检测来找到图像的角点。接下来,我将用线条连接所有的角点,并遍历这些点,看看它们是否与原始图像中的像素相匹配,并为每一行像素覆盖设置一个阈值,以说明什么是连接角点的正确线。Image of connected lines。它起作用了。。。但速度很慢。有更好的方法吗?或者我应该用不同的方法?(霍夫线不会工作,因为可能会有曲线,我只想要连接角落的线条。在
for i in c_corners: #corners thru harris and coorected with subpix
x1,y1 = i.ravel()
for k in c_corners:
x2,y2 = k.ravel()
if x1 != x2 and y1 != y2: #ignore vertical lines
linePoints = line_points(x1,y1, x2,y2) # function to get line pnts
totalLinePoints = len(linePoints)
coverPoints = 0
########## This is where I think the slow down is happening and could be optimized
for m in originalImage: #image is dialated to help detection
for n in linePoints:
match = np.all(m == n)
if match == True:
coverPoints += 1
print("Line Cover = ", (coverPoints/totalLinePoints))
if (coverPoints/totalLinePoints) > .65:
good_lines.append([x1,y1,x2,y2])
感谢您的帮助,谢谢!在
我最初的方法是创建一个空白图像并在其上绘制每一行,然后使用
cv2.bitwise_and()
与二进制(放大)图像一起计算有多少像素是一致的,如果它们满足阈值,则在原始图像上绘制这些线。然而,设置像素数的阈值会使小行受到惩罚。一个更好的指标应该是正确匹配与错误匹配的数量之比(我现在意识到这正是您实际在做的)。此外,这是一个更稳健的扩张和线的厚度,你选择绘制你的线。在但是,您所使用的一般方法对于绘图中的问题不是很可靠,因为许多绘制的曲线可能会碰到一条线段,因此,像这条这样的合成线可能很容易拟合不属于它们的直线。您可以在我的代码输出中看到此问题:
我只是硬编码了一些角落的估计,然后从那里开始。注意使用
itertools
来帮助创建所有可能的点对来定义线段。在我尝试了很多不同的设置(玩厚度,膨胀,不同的比例,等等),但没有得到虚假的较长的线出现。它非常适合原始的黑色像素,所以我不确定如果你使用这种方法,你将如何摆脱它。它有一条右上角的曲线,还有它穿过的中线,和右下角的曲线,这条曲线有点倾向于这个方向。不管怎样,这只需要两秒钟就可以运行,所以至少比您当前的代码快。在
相关问题 更多 >
编程相关推荐