如何使用OpenCV在Python中绘制灰度图像的32像素直方图 [英] How do I plot a 32-Bin Histogram for a Grayscale Image in Python using OpenCV

查看:727
本文介绍了如何使用OpenCV在Python中绘制灰度图像的32像素直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直难以为正在处理的640x480灰度图像生成直方图.

I have been having difficulty trying to generate a histogram for a 640x480 grayscale image I am working with.

我正在使用Python 2.7.3,OpenCV 2.4.6(Python绑定)和Numpy

I am using Python 2.7.3, OpenCV 2.4.6 (Python bindings) and Numpy

以下图像是使用可执行软件工具(用C ++编程)从同一图像生成的.

The image below was generated from the same image, using an executable software tool (programmed in C++)

此直方图的属性为:

bins = 50
hist_width = 250
normalised_height_max = 50

因此图像规格为250x50

The image specs are therefore 250x50

我已经查阅了此文档:

OpenCV中的直方图计算 http://docs.opencv.org/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html

Histogram Calculation in OpenCV http://docs.opencv.org/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html

Hist.py-OpenCV Python示例 https://github.com/Itseez/opencv/blob/master/samples/python2/hist.py

Hist.py - OpenCV Python Samples https://github.com/Itseez/opencv/blob/master/samples/python2/hist.py

第二个参考文献中的代码可以很好地编译,但是我尝试对其进行编辑以获取这些块样式列而不是细线,而且我似乎无法正确理解.

The code in the second reference compiles fine, yet I have tried to edit it to get these block style columns rather than the thin lines and I can't seem to get it right.

import cv2
import numpy as np

cv2.namedWindow('colorhist', cv2.CV_WINDOW_AUTOSIZE)

img = cv2.imread('sample_image.jpg')
h = np.zeros((50,256))

bins = np.arange(32).reshape(32,1)
hist_item = cv2.calcHist([img],0,None,[32],[0,256])
cv2.normalize(hist_item,hist_item,64,cv2.NORM_MINMAX)
hist=np.int32(np.around(hist_item))
pts = np.column_stack((bins,hist))
cv2.polylines(h,[pts],False,(255,255,255))

h=np.flipud(h)

cv2.imshow('colorhist',h)
cv2.waitKey(0)

我的目标是按照以下规格制作直方图:

I am aiming to make my histogram with the following specs:

bins = 32
hist_width = 256
normalised_height_max = 64

如何修复此代码,以实现具有指定规格的上述直方图?

How can I fix this code in order to achieve a histogram like the one above with the specs specified?

推荐答案

我设法解决了这个问题:

I have managed to solve the problem:

import cv2
import numpy as np

#Create window to display image
cv2.namedWindow('colorhist', cv2.CV_WINDOW_AUTOSIZE)

#Set hist parameters

hist_height = 64
hist_width = 256
nbins = 32
bin_width = hist_width/nbins

#Read image in grayscale mode
img = cv2.imread('sample_image.jpg',0)

#Create an empty image for the histogram
h = np.zeros((hist_height,hist_width))

#Create array for the bins
bins = np.arange(nbins,dtype=np.int32).reshape(nbins,1)

 #Calculate and normalise the histogram
hist_item = cv2.calcHist([img],[0],None,[nbins],[0,256])
cv2.normalize(hist_item,hist_item,hist_height,cv2.NORM_MINMAX)
hist=np.int32(np.around(hist_item))
pts = np.column_stack((bins,hist))

#Loop through each bin and plot the rectangle in white
for x,y in enumerate(hist):
    cv2.rectangle(h,(x*bin_width,y),(x*bin_width + bin_width-1,hist_height),(255),-1)

#Flip upside down
h=np.flipud(h)

#Show the histogram
cv2.imshow('colorhist',h)
cv2.waitKey(0)

这是结果:

请注意,图像的底部与C ++实现略有不同.我认为这是由于在代码中四舍五入

Note that the bottom of the image is slightly different to the C++ implementation. I assume this is due to rounding somewhere in the code

这篇关于如何使用OpenCV在Python中绘制灰度图像的32像素直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