如何用python绘制灰度直方图_如何使用OpenCV在Python中绘制灰度图像的32Bin直方图...

我设法解决了这个问题: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)

结果是:

7kPDs5l.png

注意,图像的底部与C++实现略有不同。我想这是因为代码中的某个地方舍入

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值