简体   繁体   中英

Histogram Equalization on specific area using mask

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 here , however the solutions proposed rely on bounding boxes or equalizing the whole image which is not my goal.

Any solution using Histogram Equalization or CLAHE, RGB or grey scale images would be interesting

This is possible. Here is a simple approach:

Flow:

You can perform histogram equalization for a given region with the help of the mask.

  • Using the mask, store coordinates where pixels are in white.
  • Store pixel intensities from these coordinates present in the grayscale image
  • Perform histogram equalization on these stored pixels. You will now get a new set of values.
  • Replace the old pixel values with the new ones based on the coordinate positions.

Code:

The following is an illustration using a grayscale image.

img = cv2.imread('flower.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

在此处输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM