检查已连接组件的相邻像素

时间:2018-08-01 11:52:59

标签: python opencv computer-vision opencv3.0 connected-components

我正在使用opencv从this之类的图像中提取文本。现在,我的目标是尝试删除大部分可以保留文字的墙壁/窗户/门。这是我想出的:

def process_image(path, out_path):
    image = Image.open(path)

    gray_image = cv2.cvtColor(np.asarray(image), cv2.COLOR_BGR2GRAY)
    ret,th = cv2.threshold(gray_image,127,255,cv2.THRESH_OTSU)

    kernel = np.ones((4,4),np.uint8)
    edges = cv2.dilate(th,kernel,iterations = 1)
    edges = cv2.erode(edges, kernel, iterations = 1)


    edges = cv2.bitwise_and(cv2.bitwise_not(th), edges)
    ret, labels = cv2.connectedComponents(edges, connectivity = 8)

    for label in range(ret):
        if  : #if connComp is adjacent to a wall
            edges[labels == label] = 0


cv2.imwrite(out_path,edges)

我基本上将图像放大,以使剩下的都是壁垒最细的壁,然后使用该图像作为过滤器来实际删除那些壁。最后,我寻找连接的组件,现在我想循环使用这些组件,并检查它们是否“接触”了我刚刚使用的过滤器(意味着白色像素紧接白色像素),如果愿意,将组件重新着色为黑色。这样,我应该能够对图像进行更多的过滤,使文本保持不变,因为它永远不会碰到墙。问题是这是我第一次使用opencv,我不确定如何实现我的想法的最后一部分。有关如何操作的任何提示?

0 个答案:

没有答案
相关问题