OpenCV画HSV的3D直方图 cvQueryHistValue_3D

来源:互联网 发布:蓝格眼镜软件 编辑:程序博客网 时间:2024/06/05 09:16

此代码是根据opencv附带例子修改而成。

[cpp] view plaincopy
  1. #include <iostream>  
  2. #include <time.h>  
  3. #include <cv.h>  
  4. #include <highgui.h>  
  5. #include <math.h>  
  6. #include "CommandParser.h"  
  7. using namespace std;  
  8. int main(int argc, char** argv)  
  9. {  
  10.     void WrongUsage();  
  11.     CommandParser cp(argc,argv); // object to parse command line  
  12.       
  13.     double total_time =0;  
  14.     double score= 0.8;  
  15.    
  16.       
  17.     //Load Template image   
  18.     char *param;  
  19.     char * filename;  
  20.     filename=param = cp.GetParameter("-i");  
  21.     if(param==NULL)  
  22.     {  
  23.         cout<<"ERROR: Template image argument missing";  
  24.         WrongUsage();  
  25.         return -1;  
  26.     }  
  27.     IplImage * src= cvLoadImage(param);  
  28.   
  29.     IplImage* hsv = cvCreateImage( cvGetSize(src), 8, 3 );  
  30.     IplImage* h_plane = cvCreateImage( cvGetSize(src), 8, 1 );  
  31.     IplImage* s_plane = cvCreateImage( cvGetSize(src), 8, 1 );  
  32.     IplImage* v_plane = cvCreateImage( cvGetSize(src), 8, 1 );  
  33.     IplImage* planes[] = { h_plane, s_plane ,v_plane};  
  34.    
  35.     /** H 分量划分为16个等级,S分量划分为8个等级 */  
  36.     int h_bins = 10, s_bins = 6,v_bins = 6;  
  37.     param = cp.GetParameter("-h");  
  38.     if(param)  
  39.     {  
  40.         h_bins=atoi(param);  
  41.     }  
  42.     param = cp.GetParameter("-s");  
  43.     if(param)  
  44.     {  
  45.         s_bins=atoi(param);  
  46.     }  
  47.     param = cp.GetParameter("-v");  
  48.     if(param)  
  49.     {  
  50.         v_bins=atoi(param);  
  51.     }  
  52.     int hist_size[] = {h_bins, s_bins,v_bins};  
  53.    
  54.     /** H 分量的变化范围 */  
  55.     float h_ranges[] = { 0, 180 };   
  56.    
  57.     /** S 分量的变化范围*/  
  58.     float s_ranges[] = { 0, 255 };  
  59.     float v_ranges[] = { 0, 255 };  
  60.     float* ranges[] = { h_ranges, s_ranges,v_ranges };  
  61.    
  62.     /** 输入图像转换到HSV颜色空间 */  
  63.     cvCvtColor( src, hsv, CV_BGR2HSV );  
  64.     cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 );  
  65.   
  66.     /** 创建直方图,二维, 每个维度上均分 */  
  67.     CvHistogram * hist = cvCreateHist(3, hist_size, CV_HIST_ARRAY, ranges, 1 );  
  68.     /** 根据H,S两个平面数据统计直方图 */  
  69.     cvCalcHist( planes, hist, 0, 0 );  
  70.    
  71.     /** 获取直方图统计的最大值,用于动态显示直方图 */  
  72.     float max_value;  
  73.     int maxid[4]={0};  
  74.     cvGetMinMaxHistValue( hist, 0, &max_value, 0, maxid );  
  75.    
  76.    
  77.     /** 设置直方图显示图像 */  
  78.     int height = 520;  
  79.     int ivh=height/v_bins;  
  80.     int width = (h_bins*s_bins*6);//每个bin6像素  
  81.     IplImage* hist_img = cvCreateImage( cvSize(width,height), 8, 3 );  
  82.     cvZero( hist_img );  
  83.     CvFont font = cvFont(1,1);   
  84.   
  85.     /** 用来进行HSV到RGB颜色转换的临时单位图像 */  
  86.     IplImage * hsv_color = cvCreateImage(cvSize(1,1),8,3);  
  87.     IplImage * rgb_color = cvCreateImage(cvSize(1,1),8,3);  
  88.     int bin_w = width / (h_bins * s_bins);  
  89.     char dspStr1[80];  
  90.     sprintf(dspStr1,"max=%f",max_value);  
  91.     cvPutText(hist_img,dspStr1,cvPoint(20,20),&font,cvScalar(0,0xff));   
  92.     cout<< dspStr1 <<endl;  
  93.     float tatal_val=0;  
  94.     bool bh=false;  
  95.    float htotal=0.0;  
  96.    for(int v=0;v<v_bins;v++)  
  97.    {  
  98.      for(int h = 0; h < h_bins; h++)  
  99.     {  
  100.          bh=false;  
  101.          float hto=0.0;  
  102.          int i=0;  
  103.         for(int s = 0; s < s_bins; s++)  
  104.         {  
  105.              i = h*s_bins + s;  
  106.             /** 获得直方图中的统计次数,计算显示在图像中的高度 */  
  107.             float bin_val = cvQueryHistValue_3D( hist, h, s,v );  
  108.             int intensity = cvRound(bin_val*ivh/max_value);  
  109.             tatal_val+=bin_val;  
  110.             hto+=bin_val;  
  111.             if(bin_val==max_value)  
  112.             {  
  113.                  bh=true;   
  114.                sprintf(dspStr1,"h=%d,s=%d,v=%d",h,s,v);  
  115.                cvPutText(hist_img,dspStr1,cvPoint(20,50),&font,cvScalar(0xff,0xaf));   
  116.                cout<< dspStr1 <<endl;  
  117.             }  
  118.             /** 获得当前直方图代表的颜色,转换成RGB用于绘制 */  
  119.             float vhr=(v+0.5)*255.f/v_bins;//避免显示黑色   
  120.             cvSet2D(hsv_color,0,0,cvScalar(h*180.f / h_bins,(s+0.5)*255.f/s_bins,vhr,0));  
  121.             cvCvtColor(hsv_color,rgb_color,CV_HSV2BGR);  
  122.             CvScalar color = cvGet2D(rgb_color,0,0);  
  123.    
  124.             cvRectangle( hist_img, cvPoint(i*bin_w,(v+1)*ivh),  
  125.                 cvPoint((i+1)*bin_w,(v+1)*ivh - intensity),  
  126.                 color, -1, 8, 0 );  
  127.         }  
  128.         if(bh)  
  129.             {  
  130.               htotal=hto;  
  131.             }  
  132.        cvLine(hist_img,cvPoint((i+1)*bin_w,v*ivh),cvPoint((i+1)*bin_w,(v+1)*ivh),cvScalar(0xA,0xff,0x98));  
  133.     }  
  134.      cvLine(hist_img,cvPoint(0,(v+1)*ivh),cvPoint(width,(v+1)*ivh),cvScalar(0xA,0xff,0x98));  
  135.    }  
  136.     sprintf(dspStr1,"pixels=%f",tatal_val);  
  137.     cvPutText(hist_img,dspStr1,cvPoint(20,80),&font,cvScalar(0,0xff));   
  138.     cout<< dspStr1 <<endl;  
  139.     sprintf(dspStr1,"rate=%f",htotal/tatal_val);  
  140.     cvPutText(hist_img,dspStr1,cvPoint(20,110),&font,cvScalar(0,0xff,0xff));   
  141.     cout<< dspStr1 <<endl;  
  142.     sprintf(dspStr1,"maxrt=%f",max_value/tatal_val);  
  143.     cvPutText(hist_img,dspStr1,cvPoint(20,140),&font,cvScalar(0xff,0xaf));  
  144.     cout<< dspStr1 <<endl;  
  145.     cvNamedWindow(filename, 1 );  
  146.     cvShowImage( filename, src );  
  147.    
  148.     cvNamedWindow( "H-S Histogram", 1 );  
  149.     cvShowImage( "H-S Histogram", hist_img );  
  150.    
  151.     cvWaitKey(0);  
  152.   
  153.   
  154.    
  155. }  
  

代码效果如下:

原图

原图

3D 直方图

3d直方图

 

图上表明最大像素数的HSV的值,以及该值所占比率。

0 0
原创粉丝点击