OpenCV Python equalizeHist 彩色图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31998428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenCV Python equalizeHist colored image
提问by Fariss Abdo
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:
首先,我将彩色图像转换为灰色并将其提供给equalizeHist
函数:
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?
但在此之后,我需要将图像转换回 RGB;我怎样才能做到这一点?
回答by Mohammad Al Jazaery
来源: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
~编辑:原始链接不再可用,这里实现了类似的想法: Histogram Equalization of a Color image with OpenCV
回答by ehsan
i'm not sure that it works properly :
我不确定它是否正常工作:
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))
回答by Sanjay Chan
If u want to equalizeHist the RGB image, u should not convert to gray instead of equalize RGB channels one by one.
如果你想均衡化 RGB 图像,你不应该转换为灰色,而是一个一个均衡化 RGB 通道。
So, i think maybe here is what u want:
所以,我想也许这就是你想要的:
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
回答by AFocsA
A more general approach would be transforming RGB values into another space that contains a luminescence/intensity value (Luv, Lab, HSV, HSL), apply histeq only in intensity plane and perform the inverse transform.
更通用的方法是将 RGB 值转换为另一个包含发光/强度值(Luv、Lab、HSV、HSL)的空间,仅在强度平面中应用 histeq 并执行逆变换。
回答by sammens19
You do not have to first convert your image to grayscale. You can use the approach below. A suggested solution above used the YUV colour space but I will do this example using the HSV colour space.
您不必先将图像转换为灰度。您可以使用以下方法。上面建议的解决方案使用了 YUV 颜色空间,但我将使用 HSV 颜色空间来做这个例子。
image = cv2.imread("photo.jpg")
# convert image from RGB to HSV
img_hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
# Histogram equalisation on the V-channel
img_hsv[:, :, 2] = cv2.equalizeHist(img_hsv[:, :, 2])
# convert image back from HSV to RGB
image = cv2.cvtColor(img_hsv, cv2.COLOR_HSV2RGB)
cv2.imshow("equalizeHist", image)
cv2.waitKey(0)
回答by Homura Akemi
img_yuv = cv2.cvtColor(image, cv2.COLOR_BGR2YUV)
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(2,2))
img_yuv[:,:,0] = clahe.apply(img_yuv[:,:,0])
img = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR)
cv2.imshow("equalizeHist", img)
cv2.waitKey(0)