Python中绘制直方图

Python中绘制直方图

本文介绍了在OpenCV-Python中绘制直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想使用新的OpenCV Python接口(cv2)绘制直方图。

I was just trying to draw histogram using new OpenCV Python interface ( cv2 ).

下面是我尝试的代码:

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.

即将代码 b,g,r = cv2.split(img)中的第六行更改为 b,g,r = img [:,:,0],img [:,:,1],img [:,:,2] cv2.split )。

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).

现在的输出是不同的。下面是输出。

Now the output is something different. Below is the output.

我检查了 b,g,r 的值两个代码。他们是一样的。

I checked the values of b,g,r from both the codes. They are same.

区别在于 cv2.calcHist 的输出。 hist_item 的结果在两种情况下都不同。

Difference lies in the output of cv2.calcHist. Result of hist_item is different in both the cases.

问题

为什么输入相同时 cv2.calcHist 的结果不同?

How does it happen? Why the result of cv2.calcHist is different when inputs are same?

EDIT

我尝试了不同的代码。现在,我的第一个代码的numpy版本。

I tried a different code. Now, a numpy version of my first code.

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)

输出与第一个相同。

您可以在这里取得我的原始图片:

You can get my original image here: zzz.jpg

谢谢。

推荐答案

您应该复制数组:

b,g,r = img[:,:,0].copy(), img[:,:,1].copy(), img[:,:,2].copy()


$ b b

但是,由于calcHist()可以接受通道参数,因此不需要将img拆分为三个数组。

But, since calcHist() can accept channels parameter, you need not to split your img to three array.

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)

这篇关于在OpenCV-Python中绘制直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 23:12