I am working with images that I need to process. However, only specific areas of these images are of interest so for each image I have a corresponding mask (that can be of any shape, not a bounding box or anything specific). I would like to do Histogram Equalization but only on the "masked surface" as I am not interested in the rest of the image.
A similar question has been asked
The following is the mask:
mask = cv2.imread('mask.jpg', 0)
I want to perform histogram equalization only on the flower.
Store the coordinates where pixels are white (255) in mask
:
coord = np.where(mask == 255)
Store all pixel intensities on these coordinates present in gray
:
pixels = gray[coord]
Perform histogram equalization on these pixel intensities:
equalized_pixels = cv2.equalizeHist(pixels)
Create a copy of gray
named gray2
. Place the equalized intensities in the same coordinates:
gray2 = gray.copy()
for i, C in enumerate(zip(coord[0], coord[1])):
gray2[C[0], C[1]] = equalized_pixels[i][0]
cv2.imshow('Selective equalization', gray2)
Comparison:
Note: This process can be extended for Histogram Equalization or CLAHE, ON RGB or grayscale images.