新版本对直方图不再使用之前的histogram的形式,而是用统一的Mat或者MatND的格式来存储直方图,可见新版本Mat数据结构的优势。

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

计算直方图

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 fromimages[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 asimages[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 ofranges[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.

void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, intshift=0)

画矩形

  1. #include "stdafx.h"
  2.  
  3. #include <cv.h>
  4. #include <highgui.h>
  5.  
  6. using namespace cv;
  7.  
  8. int main( int argc, char** argv )
  9. {
  10. Mat src, hsv;
  11.  
  12. /* if( argc != 2 || !(src=imread(argv[1], 1)).data )
  13. return -1; */
  14.  
  15. src=imread("zhang.jpg", 1);
  16.  
  17. cvtColor(src, hsv, CV_BGR2HSV);
  18.  
  19. // Quantize the hue to 30 levels
  20. // and the saturation to 32 levels
  21. int hbins = 30, sbins = 32; // bin 步长
  22.  
  23. int histSize[] = {hbins, sbins};
  24. // hue varies from 0 to 179, see cvtColor
  25. float hranges[] = { 0, 180 };
  26. // saturation varies from 0 (black-gray-white) to
  27. // 255 (pure spectrum color)
  28. float sranges[] = { 0, 256 };
  29. const float* ranges[] = { hranges, sranges };
  30. MatND hist;
  31. // we compute the histogram from the 0-th and 1-st channels
  32. int channels[] = {0, 1}; // --- hue && saturation
  33.  
  34. calcHist( &hsv, 1, channels, Mat(), // do not use mask
  35. hist, 2, histSize, ranges,
  36. true, // the histogram is uniform
  37. false );
  38. double maxVal=0;
  39. minMaxLoc(hist, 0, &maxVal, 0, 0); // Finds the global minimum and maximum in an array.
  40. // void minMaxLoc(InputArray src, double* minVal, double* maxVal=0, Point* minLoc=0, Point* maxLoc=0, InputArray mask=noArray())
  41.  
  42. // 直方图显示
  43. int scale = 10;
  44. Mat histImg = Mat::zeros(sbins*scale, hbins*10, CV_8UC3);
  45.  
  46. for( int h = 0; h < hbins; h++ )
  47. for( int s = 0; s < sbins; s++ )
  48. {
  49. float binVal = hist.at<float>(h, s);
  50. int intensity = cvRound(binVal*255/maxVal);
  51. rectangle( histImg, Point(h*scale, s*scale),
  52. Point( (h+1)*scale - 1, (s+1)*scale - 1),
  53. Scalar::all(intensity), // 二维直方图,颜色之深浅代表出现个数之多寡
  54. CV_FILLED );
  55. }
  56.  
  57. namedWindow( "Source", 1 );
  58. imshow( "Source", src );
  59.  
  60. namedWindow( "H-S Histogram", 1 );
  61. imshow( "H-S Histogram", histImg );
  62. waitKey();
  63. }

C++: void equalizeHist(InputArray src, OutputArray dst)

直方图均衡化

Parameters:

  • src – Source 8-bit single channel image.

  • dst – Destination image of the same size and type as src .

The function equalizes the histogram of the input image using the following algorithm:

  1. Calculate the histogram for src .

  2. Normalize the histogram so that the sum of histogram bins is 255.

  3. Compute the integral of the histogram:

  4. Transform the image using as a look-up table:

compareHist

double compareHist(const SparseMat& H1, const SparseMat& H2, int method)

直方图比较

Parameters:

  • H1 – First compared histogram.

  • H2 – Second compared histogram of the same size as H1 .
  • method

    Comparison method that could be one of the following:

    • CV_COMP_CORREL Correlation  相关性 相同为1,范围0<x<=1

    • CV_COMP_CHISQR Chi-Square   卡方 相同为0 [0,inf)
    • CV_COMP_INTERSECT Intersection   直方图交 ,数值越大越相似
    • CV_COMP_BHATTACHARYYA Bhattacharyya distance
    • CV_COMP_HELLINGER Synonym for CV_COMP_BHATTACHARYYA Bhattacharyya 距离,相同为0 [0,inf)

  1. #include "stdafx.h"
  2.  
  3. #include <cv.h>
  4. #include <highgui.h>
  5. #include "stdio.h"
  6.  
  7. using namespace std;
  8. using namespace cv;
  9.  
  10. int main( int argc, char** argv )
  11. {
  12. Mat src1, src2,dst;
  13. Mat hsv1,hsv2;
  14. MatND hist1,hist2;
  15.  
  16. src1=imread("zhang.jpg", 1);
  17. src2=imread("zhou.jpg",1);
  18. cvtColor(src1,hsv1,CV_RGB2HSV);
  19. cvtColor(src2,hsv2,CV_RGB2HSV);
  20.  
  21. int hbins=30,sbins=32;
  22. int histSize[]={hbins,sbins};
  23.  
  24. float hranges[]={0,180};
  25. float sranges[]={0,256};
  26. const float* ranges[]={hranges,sranges};
  27.  
  28. int channels[]={0,1};
  29.  
  30. calcHist(&hsv1,1,channels,Mat(),hist1,2,histSize,ranges,true,false);
  31. calcHist(&hsv2,1,channels,Mat(),hist2,2,histSize,ranges,true,false);
  32.  
  33. double temp;
  34. temp=compareHist(hist1,hist2,CV_COMP_CORREL);
  35. cout<<"CV_COMP_CORREL "<<temp<<endl;
  36.  
  37. temp=compareHist(hist1,hist2,CV_COMP_CHISQR);
  38. cout<<"CV_COMP_CHISQR "<<temp<<endl;
  39.  
  40. temp=compareHist(hist1,hist2,CV_COMP_INTERSECT);
  41. cout<<"CV_COMP_INTERSECT "<<temp<<endl;
  42.  
  43. temp=compareHist(hist1,hist2,CV_COMP_BHATTACHARYYA);
  44. cout<<"CV_COMP_BHATTACHARYYA "<<temp<<endl;
  45.  
  46. namedWindow("src1");
  47. imshow("src1",src1);
  48.  
  49. namedWindow("src2");
  50. imshow("src2",src2);
  51.  
  52. waitKey();
  53.  
  54. cvDestroyAllWindows();
  55. return 0;
  56. }

遇到 ~ 编译器错误 C2078

初始值设定项的数目超过了要初始化的对象数。

  1. // C2078.cpp
  2. int main() {
  3. int d[2] = {1, 2, 3}; // C2078
  4. int e[2] = {1, 2}; // OK
  5.  
  6. char a[]={"a", "b"}; // C2078
  7. char *b[]={"a", "b"}; // OK
  8. char c[]={'a', 'b'}; // OK
  9. }
  1.  
  1.  
  • OPENCV(5) —— 图像直方图的更多相关文章

    1. 【图像处理】基于OpenCV实现图像直方图的原理

      背景 图像的直方图是衡量图像像素分布的一种方式,可以通过分析像素分布,使用直方图均衡化对图像进行优化,让图像变的清晰. opencv官方对图像直方图的定义如下: 直方图是图像中像素强度分布的图形表达方 ...

    2. OpenCV(7)-图像直方图

      直方图定义可参考这里.图像的直方图用来表示图像像素的统计信息,它统计了图像每一个通道(如果是多通道)中,每个像素的个数(比例). 计算直方图 OpenCV提供了直接计算直方图的函数 void calc ...

    3. 8、OpenCV Python 图像直方图

      __author__ = "WSX" import cv2 as cv import numpy as np from matplotlib import pyplot as pl ...

    4. opencv:图像直方图均衡化

      // 直方图均衡化 Mat gray, dst; cvtColor(src, gray, COLOR_BGR2GRAY); equalizeHist(gray, dst); imshow(" ...

    5. OpenCV 绘制图像直方图

      OpenCV绘制图像直方图,版本2.4.11 直方图可展示图像中的像素分布,是用以表示数字图像中亮度分布的直方图,标绘了图像中每个亮度值的像素数.可以借助观察该直方图了解需要如何调整亮度分布.这种直方 ...

    6. opencv:图像直方图相似性比较

      void hist_compare(Mat src1, Mat src2) { int histSize[] = { 256, 256, 256 }; int channels[] = { 0, 1, ...

    7. OpenCV成长之路(5):图像直方图的应用

      正如第4篇文章所说的图像直方图在特征提取方面有着很重要的作用,本文将举两个实际工程中非常实用的例子来说明图像直方图的应用. 一.直方图的反向映射. 我们以人脸检测举例,在人脸检测中,我们第一步往往需要 ...

    8. OpenCV成长之路(4):图像直方图

      一.图像直方图的概念 图像直方图是反映一个图像像素分布的统计表,其实横坐标代表了图像像素的种类,可以是灰度的,也可以是彩色的.纵坐标代表了每一种颜色值在图像中的像素总数或者占所有像素个数的百分比. 图 ...

    9. OpenCV成长之路:图像直方图的应用

      OpenCV成长之路:图像直方图的应用 2014-04-11 13:57:03 标签:opencv 图像 直方图 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否 ...

    随机推荐

    1. Linux头文件的设置

      GCC/G++会查找系统默认的include和link的路径,以及自己在编译命令中指定的路径. 1.include头文件路径 除了默认的/usr/include, /usr/local/include ...

    2. [置顶] Netty学习总结(1)——Netty入门介绍

      1.Netty是什么? Netty是一个基于JAVA NIO类库的异步通信框架,它的架构特点是:异步非阻塞.基于事件驱动.高性能.高可靠性和高可定制性. 2.使用Netty能够做什么? 开发异步.非阻 ...

    3. Thinking in States

      Thinking in States Niclas Nilsson PEOPLE IN THE REAL WORLD HAVE A WEIRD RELATIONSHIP WITH STATE. Thi ...

    4. POJ 2570 Fiber Network(最短路 二进制处理)

      题目翻译 一些公司决定搭建一个更快的网络.称为"光纤网". 他们已经在全世界建立了很多网站.这 些网站的作用类似于路由器.不幸的是,这些公司在关于网站之间的接线问题上存在争论,这样 ...

    5. Android4.2.2下Stagefright下OMX编解码器组件的控制流

      本文均属自己阅读源代码的点滴总结.转账请注明出处谢谢. 欢迎和大家交流. qq:1037701636 email:gzzaigcn2012@gmail.com Android源代码版本号Version ...

    6. linux文件时间的查看和改动touch

      1. linux文件的时间 linux下文件时间主要有以下三种: 1.1 modification time(mtime) 文件改动时间.即文件内容的改动时,更新这个时间.不包含文件权限和属性的改动. ...

    7. Android 提示: The connection to adb is down, and a severe error has occured.

      今天早上打开Eclipse,一直提示 The connection to adb is down, and a severe error has occured,无法执行程序.重新启动Eclipse. ...

    8. bzoj1433: [ZJOI2009]假期的宿舍(最大二分图匹配)

      1433: [ZJOI2009]假期的宿舍 题目:传送门 题解: 这题有点水 跑个二分图匹配就完事了(注意在校生不是一定都互相认识) 代码: #include<cstdio> #inclu ...

    9. Find or Query Data with the mongo Shell

      https://docs.mongodb.com/getting-started/shell/query/ Overview You can use the find() method to issu ...

    10. m_Orchestrate learning system---二十五、复制类的时候最容易出现的错误是什么

      m_Orchestrate learning system---二十五.复制类的时候最容易出现的错误是什么 一.总结 一句话总结:命名空间错误导致Analyze类虽然继承了Base类,但是没有执行里面 ...