calcHist

本文详细解析了OpenCV中calcHist函数的使用方法,包括channels参数的作用及如何指定不同维度的通道组合。通过示例代码展示了如何计算并可视化多维直方图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

calcHist函数的channels参数和narrays以及dims共同来确定用于计算直方图的图像;
首先dims是最终的直方图维数,narrays指出了arrays数组中图像的个数,其中每一幅图像都可以是任意通道的【只要最终dims不超过32即可】

如果channels参数为0,则narrays和dims必须相等,否则弹出assert,此时计算直方图的时候取数组中每幅图像的第0通道。
当channels不是0的时候,用于计算直方图的图像是arrays中由channels指定的通道的图像,channels与arrays中的图像的对应关系,如channels的参数说明的,将arrays中的图像从第0幅开始按照通道摊开排列起来,然后channels中的指定的用于计算直方图的就是这些摊开的通道;
假设有arrays中只有一幅三通道的图像image,那么narrays应该为1,如果是想计算3维直方图【最大也只能是3维的】,想将image的通道2作为第一维,通道0作为第二维,通道1作为第三维,则可以将channels设置为channesl={2,0,1};这样calcHist函数计算时就按照这个顺序来统计直方图。
可以看出channels不为0时narrays可以和dims不相等,只要保证arrays中至少有channels指定的通道就可以。

