Check the neighbour pixels of a connected component
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)
an image is welcome to understand your problem
i edited the question LBerger