轉自http://blog.csdn.net/xiaowei_cqu/article/details/7600666
灰度直方圖是數字圖像中最簡單且有用的工具,這一篇主要總結OpenCV中直方圖CvHistogram的結構和應用。
灰度直方圖的定義
OpenCV中的直方圖CvHistogram
- typedef struct CvHistogram
- {
- int type;
- CvArr* bins; //存放每個灰度級數目的數組指針
- float thresh[CV_MAX_DIM][2]; //均勻直方圖
- float** thresh2; //非均勻直方圖
- CvMatND mat; //直方圖數組的內部數據結構
- }
- CvHistogram;
創建直方圖 cvCreateHist()
- CvHistogram* cvCreateHist(
- int dims, //直方圖維數
- int* sizes,//直翻圖維數尺寸
- int type, //直方圖的表示格式
- float** ranges=NULL, //圖中方塊範圍的數組
- int uniform=1 //歸一化標識
- );
type有兩種:CV_HIST_ARRAY 意味着直方圖數據表示爲多維密集數組 CvMatND; CV_HIST_TREE 意味着直方圖數據表示爲多維稀疏數組 CvSparseMat。
- void cvCalcHist(
- IplImage** image, //輸入圖像(也可用CvMat**)
- CvHistogram* hist, //直方圖指針
- int accumulate=0, //累計標識。如果設置,則直方圖在開始時不被清零。
- const CvArr* mask=NULL //操作 mask, 確定輸入圖像的哪個象素被計數
- );
實踐:一維直方圖
- int main( )
- {
- IplImage * src= cvLoadImage("baboon.jpg");
- IplImage* gray_plane = cvCreateImage(cvGetSize(src),8,1);
- cvCvtColor(src,gray_plane,CV_BGR2GRAY);
- int hist_size = 256; //直方圖尺寸
- int hist_height = 256;
- float range[] = {0,255}; //灰度級的範圍
- float* ranges[]={range};
- //創建一維直方圖,統計圖像在[0 255]像素的均勻分佈
- CvHistogram* gray_hist = cvCreateHist(1,&hist_size,CV_HIST_ARRAY,ranges,1);
- //計算灰度圖像的一維直方圖
- cvCalcHist(&gray_plane,gray_hist,0,0);
- //歸一化直方圖
- cvNormalizeHist(gray_hist,1.0);
- int scale = 2;
- //創建一張一維直方圖的“圖”,橫座標爲灰度級,縱座標爲像素個數(*scale)
- IplImage* hist_image = cvCreateImage(cvSize(hist_size*scale,hist_height),8,3);
- cvZero(hist_image);
- //統計直方圖中的最大直方塊
- float max_value = 0;
- cvGetMinMaxHistValue(gray_hist, 0,&max_value,0,0);
- //分別將每個直方塊的值繪製到圖中
- for(int i=0;i<hist_size;i++)
- {
- float bin_val = cvQueryHistValue_1D(gray_hist,i); //像素i的概率
- int intensity = cvRound(bin_val*hist_height/max_value); //要繪製的高度
- cvRectangle(hist_image,
- cvPoint(i*scale,hist_height-1),
- cvPoint((i+1)*scale - 1, hist_height - intensity),
- CV_RGB(255,255,255));
- }
- cvNamedWindow( "GraySource", 1 );
- cvShowImage("GraySource",gray_plane);
- cvNamedWindow( "H-S Histogram", 1 );
- cvShowImage( "H-S Histogram", hist_image );
- cvWaitKey(0);
- }
- IplImage* r_plane = cvCreateImage( cvGetSize(src), 8, 1 );
- IplImage* g_plane = cvCreateImage( cvGetSize(src), 8, 1 );
- IplImage* b_plane = cvCreateImage( cvGetSize(src), 8, 1 );
- IplImage* planes[] = { r_plane, g_plane };
- //將HSV圖像分離到不同的通道中
- cvCvtPixToPlane( src, b_plane, g_plane, r_plane, 0 );
- // 生成二維直方圖數據結構
- int r_bins =256, b_bins = 256;
- CvHistogram* hist;
- {
- int hist_size[] = { r_bins, b_bins };
- float r_ranges[] = { 0, 255 }; // hue is [0,180]
- float b_ranges[] = { 0, 255 };
- float* ranges[] = { r_ranges,b_ranges };
- hist = cvCreateHist( 2, hist_size, CV_HIST_ARRAY, ranges, 1);
- }
- //計算一張或多張單通道圖像image(s) 的直方圖
- cvCalcHist( planes, hist, 0, 0 );
- for( int h = 0; h < r_bins; h++ ) {
- for( int s = 0; s < b_bins; s++ ) {
- float bin_val = cvQueryHistValue_2D( hist, h, s ); //查詢直方塊的值
- int intensity = cvRound( bin_val * 255 / max_value );
- cvRectangle( hist_img,
- cvPoint( h*scale, s*scale ),
- cvPoint( (h+1)*scale - 1, (s+1)*scale - 1),
- CV_RGB(intensity,intensity,intensity),
- CV_FILLED);
- }
- }