下面是例子和一个运行的图像,还是使用lena图像;

  1.     Mat src, hsv;  
  2.     if(!(src=imread("d:/picture/lena.bmp")).data)  
  3.         return -1;  
  4.     cvtColor(src, hsv, CV_BGR2HSV);  
  5.     vector<Mat> hsv_plane;  
  6.     split(hsv, hsv_plane);  
  7.     Mat inputs[]={hsv_plane[1], hsv_plane[2], hsv_plane[0]};  
  8.       
  9.     vector<Mat> mixmat_plane;  
  10.     mixmat_plane.push_back(hsv_plane[2]);  
  11.     mixmat_plane.push_back(hsv_plane[0]);  
  12.     Mat mixmat;  
  13.     merge(mixmat_plane, mixmat);  
  14.     Mat mixed[]={mixmat,hsv_plane[1]};  
  15.   
  16.     int vbins = 128, sbins = 128, hbins = 128;  
  17.     int histSize[] = {sbins, vbins, hbins};  
  18.     float sranges[] = { 0, 256};  
  19.     float vranges[] = { 0, 256};  
  20.     float hranges[] = { 0, 256};  
  21.     const float*ranges[] = {sranges, vranges, hranges};  
  22.     MatND hist;  
  23.   
  24. //#define SINGLE_MAT  
  25. #define MIX_MAT  
  26.   
  27. #ifdef SINGLE_MAT     
  28. /* 
  29. use one multi-channel mat, channels param gives the channels used;  
  30. 使用多通道的图像计算多维直方图,可以计算1,2,3维的; 
  31. */  int channels[] = {1, 2};  
  32.     calcHist(&hsv, 1, channels, Mat(),hist, 2, histSize, ranges,truefalse );  
  33. #elif defined MIX_MAT   
  34. /* 
  35. use mix mat array, the first elem is a single channel mat, second is a two channel mat; 
  36. 使用混合通道图像数组,第1个图像是2通道的,第2个是单通道的; 
  37. channels指定每一维对应的通道; 
  38. */    
  39.     int channels[] = {1, 2, 0};  
  40.   
  41. // #define DIM_2  
  42. #ifdef DIM_2  
  43. //统计二维直方图;  
  44.     calcHist(mixed, 2, channels, Mat(),hist, 2, histSize, ranges,truefalse);  
  45. #else  
  46. //统计三维直方图;  
  47.     calcHist(mixed, 2, channels, Mat(),hist, 3, histSize, ranges,truefalse);  
  48. #endif  
  49.   
  50. #else  
  51. /* 
  52. use multi-mat arrays, channels param gives the array mat and its channels used; 
  53. 使用都是单通道图像数组计算2维直方图--也可以计算3维的; 
  54. */    
  55.     int channels[] = {2, 1};  
  56.     hbins = 1;  
  57.     calcHist(inputs, 3, channels, Mat(),hist, 2, histSize, ranges,truefalse );  
  58. #endif  
  59.   
  60. #ifndef MIX_MAT  
  61.     double maxVal=0;  
  62.     minMaxLoc(hist, 0, 0, 0, 0);//only can process mat that dims<=2--minMaxLoc只能处理2维以下的;  
  63. #endif  
  64.   
  65.     int scale = 4;  
  66.     Mat histImg = Mat::zeros(vbins*scale, sbins*scale, CV_8UC3);  
  67.     float *hist_sta = new float[sbins];  
  68.     float *hist_val = new float[vbins];  
  69.     float *hist_hue = new float[hbins];  
  70.     memset(hist_val, 0, vbins*sizeof(float));  
  71.     memset(hist_sta, 0, sbins*sizeof(float));  
  72.     memset(hist_hue, 0, hbins*sizeof(float));  
  73.   
  74.     forint s = 0; s < sbins; s++ )  
  75.     {  
  76.         forint v = 0; v < vbins; v++ )  
  77.         {  
  78.             for(int h=0; h<hbins; h++)  
  79.             {  
  80. #ifdef MIX_MAT  
  81. //-----------------------------------------------------------//  
  82. #ifdef DIM_2  
  83.                 float binVal = hist.at<float>(s, v);  
  84. #else  
  85.                 float binVal = hist.at<float>(s, v, h);  
  86.                 hist_hue[h] += binVal;  
  87. #endif  
  88. //-----------------------------------------------------------//               
  89. #else  
  90.                 float binVal = hist.at<float>(s, v);  
  91.                 int intensity = cvRound(binVal*255/maxVal);  
  92.                 rectangle( histImg, Point(s*scale, v*scale),Point((s+1)*scale-1, (v+1)*scale-1), Scalar::all(intensity), CV_FILLED);  
  93. #endif  
  94.                 hist_val[v] += binVal;  
  95.                 hist_sta[s] += binVal;  
  96.   
  97.             }  
  98.         }  
  99.     }  
  100.     //find max bin value;  
  101.     double max_sta=.0, max_val=.0,max_hue=.0;  
  102.     for(int i=0; i<sbins; ++i)  
  103.     {  
  104.         if(hist_sta[i]>max_sta)  
  105.             max_sta = hist_sta[i];  
  106.     }  
  107.     for(int i=0; i<vbins; ++i)  
  108.     {  
  109.         if(hist_val[i]>max_val)  
  110.             max_val = hist_val[i];  
  111.     }  
  112.     for(int i=0; i<hbins; ++i)  
  113.     {  
  114.         if(hist_hue[i]>max_hue)  
  115.             max_hue = hist_hue[i];  
  116.     }  
  117.   
  118.     Mat sta_img = Mat::zeros(310, sbins*scale+20, CV_8UC3);  
  119.     Mat val_img = Mat::zeros(310, vbins*scale+20, CV_8UC3);  
  120.     Mat hue_img = Mat::zeros(310, hbins*scale+20, CV_8UC3);  
  121.   
  122.     for(int i=0; i<sbins; ++i)  
  123.     {  
  124.         int intensity = cvRound(hist_sta[i]*(sta_img.rows-10)/max_sta);  
  125.         rectangle(sta_img, Point(i*scale+10, sta_img.rows-intensity),Point((i+1)*scale-1+10, sta_img.rows-1), Scalar(0,255,0), 1);  
  126.     }  
  127.     for(int i=0; i<vbins; ++i)  
  128.     {  
  129.         int intensity = cvRound(hist_val[i]*(val_img.rows-10)/max_val);  
  130.         rectangle(val_img, Point(i*scale+10, val_img.rows-intensity),Point((i+1)*scale-1+10, val_img.rows-1), Scalar(0,0,255), 1);  
  131.     }  
  132.     for(int i=0; i<hbins; ++i)  
  133.     {  
  134.         int intensity = cvRound(hist_hue[i]*(hue_img.rows-10)/max_hue);  
  135.         rectangle(hue_img, Point(i*scale+10, hue_img.rows-intensity),Point((i+1)*scale-1+10, hue_img.rows-1), Scalar(255,0,0), 1);  
  136.     }  
  137.   
  138.     namedWindow( "Source");  
  139.     imshow( "Source", src );  
  140.     namedWindow( "Histogram");  
  141.     imshow( "Histogram", histImg );  
  142.     namedWindow("dim1");  
  143.     imshow("dim1", sta_img);  
  144.     namedWindow("dim2");  
  145.     imshow("dim2", val_img);      
  146.     namedWindow("dim3");  
  147.     imshow("dim3", hue_img);  
	Mat src, hsv;
	if(!(src=imread("d:/picture/lena.bmp")).data)
		return -1;
	cvtColor(src, hsv, CV_BGR2HSV);
	vector<Mat> hsv_plane;
	split(hsv, hsv_plane);
	Mat inputs[]={hsv_plane[1], hsv_plane[2], hsv_plane[0]};
	
	vector<Mat> mixmat_plane;
	mixmat_plane.push_back(hsv_plane[2]);
	mixmat_plane.push_back(hsv_plane[0]);
	Mat mixmat;
	merge(mixmat_plane, mixmat);
	Mat mixed[]={mixmat,hsv_plane[1]};

	int vbins = 128, sbins = 128, hbins = 128;
	int histSize[] = {sbins, vbins, hbins};
	float sranges[] = { 0, 256};
	float vranges[] = { 0, 256};
	float hranges[] = { 0, 256};
	const float*ranges[] = {sranges, vranges, hranges};
	MatND hist;

//#define SINGLE_MAT
#define MIX_MAT

#ifdef SINGLE_MAT	
/*
use one multi-channel mat, channels param gives the channels used; 
使用多通道的图像计算多维直方图,可以计算1,2,3维的;
*/	int channels[] = {1, 2};
	calcHist(&hsv, 1, channels, Mat(),hist, 2, histSize, ranges,true, false );
