I am developing a color masking algorithm to extract overlapped regions of interest in images, using Python 3.9 and cv2.
To achieve this, I'm implementing K-Means clustering as a way to group up similar pixels and reduce the image's color palette to only a few hues. Then, I use np.unique to find the unique RGB values present, and I finally filter the original image with one of the values provided.
From the following image, I am able to sucessfully extract the region I want:
Extracted Region:
As observed, the result still contains some noise from the underlying region (flame) around the area of interest. I have attempted to clean it up using cv2.GaussianBlur and cv2.ConnectedComponentsWithStats, but I can't seem to remove it properly. Below is my desired output (manually removed noise):
How can I achieve the same result (or similar) programatically?
Note: The RGB filter to extract this region can be defined as follows:
rgb = np.array([198, 154, 107])
# Making a range with 25% offsets
low = rgb * 0.75
high = rgb * 1.25
# Defining the mask
mask = cv2.inRange(image, low, high)
# Applying mask
result = cv2.bitwise_and(image, image, mask=mask)


