I need to do a histogram equalization for a colored image.
First I convert the colored image to gray and give it to the equalizeHist
function:
image = cv2.imread("photo.jpg") image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.equalizeHist(image) cv2.imshow("equalizeHist", image) cv2.waitKey(0)
But after this I need to convert the image back to RGB; how can i do that?
We can do this in OpenCV using a function cv2. equalizeHist(). If its input is just grayscale image, then output is our histogram equalized image. If it is colored (RGB) image, we can segregate all three different streams — red, green, blue; call cv2.
The method is useful in images with backgrounds and foregrounds that are both bright or both dark. OpenCV has a function to do this, cv2. equalizeHist(). Its input is just grayscale image and output is our histogram equalized image.
In this tutorial, we are going to see how to apply Contrast Limited Adaptive Histogram Equalization (CLAHE) to equalize images. CLAHE is a variant of Adaptive histogram equalization (AHE) which takes care of over-amplification of the contrast.
Source : https://www.packtpub.com/packtlib/book/Application-Development/9781785283932/2/ch02lvl1sec26/Enhancing%20the%20contrast%20in%20an%20image
import cv2 import numpy as np img = cv2.imread('input.jpg') img_yuv = cv2.cvtColor(img, cv2.COLOR_BGR2YUV) # equalize the histogram of the Y channel img_yuv[:,:,0] = cv2.equalizeHist(img_yuv[:,:,0]) # convert the YUV image back to RGB format img_output = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR) cv2.imshow('Color input image', img) cv2.imshow('Histogram equalized', img_output) cv2.waitKey(0)
~edit: original link is no longer available, similar idea is implemented here: Histogram Equalization of a Color image with OpenCV
import cv2 def run_histogram_equalization(image_path): rgb_img = cv2.imread(image_path) # convert from RGB color-space to YCrCb ycrcb_img = cv2.cvtColor(rgb_img, cv2.COLOR_BGR2YCrCb) # equalize the histogram of the Y channel ycrcb_img[:, :, 0] = cv2.equalizeHist(ycrcb_img[:, :, 0]) # convert back to RGB color-space from YCrCb equalized_img = cv2.cvtColor(ycrcb_img, cv2.COLOR_YCrCb2BGR) cv2.imshow('equalized_img', equalized_img) cv2.waitKey(0)
Histogram Equalization (HE) is a statistical approach for spreading out intensity values. In image processing, HE is used for improving the contrast of any image, that is- to make the dark portion darker and the bright portion brighter.
For a grey-scale image, each pixel is represented by the intensity value (brightness); that is why we can feed the pixel values directly to the HE function. However, that is not how it works for an RGB-formatted color image. Each channel of the R, G, and B represents the intensity of the related color, not the intensity/brightness of the image as a whole. And so, running HE on these color channels is NOT the proper way.
We should first separate the brightness of the image from the color and then run HE on the brightness. Now, there are already standardized colorspaces that encode brightness and color separately, like- YCbCr, HSV, etc.; so, we can use them here for separating and then re-merging the brightness. The proper way:
Convert the colorspace from RGB to YCbCr >> Run HE on the Y channel (this channel represents brightness) >> Convert back the colorspace to RGB
For HSV colorspace, HE should be run on the V channel. However, the Y channel of YCbCr is the better representer for brightness than the V channel of HSV. So, using the YCbCr format produces a more correct result for HE.
HE is too a naive technique and often produces peculiar colors and small artifacts. This is because it does not care about outliers and the location of a pixel. So, extensions like- Contrast Limited Adaptive HE, Brightness preserving Bi-HE, etc. are used more commonly. Also, different noise reduction functions are executed in the post-processing phase for improving the final output.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With