Check the neighbour pixels of a connected component

asked 2018-07-30 17:15:02 -0600

Betelgeuse gravatar image

updated 2018-07-31 02:24:38 -0600

Hello, after filtering out parts of a binary image with some bitwise operations i'd like to check wheter the remaining connected components "touch" or not the mask I used to filter the image and if they do touch it i'd like to paint them black. How can I do it? Code should look something like this i think but i can't really specify the if condition:

ret, labels = cv2.connectedComponents(image, connectivity = 8)
for label in range(ret):
    if  : #if connComp adjacent to black pixel in the mask
        edges[labels == label] = 0

Edit: I'm trying to extract text from images like this and I'd like to clean them out of walls and everything touching the walls first. This is the code i use

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)
    Image.fromarray(edges).show()

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

    for label in range(ret):
        if  : #if connComp is adjacent to black pixel in the mask
            edges[labels == label] = 0


    cv2.imwrite(out_path,edges)
edit retag flag offensive close merge delete

Comments

1

an image is welcome to understand your problem

LBerger gravatar imageLBerger ( 2018-07-31 00:27:07 -0600 )edit
1

i edited the question LBerger

Betelgeuse gravatar imageBetelgeuse ( 2018-07-31 02:23:38 -0600 )edit