[英]Drawing Histogram in OpenCV-Python
I was just trying to draw histogram using new OpenCV Python interface ( cv2 ). 我只是试图使用新的OpenCV Python接口(cv2)绘制直方图。
Below is the code i tried: 以下是我试过的代码:
import cv2
import numpy as np
import time
img = cv2.imread('zzz.jpg')
h = np.zeros((300,256,3))
b,g,r = cv2.split(img)
bins = np.arange(256).reshape(256,1)
color = [ (255,0,0),(0,255,0),(0,0,255) ]
for item,col in zip([b,g,r],color):
hist_item = cv2.calcHist([item],[0],None,[256],[0,255])
cv2.normalize(hist_item,hist_item,0,255,cv2.NORM_MINMAX)
hist=np.int32(np.around(hist_item))
pts = np.column_stack((bins,hist))
cv2.polylines(h,[pts],False,col)
h=np.flipud(h)
cv2.imshow('colorhist',h)
cv2.waitKey(0)
And it works fine. 它工作正常。 Below is the resulting histogram i obtained. 以下是我获得的结果直方图。
Then i modified the code a little bit. 然后我修改了一点代码。
ie changed the sixth line in code b,g,r = cv2.split(img)
to b,g,r = img[:,:,0], img[:,:,1], img[:,:,2]
(because it works a little faster than cv2.split
). 即改变代码b,g,r = cv2.split(img)
的第六行b,g,r = cv2.split(img)
为b,g,r = img[:,:,0], img[:,:,1], img[:,:,2]
(因为它的工作速度比cv2.split
快一点)。
Now the output is something different. 现在输出是不同的。 Below is the output. 以下是输出。
I checked the values of b,g,r
from both the codes. 我从两个代码中检查了b,g,r
的值。 They are same. 他们是一样的。
Difference lies in the output of cv2.calcHist
. 区别在于cv2.calcHist
的输出。 Result of hist_item
is different in both the cases. hist_item
结果在两种情况下都不同。
Question : 问题 :
How does it happen? 怎么会发生? Why the result of cv2.calcHist
is different when inputs are same? 当输入相同时,为什么cv2.calcHist
的结果不同?
EDIT 编辑
I tried a different code. 我尝试了不同的代码。 Now, a numpy version of my first code. 现在,我的第一个代码的numpy版本。
import cv2
import numpy as np
img = cv2.imread('zzz.jpg')
h = np.zeros((300,256,3))
b,g,r = img[:,:,0],img[:,:,1],img[:,:,2]
bins = np.arange(257)
bin = bins[0:-1]
color = [ (255,0,0),(0,255,0),(0,0,255) ]
for item,col in zip([b,g,r],color):
N,bins = np.histogram(item,bins)
v=N.max()
N = np.int32(np.around((N*255)/v))
N=N.reshape(256,1)
pts = np.column_stack((bin,N))
cv2.polylines(h,[pts],False,col,2)
h=np.flipud(h)
cv2.imshow('img',h)
cv2.waitKey(0)
And the output is same as first one. 输出与第一个相同。
You can get my original image here: zzz.jpg 你可以在这里获得我的原始图像: zzz.jpg
Thank you. 谢谢。
You should copy the array: 你应该复制数组:
b,g,r = img[:,:,0].copy(), img[:,:,1].copy(), img[:,:,2].copy()
But, since calcHist() can accept channels parameter, you need not to split your img to three array. 但是,由于calcHist()可以接受通道参数,因此您无需将img拆分为三个数组。
import cv2
import numpy as np
img = cv2.imread('zzzyj.jpg')
h = np.zeros((300,256,3))
bins = np.arange(256).reshape(256,1)
color = [ (255,0,0),(0,255,0),(0,0,255) ]
for ch, col in enumerate(color):
hist_item = cv2.calcHist([img],[ch],None,[256],[0,255])
cv2.normalize(hist_item,hist_item,0,255,cv2.NORM_MINMAX)
hist=np.int32(np.around(hist_item))
pts = np.column_stack((bins,hist))
cv2.polylines(h,[pts],False,col)
h=np.flipud(h)
cv2.imshow('colorhist',h)
cv2.waitKey(0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.