#elif defined MIX_MAT 
/*
use mix mat array, the first elem is a single channel mat, second is a two channel mat;
使用混合通道图像数组,第1个图像是2通道的,第2个是单通道的;
channels指定每一维对应的通道;
*/	
	int channels[] = {1, 2, 0};

// #define DIM_2
#ifdef DIM_2
//统计二维直方图;
	calcHist(mixed, 2, channels, Mat(),hist, 2, histSize, ranges,true, false);
#else
//统计三维直方图;
	calcHist(mixed, 2, channels, Mat(),hist, 3, histSize, ranges,true, false);
#endif

#else
/*
use multi-mat arrays, channels param gives the array mat and its channels used;
使用都是单通道图像数组计算2维直方图--也可以计算3维的;
*/	
	int channels[] = {2, 1};
	hbins = 1;
	calcHist(inputs, 3, channels, Mat(),hist, 2, histSize, ranges,true, false );
#endif

#ifndef MIX_MAT
	double maxVal=0;
	minMaxLoc(hist, 0, 0, 0, 0);//only can process mat that dims<=2--minMaxLoc只能处理2维以下的;
#endif

	int scale = 4;
	Mat histImg = Mat::zeros(vbins*scale, sbins*scale, CV_8UC3);
	float *hist_sta = new float[sbins];
	float *hist_val = new float[vbins];
	float *hist_hue = new float[hbins];
	memset(hist_val, 0, vbins*sizeof(float));
	memset(hist_sta, 0, sbins*sizeof(float));
	memset(hist_hue, 0, hbins*sizeof(float));

	for( int s = 0; s < sbins; s++ )
	{
		for( int v = 0; v < vbins; v++ )
		{
			for(int h=0; h<hbins; h++)
			{
#ifdef MIX_MAT
//-----------------------------------------------------------//
#ifdef DIM_2
				float binVal = hist.at<float>(s, v);
#else
				float binVal = hist.at<float>(s, v, h);
				hist_hue[h] += binVal;
#endif
//-----------------------------------------------------------//				
#else
				float binVal = hist.at<float>(s, v);
				int intensity = cvRound(binVal*255/maxVal);
				rectangle( histImg, Point(s*scale, v*scale),Point((s+1)*scale-1, (v+1)*scale-1), Scalar::all(intensity), CV_FILLED);
#endif
				hist_val[v] += binVal;
				hist_sta[s] += binVal;

			}
		}
	}
	//find max bin value;
	double max_sta=.0, max_val=.0,max_hue=.0;
	for(int i=0; i<sbins; ++i)
	{
		if(hist_sta[i]>max_sta)
			max_sta = hist_sta[i];
	}
	for(int i=0; i<vbins; ++i)
	{
		if(hist_val[i]>max_val)
			max_val = hist_val[i];
	}
	for(int i=0; i<hbins; ++i)
	{
		if(hist_hue[i]>max_hue)
			max_hue = hist_hue[i];
	}

	Mat sta_img = Mat::zeros(310, sbins*scale+20, CV_8UC3);
	Mat val_img = Mat::zeros(310, vbins*scale+20, CV_8UC3);
	Mat hue_img = Mat::zeros(310, hbins*scale+20, CV_8UC3);

	for(int i=0; i<sbins; ++i)
	{
		int intensity = cvRound(hist_sta[i]*(sta_img.rows-10)/max_sta);
		rectangle(sta_img, Point(i*scale+10, sta_img.rows-intensity),Point((i+1)*scale-1+10, sta_img.rows-1), Scalar(0,255,0), 1);
	}
	for(int i=0; i<vbins; ++i)
	{
		int intensity = cvRound(hist_val[i]*(val_img.rows-10)/max_val);
		rectangle(val_img, Point(i*scale+10, val_img.rows-intensity),Point((i+1)*scale-1+10, val_img.rows-1), Scalar(0,0,255), 1);
	}
	for(int i=0; i<hbins; ++i)
	{
		int intensity = cvRound(hist_hue[i]*(hue_img.rows-10)/max_hue);
		rectangle(hue_img, Point(i*scale+10, hue_img.rows-intensity),Point((i+1)*scale-1+10, hue_img.rows-1), Scalar(255,0,0), 1);
	}

	namedWindow( "Source");
	imshow( "Source", src );
	namedWindow( "Histogram");
	imshow( "Histogram", histImg );
	namedWindow("dim1");
	imshow("dim1", sta_img);
	namedWindow("dim2");
	imshow("dim2", val_img);	
	namedWindow("dim3");
	imshow("dim3", hue_img);


 

 

程序中使用了一些宏来控制不同的情况,比较简单一看就明白,毋庸多说。上图中的channels顺序是1,2,0,而图像数组是采用将VH组成一个两通道图像以及S图像放到一个数组中,即混合通道的数组,计算的是3通道的直方图,然后将每一维拆开了,分别显示在dim1-3中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值