转载自http://blog.csdn.net/lu597203933/article/details/14104505
灰度直方图
这次我从最基本的直方图讲起,一维直方图,至于二维等高维直方图,仅作为了解,后面有时间另开blog详解。
1:首先我们给出一幅图
其中的数据假设对应一副灰度图片的灰度值,则直方图的作用就是画出在bin范围内取值的个数,如图左边的直方图---相当于我们word中的柱状图。
2:在opencv中直方图的结构为:
typedef struct CvHistogram
{
int type; // 不用管
CvArr* bins;存放直方图在每一维上直方柱的具体数据,由于存在多维直方图。如果是一维直方图,那么bins就是一个一维的矩阵;如果是二维直方图,那么bins就是一个二维的矩阵,等等。
float thresh[CV_MAX_DIM][2]; /* 直方柱的划分 是统一划分的,即均等划分的 */
float** thresh2; /*不均等划分,可以自动设定每一个直方柱的取值范围。之所以是二级指针,每一个柱的取值范围用一级指针表示,又存在多个直方柱,所以需要二级指针才能表示。每一个不在指定范围的值,会被忽略掉。*/
CvMatND mat; /* 存放直方图的数据 */
}
3:直方图的创建:
CvHistogram* cvCreateHist( int dims, int* sizes, int type, float** ranges=NULL, int uniform=1);
其中:
dims:表示直方图的维度
sizes:每一维上直方柱(bin)的数据 ----- 创建多大的矩阵 如一维则size[0] =256
二维:则size[0]=256,size[1] = 256;;
type ---- 创建一个什么样的矩阵
直方图存储数据的方式:
CV_HIST_ARRAY意味着直方图数据表示为多维密集数组CvMatND;
CV_HIST_TREE 意味着直方图数据表示为多维稀疏数组CvSparseMat.---- 大于0才开设相应的空间存储它<高维的必选它>
Ranges:
直方图在每一维度上的范围。
如果是一维则代码为:
Floatrange[] = {0,255};
Float*ranges[]={range};
如果是二维则代码为:
Floatb_range[] = {0,255};
Floatg_range[] ={0,255};
Float*ranges[] = {b_range, g_range};
uniform
该值为0时,表示bin的范围是程序员自由设定的。当该值为非零时,表示bin的划分,是均等划分。
4:创建一个直方图后,避免里面存在一些随机值,我们可以将其清除cvClearHist
代码: cvClearHist(hist);
5:计算图像的直方图 cvCalcHist
代码:cvCalcHist(&imgBlue, hist, 0, 0);
6:将得到的直方图画出来: 其中用到了函数 cvGetMinMaxHistVaule, cvQueryHistValue_1D, cvFillConvexPoly.
总结:以上给出得到直方图的主要步骤:直方图的创建—>计算图像的直方图—>画出直方图。以下给出完整的代码及运行的结果
- IplImage *DrawHistogram(CvHistogram*hist, float scaleX = 1, float scaleY = 1){ // 画直方图
- float histMax = 0;
- cvGetMinMaxHistValue(hist, 0 , &histMax, 0, 0); // 取得直方图中的最值
- IplImage *imgHist = cvCreateImage(cvSize(256 * scaleX, 64*scaleY), 8, 1);
- cvZero(imgHist); 清空随机值
- for(int i = 0; i < 255; i++)
- {
- float histValue = cvQueryHistValue_1D(hist, i); // 取得直方图中的i值
- float nextValue = cvQueryHistValue_1D(hist, i+1);
- int numPt = 5;
- CvPoint pt[5];
- pt[0] = cvPoint(i*scaleX, 64*scaleY);
- pt[1] = cvPoint((i+1)*scaleX, 64*scaleY);
- pt[2] = cvPoint((i+1)*scaleX, (1 -(nextValue/histMax))* 64 * scaleY);
- pt[3] = cvPoint((i+1)*scaleX, (1 -(histValue/histMax))* 64 * scaleY);
- pt[4] = cvPoint(i*scaleX, 64*scaleY);
- cvFillConvexPoly(imgHist, pt, numPt, cvScalarAll(255));
- }
- return imgHist;
- }
运行结果:
- #include <iostream>
- #include "cv.h"
- #include "highgui.h"
- #include "cxcore.h"
- using namespace std;
- IplImage *DrawHistogram(CvHistogram*hist, float scaleX = 1, float scaleY = 1){ // 画直方图
- float histMax = 0;
- cvGetMinMaxHistValue(hist, 0 , &histMax, 0, 0); // 取得直方图中的最值
- IplImage *imgHist = cvCreateImage(cvSize(256 * scaleX, 64*scaleY), 8, 1);
- cvZero(imgHist); 清空随机值
- for(int i = 0; i < 255; i++)
- {
- float histValue = cvQueryHistValue_1D(hist, i); // 取得直方图中的i值
- float nextValue = cvQueryHistValue_1D(hist, i+1);
- int numPt = 5;
- CvPoint pt[5];
- pt[0] = cvPoint(i*scaleX, 64*scaleY);
- pt[1] = cvPoint((i+1)*scaleX, 64*scaleY);
- pt[2] = cvPoint((i+1)*scaleX, (1 -(nextValue/histMax))* 64 * scaleY);
- pt[3] = cvPoint((i+1)*scaleX, (1 -(histValue/histMax))* 64 * scaleY);
- pt[4] = cvPoint(i*scaleX, 64*scaleY);
- cvFillConvexPoly(imgHist, pt, numPt, cvScalarAll(255));
- }
- return imgHist;
- }
- int main()
- {
- IplImage *img = cvLoadImage("F:\\tongtong.jpg",1);
- if(!img){
- cout << "No data img" << endl;
- }
- int dims = 1;
- int sizes = 256;
- float range[] = {0,255};
- float*ranges[]={range};
- CvHistogram *hist = cvCreateHist(dims, &sizes, CV_HIST_ARRAY, ranges, 1);
- cvClearHist(hist); //清除直方图里面的随机值
- IplImage *imgBlue = cvCreateImage(cvGetSize(img), 8, 1);
- IplImage *imgGreen = cvCreateImage(cvGetSize(img), 8, 1);
- IplImage *imgRed = cvCreateImage(cvGetSize(img), 8, 1);
- cvSplit(img, imgBlue, imgGreen, imgRed, NULL); //将多通道图像分解
- cvCalcHist(&imgBlue, hist, 0, 0); // 计算图像的直方图
- IplImage *histBlue = DrawHistogram(hist); // 将直方图中的数据画出来
- cvClearHist(hist);
- cvCalcHist(&imgGreen, hist, 0, 0);
- IplImage *histGreen = DrawHistogram(hist);
- cvClearHist(hist);
- cvCalcHist(&imgRed, hist, 0, 0);
- IplImage *histRed = DrawHistogram(hist);
- cvClearHist(hist);
- cvNamedWindow("show",0);
- cvNamedWindow("B", 0);
- cvNamedWindow("G", 0);
- cvNamedWindow("R", 0);
- cvShowImage("show",img);
- cvShowImage("B",histBlue);
- cvShowImage("G",histGreen);
- cvShowImage("R", histRed);
- cvWaitKey(0);
- cvReleaseImage(&img);
- cvDestroyWindow("show");
- cvReleaseImage(&histBlue);
- cvDestroyWindow("B");
- cvReleaseImage(&histGreen);
- cvDestroyWindow("G");
- cvReleaseImage(&histRed);
- cvDestroyWindow("R");
- return 0;
- }
/*这部分是后面添加的,没有另开blog进行讲解了,其实道理一样的*/
下面简单介绍二维的情况:要统计一个二维直方图,二维坐标+统计值,这就是三维的结构,就会变成一个3D图像,其实不然。我们的处理技巧是将二维坐标分别作为x和y轴,而统计值作为颜色值。如代码cvRectangle(imgHist, cvPoint(i*scaleX, j*scaleY), cvPoint((i+1)*scaleX- 1, (j+1)*scaleY - 1), CV_RGB(intensity,intensity,intensity), CV_FILLED); i和j分别代表坐标,intensity即为统计值。
Code:
- IplImage *DrawHistogram2(CvHistogram*hist, float scaleX = 1, float scaleY = 1){ // 画直方图
- float histMax = 0;
- cvGetMinMaxHistValue(hist, 0 , &histMax, 0, 0); // 取得直方图中的最值
- IplImage *imgHist = cvCreateImage(cvSize(256 * scaleX, 256*scaleY), 8, 3);
- cvZero(imgHist); 清空随机值
- for(int i = 0; i < 255; i++)
- {
- for(int j = 0; j < 255; j++)
- {
- float histValue = cvQueryHistValue_2D(hist, i, j); // 取得直方图中的i值
- int intensity = cvRound(histValue * 255 /histMax);
- float nextValue = cvQueryHistValue_1D(hist, i+1);
- int numPt = 5;
- CvPoint pt[5];
- pt[0] = cvPoint(i*scaleX, j*scaleY);
- pt[1] = cvPoint(i*scaleX, (j+1)*scaleY - 1);
- pt[2] = cvPoint((i+1)*scaleX - 1, (j+1)*scaleY - 1);
- pt[3] = cvPoint((i+1)*scaleX - 1, j*scaleY);
- pt[4] = cvPoint(i*scaleX, j*scaleY);
- cvFillConvexPoly(imgHist, pt, numPt, cvScalarAll(intensity));
- //cvRectangle(imgHist, cvPoint(i*scaleX, j*scaleY), cvPoint((i+1)*scaleX - 1, (j+1)*scaleY - 1), CV_RGB(intensity,intensity,intensity), CV_FILLED);
- // (i+1)*scaleX - 1代表下一个坐标点(i+1)*scaleX的最后一点
- }
- }
- return imgHist;
- }
- int main()
- {
- IplImage *img = cvLoadImage("F:\\baboon.jpg",1);
- if(!img){
- cout << "No data img" << endl;
- }
- int dims = 2;
- int sizes[2] = {256, 256}; // 创建多大的矩阵
- float b_range[] = {0,255}; // 矩阵的取值范围
- float g_range[] = {0, 255};
- float*ranges[]={b_range, g_range};
- CvHistogram *hist = cvCreateHist(dims, sizes, CV_HIST_ARRAY, ranges, 1);
- cvClearHist(hist); //清除直方图里面的随机值
- IplImage *imgBlue = cvCreateImage(cvGetSize(img), 8, 1);
- IplImage *imgGreen = cvCreateImage(cvGetSize(img), 8, 1);
- IplImage *imgRed = cvCreateImage(cvGetSize(img), 8, 1);
- IplImage *planes[] = {imgBlue, imgGreen}; // 二维是这样处理的********
- cvSplit(img, imgBlue, imgGreen, imgRed, NULL); //将多通道图像分解
- cvCalcHist(planes,hist, 0, 0);
- IplImage *bg_image = DrawHistogram2(hist);
- cvNamedWindow("show",0);
- cvShowImage("show",img);
- cvNamedWindow("bg_image");
- cvShowImage("bg_image",bg_image);
- cvWaitKey(0);
- cvReleaseImage(&img);
- cvReleaseImage(&bg_image);
- cvDestroyWindow("show");
- cvDestroyWindow("bg_image");
- return 0;
- }
Result:
作者:小村长 出处:http://blog.csdn.net/lu597203933 欢迎转载或分享,但请务必声明文章出处。 (新浪微博:小村长zack, 欢迎交流!)