从头学习opencv(9)--直方图

直方图

#直方图
def plt_demo(image):
    plt.hist(image.ravel(),256,[0,256])
    # numpy的ravel函数功能是将多维数组降为一维数组
    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()

直方图完整代码

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

#直方图属于统计信息,卷积属于特征信息

#直方图
def plt_demo(image):
    plt.hist(image.ravel(),256,[0,256])
    # numpy的ravel函数功能是将多维数组降为一维数组
    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("C:/Users/Administrator/Desktop/demo.png")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
plt_demo(src)
image_hist(src)
cv.waitKey(0)
cv.destroyAllWindows()
print("Hi,python!")

运行结果
在这里插入图片描述
在这里插入图片描述

函数补充

https://www.cnblogs.com/FHC1994/p/9118351.html

注意:

1.numpy的ravel函数功能是将多维数组降为一维数组。参考博客:https://blog.csdn.net/lanchunhui/article/details/50354978

2.matplotlib.pyplot.hist函数主要是计算直方图。

hist函数原型:hist(x, bins=None, range=None, density=None, weights=None, cumulative=False, bottom=None, histtype=‘bar’, align=‘mid’, orientation=‘vertical’, rwidth=None, log=False, color=None, label=None, stacked=False, normed=None, hold=None, data=None, **kwargs)

x参数表示是一个数组或一个序列,是指定每个bin(箱子)分布的数据

bins参数表示指定bin(箱子)的个数,也就是总共有几条条状图

range参数表示箱子的下限和上限。即横坐标显示的范围,范围之外的将被舍弃。

参考博客:https://blog.csdn.net/u013571243/article/details/48998619

3.enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据下标和数据,一般用在 for 循环当中。

4.cv2.calcHist的原型为:calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]]) -> hist

images参数表示输入图像,传入时应该用中括号[ ]括起来

channels参数表示传入图像的通道,如果是灰度图像,那就不用说了,只有一个通道,值为0,如果是彩色图像(有3个通道),那么值为0,1,2,中选择一个,对应着BGR各个通道。这个值也得用[ ]传入。

mask参数表示掩膜图像。如果统计整幅图,那么为None。主要是如果要统计部分图的直方图,就得构造相应的掩膜来计算。

histSize参数表示灰度级的个数,需要中括号,比如[256]

ranges参数表示像素值的范围,通常[0,256]。此外,假如channels为[0,1],ranges为[0,256,0,180],则代表0通道范围是0-256,1通道范围0-180。

hist参数表示计算出来的直方图。

参考:https://blog.csdn.net/YZXnuaa/article/details/79231817

5.关于pyplot模块里plot()函数、xlim()函数等的用法参考:

https://blog.csdn.net/cymy001/article/details/78344316

https://blog.csdn.net/chinwuforwork/article/details/51786967

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值