0

I have a fingernail dataset and in these images they have different background colors as below image.

enter image description here

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:

enter image description here

It change the background color to black as the output:

enter image description here

Is there any way to do this change to all images in the dataset at once?

  • 2
    You have to loop over each image and do the same thing. – fmw42 Sep 05 '21 at 04:28
  • @fmw42thank you!!. But in my dataset background colors are different in different images. Is there any way to change the range1 and range 2? because ranges are different for different background colors – Pramodi Samaratunga Sep 05 '21 at 04:47
  • 2
    Try thresholding on skin tones rather than background color. – fmw42 Sep 05 '21 at 15:05
  • 2
    you shouldn't need to do any of this. your network should have learned which part is the finger/fingernail, and it should ignore the background on its own. – Christoph Rackwitz Sep 05 '21 at 17:04

1 Answers1

0

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.

  • 1
    A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted](/help/deleted-answers). –  May 10 '22 at 18:30