'Change the Background color of an image set using OpenCV
I have a fingernail dataset and in these images they have different background colors as below image.
I need to covert all those image's background color to one background color and check the accuracy of CNN model that I built.
Here is the code I tried and here I change white background to black background. How can I change the all background colors to a one background color at once.
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
# threshold using inRange
range1 = (0, 0, 231)
range2 = (180, 18, 255)
mask = cv2.inRange(hsv,range1,range2)
mask = 255 - mask
# apply morphology opening to mask
kernel = np.ones((3,3), np.uint8)
mask = cv2.morphologyEx(mask, cv2.MORPH_ERODE, kernel)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
# antialias mask
mask = cv2.GaussianBlur(mask, (0,0), sigmaX=3, sigmaY=3, borderType = cv2.BORDER_DEFAULT)
mask = skimage.exposure.rescale_intensity(mask, in_range=(127.5,255), out_range=(0,255))
result = img.copy()
result[mask == 0] = (0,0,0)
Input to the code:
It change the background color to black as the output:
Is there any way to do this change to all images in the dataset at once?
Solution 1:[1]
Check the pixellib libary, it should be able to that for you.. you just need to find a way to parse through all the images using a for loop perhaps.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | onyeka okonji |