好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Python opencv(九) 图像直方图

前提条件

使用工具python3.x 使用库 :numpy、opencv,

api简介

1.cv.calcHist cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate ]]) ->hist 参数说明: 一、images(输入图像)参数必须用方括号括起来。 二、计算直方图的通道(三通道:0,1,2)。 三、Mask(掩膜),一般用None,表示处理整幅图像。 四、histSize,表示这个直方图分成多少份(即多少个直方柱)。 五、range,直方图中各个像素的值,[0.0, 256.0]表示直方图能表示像素值从0.0到256的像素。 六、最后是两个可选参数,由于直方图作为函数结果返回了,所以第六个hist就没有意义了(待确定) 最后一个accumulate是一个布尔值,用来表示直方图是否叠加。 (cv.calcHist参考:https://blog.csdn.net/qq_42250840/article/details/104878333)

代码示例

import cv2 as cvimport numpy as npfrom matplotlib import pyplot as pltdef plot_demo(image):
	plt.hist(image.ravel(),256,[0,256])
	plt.show('直方图')def image_hist(image):
	color = ('blue', 'green', 'red')
	for i, color in enumerate(color):
		hist = cv.calcHist([image], [i], None, [256], [0,256])
		plt.plot(hist, color=color)
		plt.xlim([0,256])
	plt.show()src=cv.imread("D:/openCV_image/image/1.png")# src=cv.imread("D:/openCV_image/image/3.png")# src=cv.imread("D:/openCV_image/image/2_2.png")# print(src)cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)cv.imshow("input image",src)# plot_demo(src)image_hist(src)cv.waitKey(0)cv.destroyAllWindows()

效果:

查看更多关于Python opencv(九) 图像直方图的详细内容...

  阅读:31次