How to Set the Background Color of an Image to a same Color in OpenCV in Python

Viewed 240

I have a dataset of fingernails with different background colors.

First I load those images from my google drive and resize the images to 224*224 as the first preprocessing step.

In the google drive under Dataset folder I have many folders according to the disease of fingernails. As an exmaple: Clubbing

Now I want to add a same background color for all these images.

Here is the code.

import numpy as np
import pandas as pd
import cv2

#Access the Google drive from colab, we have to mount the drive to colab
from google.colab import drive
drive.mount('/content/gdrive')
root_path = 'gdrive/My Drive/Dataset/'

#Access dataet
dataset_dir = pathlib.Path(root_path)
dataset_dir

#Display an image in the Clubbing folder
clubbing = list(dataset_dir.glob('clubbing/*.png'))
clubbing[:4]

#Prepare the dataset contains each nail type as key and list of paths to those images as value
nail_disease_images_dict = {
    'clubbing': list(dataset_dir.glob('clubbing/*')),
    
}
#Create new python dictionary ('flowers_labels_dict') and input numeric value to each label
flowers_labels_dict = {
    'clubbing': 0,
}

#All the images resized to 224 X 224.   
image_list, classes = [], []
IMG_WIDTH=224
IMG_HEIGHT=224

for disease_name, images in nail_disease_images_dict.items():
    for image in images:
        img = cv2.imread(str(image))
        resized_img = cv2.resize(img,(IMG_WIDTH,IMG_HEIGHT))
        image_list.append(resized_img)
        classes.append(flowers_labels_dict[disease_name])

How Can I add the Same background color for all these images in the dataset.

For Example:

enter image description here

I want to change to this: (Please donot consider about the size. It should be in the same size as above image)

enter image description here

0 Answers
Related