Home > Blockchain >  Changing background color for part of image in Open CV
Changing background color for part of image in Open CV

Time:12-09

I am able to extract data from image where it is having white background, data with black background I am not able to extract. How to get this data.

enter image description here

Below is the code I tried but not working..

#denoise
#For normal image
#img = cv2.fastNlMeansDenoisingColored(img, None, 3, 3, 7, 21)

#gray
#img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#Morp operation
#kernel = np.ones((1, 1), np.uint8)
#img = cv2.dilate(img, kernel, iterations=1)
#img = cv2.erode(img, kernel, iterations=1)

CodePudding user response:

An approach that inverts the color of big dark areas using cv2.findContours and cv2.boudingRect

#denoise
#For normal image
img = cv2.fastNlMeansDenoisingColored(img, None, 3, 3, 7, 21)

#gray
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#Morp operation
kernel = np.ones((1, 1), np.uint8)
img = cv2.dilate(img, kernel, iterations=1)
img = cv2.erode(img, kernel, iterations=1)

#Transforms gray to white
img[img > 215] = 255

binary = cv2.bitwise_not(img)

contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

for contour in contours:
    (x,y,w,h) = cv2.boundingRect(contour)
    # Ignore small areas
    if w*h < 500:
        continue

    # Invert colors
    img[y:y h, x:x w] = ~img[y:y h, x:x w]

# Transforms gray to white
img[img > 215] = 255

result:

Filtered image

  • Related