Opencv : How to remove rectangle black box without removing any other characters

asked 2018-06-23 16:56:27 -0600

Ahmed Osama gravatar image

I would like to remove a rectangle black box from the below image.

enter image description here

I do some preprocessing operation to keep the upper top of the image only. My problem with the rectangle in the middle of the image

This is the preprocessing operation I do on this image

gray = cv2.cvtColor(cropped_top, cv2.COLOR_BGR2GRAY)




binary = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 15, 2)


binary = cv2.fastNlMeansDenoising(binary, None, 65, 5, 21)


ret, thresh1 = cv2.threshold(binary, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

k = np.ones((4,4))
binary = cv2.morphologyEx(thresh1, cv2.MORPH_CLOSE, k)

This is the output till now

enter image description here

Here it appears 3 lines connected together. I have used cv2.findContours. But till now I failed to remove this rectangle. I know I am doing something wrong regarding contours.

Here is the code I used for detecting contours

_,binary = cv2.threshold(image, 150, 255, cv2.THRESH_BINARY)


# find external contours of all shapes
_,contours,_ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

# create a mask for floodfill function, see documentation
h,w= image.shape
mask = np.zeros((h+2,w+2), np.uint8)

# determine which contour belongs to a square or rectangle
for cnt in contours:
    poly = cv2.approxPolyDP(cnt, 0.05*cv2.arcLength(cnt,True),True)
    if len(poly) == 4:
        # if the contour has 4 vertices then floodfill that contour with black color
        cnt = np.vstack(cnt).squeeze()
        _,binary,_,_ = cv2.floodFill(binary, mask, tuple(cnt[0]), 0)

how I can successfully remove this black rectangle without distorting letter Q

edit retag flag offensive close merge delete