原文转自https://docs.opencv.org/2.4/modules/imgproc/doc/histograms.html

文档是英文的,应该不难看懂,就不给翻译了。

-------------------------------------------------------------------------------------------------------------

Histograms

calcHist

Calculates a histogram of a set of arrays.

C++: void calcHist(const Mat* images, int nimages, const int* channels, InputArray mask, OutputArray hist, int dims, const int* histSize, const float** ranges, bool uniform=true, bool accumulate=false )

C++: void calcHist(const Mat* images, int nimages, const int* channels, InputArray mask, SparseMat& hist, int dims, const int* histSize, const float** ranges, bool uniform=true, bool accumulate=false )

Python: cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]]) → hist

C: void cvCalcHist(IplImage** image, CvHistogram* hist, int accumulate=0, const CvArr* mask=NULL )

Python: cv.CalcHist(image, hist, accumulate=0, mask=None) → None

Parameters:
  • images – Source arrays. They all should have the same depth, CV_8U or CV_32F , and the same size. Each of them can have an arbitrary number of channels.
  • nimages – Number of source images.
  • channels – List of the dims channels used to compute the histogram. The first array channels are numerated from 0 to images[0].channels()-1 , the second array channels are counted from images[0].channels() to images[0].channels() +images[1].channels()-1, and so on.
  • mask – Optional mask. If the matrix is not empty, it must be an 8-bit array of the same size as images[i] . The non-zero mask elements mark the array elements counted in the histogram.
  • hist – Output histogram, which is a dense or sparse dims -dimensional array.
  • dims – Histogram dimensionality that must be positive and not greater than CV_MAX_DIMS (equal to 32 in the current OpenCV version).
  • histSize – Array of histogram sizes in each dimension.
  • ranges – Array of the dims arrays of the histogram bin boundaries in each dimension. When the histogram is uniform ( uniform =true), then for each dimension i it is enough to specify the lower (inclusive) boundary  of the 0-th histogram bin and the upper (exclusive) boundary  for the last histogram bin histSize[i]-1 . That is, in case of a uniform histogram each of ranges[i] is an array of 2 elements. When the histogram is not uniform ( uniform=false ), then each of ranges[i] contains histSize[i]+1 elements:  . The array elements, that are not between  and  , are not counted in the histogram.
  • uniform – Flag indicating whether the histogram is uniform or not (see above).
  • accumulate – Accumulation flag. If it is set, the histogram is not cleared in the beginning when it is allocated. This feature enables you to compute a single histogram from several sets of arrays, or to update the histogram in time.

The functions calcHist calculate the histogram of one or more arrays. The elements of a tuple used to increment a histogram bin are taken from the corresponding input arrays at the same location. The sample below shows how to compute a 2D Hue-Saturation histogram for a color image.

#include <cv.h>
#include <highgui.h>using namespace cv;int main( int argc, char** argv )
{Mat src, hsv;if( argc != 2 || !(src=imread(argv[1], 1)).data )return -1;cvtColor(src, hsv, CV_BGR2HSV);// Quantize the hue to 30 levels// and the saturation to 32 levelsint hbins = 30, sbins = 32;int histSize[] = {hbins, sbins};// hue varies from 0 to 179, see cvtColorfloat hranges[] = { 0, 180 };// saturation varies from 0 (black-gray-white) to// 255 (pure spectrum color)float sranges[] = { 0, 256 };const float* ranges[] = { hranges, sranges };MatND hist;// we compute the histogram from the 0-th and 1-st channelsint channels[] = {0, 1};calcHist( &hsv, 1, channels, Mat(), // do not use maskhist, 2, histSize, ranges,true, // the histogram is uniformfalse );double maxVal=0;minMaxLoc(hist, 0, &maxVal, 0, 0);int scale = 10;Mat histImg = Mat::zeros(sbins*scale, hbins*scale, CV_8UC3);for( int h = 0; h < hbins; h++ )for( int s = 0; s < sbins; s++ ){float binVal = hist.at<float>(h, s);int intensity = cvRound(binVal*255/maxVal);rectangle( histImg, Point(h*scale, s*scale),Point( (h+1)*scale - 1, (s+1)*scale - 1),Scalar::all(intensity),CV_FILLED );}namedWindow( "Source", 1 );imshow( "Source", src );namedWindow( "H-S Histogram", 1 );imshow( "H-S Histogram", histImg );waitKey();
}

Note

  • An example for creating histograms of an image can be found at opencv_source_code/samples/cpp/demhist.cpp
  • (Python) An example for creating color histograms can be found at opencv_source/samples/python2/color_histogram.py
  • (Python) An example illustrating RGB and grayscale histogram plotting can be found at opencv_source/samples/python2/hist.py

