Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用cv2在python中连接手绘图像上的角的线_Python_Opencv_Image Processing_Corner Detection - Fatal编程技术网

使用cv2在python中连接手绘图像上的角的线

使用cv2在python中连接手绘图像上的角的线,python,opencv,image-processing,corner-detection,Python,Opencv,Image Processing,Corner Detection,我想检测手绘图像上连接角的线条。我使用Harris角点检测来查找图像的角点。接下来,我将用线连接所有的角点,并对这些点进行迭代,以查看它们是否与原始图像中的像素相匹配,并为每行像素覆盖设置一个阈值,以说明可以接受的是,这是一条连接角点的正确线。。它是有效的。。。但是它非常慢。有没有更好的方法来做这件事,或者我应该使用不同的方法?(Hough线不起作用,因为可能有曲线,我只想要连接角的线 for i in c_corners: #corners thru harris and coorected

我想检测手绘图像上连接角的线条。我使用Harris角点检测来查找图像的角点。接下来,我将用线连接所有的角点,并对这些点进行迭代,以查看它们是否与原始图像中的像素相匹配,并为每行像素覆盖设置一个阈值,以说明可以接受的是,这是一条连接角点的正确线。。它是有效的。。。但是它非常慢。有没有更好的方法来做这件事,或者我应该使用不同的方法?(Hough线不起作用,因为可能有曲线,我只想要连接角的线

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
来帮助创建所有可能的点对以定义线段

import cv2
import numpy as np
import itertools

img = cv2.imread('drawing.png')
bin_inv = cv2.bitwise_not(img) # flip image colors
bin_inv = cv2.cvtColor(bin_inv, cv2.COLOR_BGR2GRAY) # make one channel
bin_inv = cv2.dilate(bin_inv, np.ones((5,5)))

corners = ((517, 170),
    (438, 316),
    (574, 315),
    (444, 436),
    (586, 436))

lines = itertools.combinations(corners,2) # create all possible lines
line_img = np.ones_like(img)*255 # white image to draw line markings on
for line in lines: # loop through each line
    bin_line = np.zeros_like(bin_inv) # create a matrix to draw the line in
    start, end = line # grab endpoints
    cv2.line(bin_line, start, end, color=255, thickness=5) # draw line
    conj = (bin_inv/255 + bin_line/255) # create agreement image
    n_agree = np.sum(conj==2)
    n_wrong = np.sum(conj==1)
    if n_agree/n_wrong > .05: # high agreements vs disagreements
        cv2.line(line_img, start, end, color=[0,200,0], thickness=5) # draw onto original img

# combine the identified lines with the image
marked_img = cv2.addWeighted(img, .5, line_img, .5, 1)
cv2.imwrite('marked.png', marked_img)

我尝试了很多不同的设置(玩厚度、膨胀、不同比率等)但是,它非常适合原始的黑色像素,所以我不确定如果你使用这种方法,你将如何摆脱它。它有右上角的曲线,以及它穿过的中间线,以及右下角的曲线,它朝着这个方向n。不管怎样,这只需要两秒钟就可以运行,因此至少比您当前的代码快。

您是否试图简单地计算横过图像的线条数?如果您已经有了这些点,您应该将这些线条放在另一个二进制图像上,然后简单地使用逻辑比较来计算共有多少个点.65是从哪里来的?是的,这正是我正在做的。65是我用来比较的一个阈值。谢谢。我会尝试二进制比较,听起来应该会快得多。你真的需要这些合成线吗?还是它们只是定位手绘线的一种方法?我需要角点连接用一条直线绘制。我实际使用的图形比示例图形要复杂一些,而且通常绘制得不太好。您的链接输出图像显示了绘制的每一条可能的线,而不仅仅是匹配的线。非常感谢。这正是我需要的帮助。我将尝试不同的方法对此进行扩展以更好地适应找到的线路,但这将使它更容易,因为它是如此快。