I am working with an image like this:
What I need is a list with all the contours that touch the top AND the bottom of the image. Currently, what I am doing is i find all the contours of the image and then I iterate through them in order to discard the ones that dont satisfy the condition. My code is as follows:
contours, hier= cv2.findContours(projection,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)
for contour in contours:
if 0 in contour[:,0,1] and rows in contour[:,0,1]:
validContours.append(contour)
I need this to run as fast as possible, so I would just like to just do the checking for the for the first row of pixels.
I think the long but sure way of doing this is implementing another algorithm from the ground up.
Another idea that I have is using the floodfill function algorithm in order to color a blank image another image with the pixel data and then only transfer this data to a "finished" image such that I only have the colored data.
If anyone has any suggestion I would gladly accept them!
You can try use Flood Fill algorithm.
import cv2
im = cv2.imread('VlKak.png', cv2.IMREAD_GRAYSCALE)
h, w = im.shape
mask = im.copy()
mask[0, :] = 255
mask[-1, :] = 255
flood_fill = cv2.floodFill(mask, None, (0, 0), 0)[1]
flood_fill = cv2.floodFill(flood_fill, None, (h, 0), 0)[1]
result = cv2.bitwise_xor(im, flood_fill)