本文转自:http://www.xuebuyuan.com/1014703.html

特别提醒读者:注意实例中数据成员很多都定义成数据,这是由于calcHist函数形参要求的。

直方图在图形处理中很常用,直方图可以统计图像的像素特征分布,用于修改图像显示,修改图像内容,通过比较不同图片的直方图可以识别和跟踪特殊纹理的物体和图像,下面先学习怎么计算图像的直方图。

opencv2提供calchist函数可以方便的计算直方图。

calchist函数头文件 #include <opencv2/imgproc/imgproc.hpp>

calchist函数定义:

//! computes the joint dense histogram for a set of images.
CV_EXPORTS 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 );//! computes the joint sparse histogram for a set of images.
CV_EXPORTS 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 );CV_EXPORTS_W void calcHist( InputArrayOfArrays images,const vector<int>& channels,InputArray mask, OutputArray hist,const vector<int>& histSize,const vector<float>& ranges,bool accumulate=false );

举例说明函数应用:

Histogram1D::Histogram1D(){histSize[0] = 256; hranges[0] = 0.0;hranges[1] = 255.0;ranges[0] = hranges;channels[0] = 0;
}cv::MatND Histogram1D::getHistogram(const cv::Mat &image){cv::MatND hist;cv::calcHist(&image,   //source image1,        //histogram from 1 image onlychannels, //the channel usedcv::Mat(),//no mask is uesdhist,     //the resulting histogram1,        //it is a 1D histogramhistSize, //number of binsranges    //pixel value range);//直方图函数return hist;
}

函数参数介绍:

const Mat* images      //源图像组

int nimages       (Number of source arrays)   //源图像组图像个数

const int* channels   (List of the dims channels used to compute the histogram.)   //图像信道

InputArray mask   ( Optional mask. If the matrix is not empty, it must be an 8-bit array of the same size as arrays[i].  The non-zero mask elements mark the array elements counted in the histogram.)
                          //可选的掩码,如果不为空,则必须是8-bit数组,而且大小和原图像相同,非零位置为要计算的直方  图区域

OutputArray hist   (Output histogram, which is a dense or sparse dims -dimensional array.)
                       //输出直方图数组,稠密或者稀疏,dims维的数组

int dims    ( Histogram dimensionality that must be positive and not greater than CV_MAX_DIMS)
                        //处理直方图的维数正数,最大32维,CV_MAX_DIMS是32.

const int* histSize   ( Array of histogram sizes in each dimension.)
                      //每一维的直方图的尺寸大小

const float** 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.)
                                  //直方图每一维的数据大小范围

下面是计算1维图像的直方图:

cv::Mat Histogram1D::getHistogramImage(const cv::Mat &image){//compute histogram firstcv::MatND hist = getHistogram(image);//get min and max bin valuesdouble maxVal = 0;double minVal = 0;cv::minMaxLoc(hist,&minVal,&maxVal,0,0);//Image on which to display histogramcv::Mat histImg(histSize[0],histSize[0],CV_8U,cv::Scalar(255));//set highest point at 90% of nbins int hpt = static_cast<int>(0.9*histSize[0]);//Draw a vertical line for each bin for (int h =0;h<histSize[0];h++){float binVal = hist.at<float>(h);int intensity = static_cast<int>(binVal*hpt/maxVal);cv::line(histImg,cv::Point(h,histSize[0]),cv::Point(h,histSize[0]-intensity),cv::Scalar::all(0));}return histImg;
}

源图像:

histogram:

计算H-S直方图分布:

/*********************************************内容:计算H-S 直方图分布      时间:2013 5.27作者:恋上蛋炒面
*********************************************/
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;void main()
{Mat source = imread("baboon.jpg");namedWindow("Source");imshow("Source",source);Mat hsv;cvtColor(source,hsv,CV_BGR2HSV);//Quantize the hue to 60 levels//and the saturation to 64 levelsint hbins = 60,sbins = 64;int histSize[] = {hbins,sbins};//hue varies from 0 to 179float hranges[] = {0,180};//saturation varies from 0 to 255float sranges[] = {0,255};const float *ranges[] = {hranges,sranges};//two channels 0th,1thint channels[] = {0,1};MatND hist;//compute h-s histogramcalcHist(&hsv,1,channels,Mat(),hist,2,histSize,ranges);//get the max valuedouble maxVal = .0;minMaxLoc(hist,0,&maxVal,0,0);int scale = 8;//show the histogram on the imageMat 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*0.9*255/maxVal);rectangle(histImg,Point(h*scale,s*scale),Point((h+1)*scale-1,(s+1)*scale-1),Scalar::all(intensity),CV_FILLED);}}namedWindow("H-S Histogram");imshow("H-S Histogram",histImg);imwrite("hshistogram.jpg",histImg);waitKey(0);
}

源图像:

h-s histogram:

