OpenCV Python equalizeHist color image

I need to perform histogram alignment for a color image.

First, I convert the color image to gray and pass 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 that I need to convert the image back to RGB; How can i do this?

+6
source share
4 answers

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) 
+16
source

I am not sure if it works correctly:

 def histogram_equalize(img): b, g, r = cv2.split(img) red = cv2.equalizeHist(r) green = cv2.equalizeHist(g) blue = cv2.equalizeHist(b) return cv2.merge((blue, green, red)) 
0
source

If you want to align the RGB image, u should not be converted to gray, and not align the RGB channels one at a time.

So, I think maybe this is what you need:

 def equalize_hist(img): for c in xrange(0, 2): img[:,:,c] = cv2.equalizeHist(img[:,:,c]) cv2.imshow('Histogram equalized', img) cv2.waitKey(0) return img 
0
source

A more general approach would be to convert the RGB values ​​to another space that contains the luminescence / intensity value (Luv, Lab, HSV, HSL), apply histeq only in the intensity plane and perform the inverse transformation.

0
source

All Articles