How can I remove a black color rectangle from an image like this below,
I want to detect that huge black color rectangle box and change it to white color? Its my next pre-processing stage.
Any help is appreciated
CodePudding user response:
Here is one way to do that in Python/OpenCV.
- Read the input
- Convert to gray
- Threshold to binary and invert
- Get the largest external contour
- Get the bounding box for that contour
- Fill that region of the input with white
- Save the result
Input:
import cv2
import numpy as np
# read image
img = cv2.imread('black_rectangle.png')
ht, wd = img.shape[:2]
print(img.shape)
# convert to grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# threshold
thresh = cv2.threshold(gray,128,255,cv2.THRESH_BINARY)[1]
# invert
thresh = 255 - thresh
# get largest external contour
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
big_contour = max(contours, key=cv2.contourArea)
# get bounding box of largest contour
x,y,w,h = cv2.boundingRect(big_contour)
# make that region white in the input image
result = img.copy()
result[y:y h, x:x w] = (255,255,255)
# show thresh and result
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
# save resulting image
cv2.imwrite('black_rectangle_2white.png',result)
Result:
CodePudding user response:
Changing a rectangle to a certain color is the easy part, detection is harder.
You could try for each black pixel to group it with neighboring pixels of the same color. Once you have these groups, you need to find out if it's a rectangle or something else, e.g. a letter or a line.
A rectangle would have a minimum size, a convex outline and its area should be fully black.
For each group that fulfills these conditions, replace its pixels with white ones.