RGB直方图:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;void main()
{//Mat source = imread("red.jpg");Mat source = imread("baboon.jpg");//Mat source(300,300,CV_8UC3,Scalar(1,1,244));//imwrite("red.jpg",source);namedWindow("Source");imshow("Source",source);int channels_r[1],channels_g[1],channels_b[1],histSize[1];float hranges[2];const float *ranges[1];histSize[0] = 256;hranges[0] = 0.0;hranges[1] = 255.0;ranges[0] = hranges;channels_b[0] = 0;channels_g[0] = 1;channels_r[0] = 2;MatND hist_r,hist_g,hist_b;double max_val_r,max_val_g,max_val_b;Mat histImage(histSize[0],3*histSize[0],CV_8UC3);//RcalcHist(&source,1,channels_r,Mat(),hist_r,1,histSize,ranges);minMaxLoc(hist_r,0,&max_val_r,0,0);//GcalcHist(&source,1,channels_g,Mat(),hist_g,1,histSize,ranges);minMaxLoc(hist_r,0,&max_val_g,0,0);//BcalcHist(&source,1,channels_b,Mat(),hist_b,1,histSize,ranges);minMaxLoc(hist_r,0,&max_val_b,0,0);for (int i =0;i<histSize[0];i++){float binVal_r = hist_r.at<float>(i);float binVal_g = hist_g.at<float>(i);float binVal_b = hist_b.at<float>(i);int intensity_r = static_cast<int>(0.9*histSize[0]*binVal_r/max_val_r);int intensity_g = static_cast<int>(0.9*histSize[0]*binVal_g/max_val_g);int intensity_b = static_cast<int>(0.9*histSize[0]*binVal_b/max_val_b);line(histImage,Point(i,histImage.rows),Point(i,histImage.rows-intensity_r),Scalar(0,0,255));line(histImage,Point(i+histSize[0],histImage.rows),Point(i+histSize[0],histImage.rows-intensity_g),Scalar(0,255,0));line(histImage,Point(i+histSize[0]*2,histImage.rows),Point(i+histSize[0]*2,histImage.rows-intensity_b),Scalar(255,0,0));}namedWindow("RGB Histogram");imshow("RGB Histogram",histImage);waitKey(0);
}

源图像:图上图

RGB-Histogram:

<pre class="cpp" name="code"><pre class="cpp" name="code"><pre class="cpp" name="code"><pre class="cpp" name="code"><pre class="cpp" name="code"><pre>

opencv 直方图 CV::calcHist使用相关推荐

  1. 23 OpenCV直方图计算calcHist

    一.直方图 图像直方图是基于图像像素值,其实对图像梯度.每个像素的角度.等一切图像的属性值,我们都可以建立直方图.直方图最常见的几个属性: dims: 需要统计的特征的数据 bins:每个特征空间子区 ...

  2. OpenCV直方图计算Histogram Calculation

    OpenCV直方图均衡Histogram Equalization 直方图均衡Histogram Equalization 目标 什么是直方图? OpenCV为您提供什么 代码 解释 结果 直方图均衡 ...

  3. OpenCV学习笔记(八)——直方图的计算与绘制(cv.calcHist()、plt.hist()、plt.imshow())

    目录 1 直方图的计算 2 直方图的绘制 2.1 cv.line()和cv.polylines() 2.2 plt.hist() 3 2D 直方图 3.1 cv.calcHist() 3.2 plt. ...

  4. 【拜小白opencv】45-二维H-S直方图绘制----calcHist()函数、minMaxLoc()函数

    常言道"温故而知新",写此文章就是对自己目前学习内容的小小的总结与记录. 本文力求用最简洁的语言,详细的代码将此部分内容讲解清楚,但由于博主同样是刚刚接触OpenCV,或许表达上有 ...

  5. Opencv中直方图函数calcHist

    Opencv中直方图函数calcHist calcHist函数在Opencv中是极难理解的一个函数,一方面是参数说明晦涩难懂,另一方面,说明书给出的实例也不足以令人完全搞清楚该函数的使用方式.最难理解 ...

  6. 【opencv-c++】cv::calcHist计算直方图

    [opencv-c++]cv::calcHist计算直方图 1.背景 2.示例 1.背景 直方图顾名思义就是矩形块组成的图,如下图: 直方图是数值数据分布的图形表示.构建直方图的步骤如下: 第1步是将 ...

  7. OpenCV 图像直方图计算calcHist()

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/keith_bb/article/details/56680997 图像直方图是对数据集合的一种统计方 ...

  8. 使用Python,OpenCV计算图像直方图(cv2.calcHist)

    使用Python,OpenCV计算图像直方图(cv2.calcHist 1. 效果图 2. 原理 2.1 什么是图像直方图? 2.2 计算直方图 2.3 可视化蒙版区域 3. 源码 参考 这篇博客将介 ...

  9. OpenCV直方图计算函数calcHist详解

    原文转自https://docs.opencv.org/2.4/modules/imgproc/doc/histograms.html 文档是英文的,应该不难看懂,就不给翻译了. ---------- ...

最新文章

  1. 不学51直接学stm32可以吗?学stm32需要哪些基础?
  2. Swagger中配置了@ApiModelProperty的allowableValues属性但不显示的问题
  3. 010_Raphael事件
  4. Algorithm之OP:OP之GA遗传算法思路理解相关配图资料
  5. kafka通过零拷贝实现高效的数据传输
  6. 转载:常见的正则表达式
  7. Linux备份策略研究
  8. 第001讲 html介绍 html运行原理
  9. 什么是句柄?为什么会有句柄?HANDLE
  10. Ramp Number
  11. 基础工具组件starter-idempotent-redission设计与实现
  12. 如何制定一个App推广活动方案
  13. 微信小程序学习日记7
  14. Linux-centos7自我学习
  15. DLL 导出方法(两种)
  16. libnetwork
  17. 费马小定理看了等于没看证明
  18. TL-WR740N_V4板TTL刷机指南_TFTP
  19. 有哪些小巧好用的pdf阅读器
  20. C#实现图片从数据库的上传和下载

热门文章

  1. 【Android】Activity详解
  2. 51单片机 | 蜂鸣器实验
  3. 重命名Active Directory域名
  4. mysql insert 阻塞_insert遭遇阻塞
  5. 模电一、半导体二极管和三极管
  6. MySql:事务的ACID特性
  7. 国产系统部署服务器,国产服务器操作系统取得重大突破
  8. MySQL中IN对NULL的处理
  9. expdp与impdp导出导入特定表
  10. window下Slik SVN的安装配置