OpenCV直方图计算函数calcHist详解相关推荐

  1. 详解为什么OpenCV的直方图计算函数calcHist()计算出的灰度值为255的像素个数为0

    在使用OpenCV的直方图计算函数calcHist()时,发现灰度值为255的像素个数总是为0. 哪怕图像中灰度值为255的像素个数不为0,使用OpenCV的直方图计算函数calcHist()计算出的 ...

  2. [Python图像处理] 三十七.OpenCV直方图统计两万字详解(掩膜直方图、灰度直方图对比、黑夜白天预测)

    该系列文章是讲解Python OpenCV图像处理知识,前期主要讲解图像入门.OpenCV基础用法,中期讲解图像处理的各种算法,包括图像锐化算子.图像增强技术.图像分割等,后期结合深度学习研究图像识别 ...

  3. Opencv双目校正函数 stereoRectify 详解

    目录 函数的[官方解释](https://docs.opencv.org/4.x/d9/d0c/group__calib3d.html#ga617b1685d4059c6040827800e72ad2 ...

  4. 归一化php,归一化函数normalize详解

    opencv 2 归一化函数normalize详解 1. 归一化定义与作用 归一化就是要把需要处理的数据经过处理后(通过某种算法)限制在你需要的一定范围内.首先归一化是为了后面数据处理的方便,其次是保 ...

  5. python normalize函数_归一化函数normalize详解

    opencv 2 归一化函数normalize详解 1. 归一化定义与作用 归一化就是要把需要处理的数据经过处理后 (通过某种算法)限制在你需要的一定范围内.首先归一化是为了后面数据处理的方便,其次是 ...

  6. 归一化函数normalize详解

    opencv 2 归一化函数normalize详解 1. 归一化定义与作用     归一化就是要把需要处理的数据经过处理后(通过某种算法)限制在你需要的一定范围内.首先归一化是为了后面数据处理的方便, ...

  7. OpenCV函数remap详解

    OpenCV函数remap详解 remap的作用是将原影像映射到目标影像的函数. 这是OpenCV文档中的说明. 但这个描述给人的感觉是,云里雾里,到底是需要计算目标到原的映射关系,还是原到目标的映射 ...

  8. OpenCv中的cv::Mat::create()函数,cvRound(),cvFloor(),cvCeil()函数的详解l

    文件说明: cv::create()函数的详解 函数原型: inline void Mat::create(int _rows, int _cols, int _type) inline void M ...

  9. [Python从零到壹] 五十一.图像增强及运算篇之图像灰度直方图对比分析万字详解

    欢迎大家来到"Python从零到壹",在这里我将分享约200篇Python系列文章,带大家一起去学习和玩耍,看看Python这个有趣的世界.所有文章都将结合案例.代码和作者的经验讲 ...

最新文章

  1. KMeans聚类并绘制聚类后的决策边界
  2. 10 行 Python 代码写的模糊查询
  3. Socket实现java服务端与AndroidApp端数据交互
  4. 成功解决利用matplotlib.pyplot进行绘图的时候整个画布中的绘制曲线只显示一部分
  5. poj-1284(Primitive Roots)(欧拉函数运用)
  6. 测试wifi网络常用软件,网络基础-常用网络测试工具
  7. 数据结构----快速排序
  8. 构建自己的简单微服务架构(开源)
  9. 引入js_好程序员web前端教程分享js中的模块化一
  10. 大数据之-Hadoop之HDFS的API操作_文件IO流_上传_案例---大数据之hadoop工作笔记0063
  11. iTextSharp 使用详解(转)
  12. OSPFv3中LSA详解(二)——Router LSA详解
  13. Git 报错:error: The requested URL returned error: 401 Unauthorized while accessing
  14. [Python从零到壹] 四十三.图像增强及运算篇之图像点运算和图像灰度化处理
  15. 字节跳动 Go 语言面试高频题
  16. 阿里云DataV专业版发布,为可视化创造更多可能!
  17. Sentinel-2 哨兵二号数据(Level-1C)下载及预处理教程
  18. Legendshop工时记录系统
  19. 常用PC服务器阵列卡、硬盘健康监控
  20. i.MX8QM环境搭建

热门文章

  1. 商标申请号查询办法有哪些
  2. 飞机qar数据可视化_图片 合抱之木 生于毫末——记“737NG飞机基于QAR数据的辅助排故系统”研发之路_民航新闻_民航资源网...
  3. 数据库系统之sql语言
  4. html5中设置省略号颜色,CSS3中text-overflow实现文章标题带省略号的显示效果(代码实例 )...
  5. 前端模板引擎 artTemplate的 使用与进阶
  6. 我的2016,我的苦辣酸甜
  7. python snownlp情感分析_白杨数说 | 不会做文本情感分析?试试这两个Python包
  8. 一道简单的ctf 分析
  9. 分布式事务:seata
  10. MAVIC 山地 轮组(整理)