直方圖實在是這個世界上最有用的工具之一了,做做統計 做做均衡化,幹啥都要用到它。
下面給出自己用的一段簡單的程序,將圖像的H分量分離出來計算直方圖:H分量分成16個等級
int hsize = 16;
float hranges[] = {0,180};
const float* phranges = hranges;
int ch[] = {0, 0};
Mat hsv_src, hue_src,hist_src;
cvtColor(img_src, hsv_src, CV_BGR2HSV); //OpenCV默認的圖片通道是BGR。IOS 是 RGBA
hue_src.create(hsv_src.size(), hsv_src.depth());
mixChannels(&hsv_src, 1, &hue_src, 1, ch, 1);
calcHist(&hue_src, 1, 0, Mat(), hist_src, 1, &hsize, &phranges);
上面分離h通道是怎麼做的呢,通過mixChannels 將hsv中的0號通道 放到hue_src中。那麼就被分割出來了。
普通的通道分割是怎麼做呢?
vector<Mat> img_plane; //或者如果知道是3通道 就std::vector<cv::Mat> img_plane(3);
//或者 std::vector<cv::Mat> img_plane(img.channels());
split(img, img_plane);
得到img_plane[channel_index]索引到圖像不同的通道
然後對通道做完操作之後,可以用merge函數將它們合成一個Mat
merge(img_planes, img_end)
如果我們只是對一個通道做某件事情 就不需要拆出那麼矩陣,而通過mixChannels 分離出一個通道,然後再通過mixChannels融合回去。
下面我是拆分了v通道
int ch[] = {2, 0}; int ch1[] = {0,2};
……
mixChannels(&hsv_src, 1, &v_src, 1, ch, 1);
……
mixChannels(&v_src, 1, &hsv_src, 1,ch1 , 1);
網上有一個簡單的直方圖顯示函數 備份在這裏
使用的時候 直接用
Mat histImg = imHist(hist_src,5,5);
namedWindow( "H-S Histogram", 1 );
imshow( "H-S Histogram", histImg );
waitKey();
就可以了。
Mat imHist(Mat hist, float scaleX=1, float scaleY=1){
double maxVal=0;
minMaxLoc(hist, 0, &maxVal, 0, 0);
int rows = 64; //default height size
int cols = hist.rows; //get the width size from the histogram
Mat histImg = Mat::zeros(rows*scaleX, cols*scaleY, CV_8UC3);
//for each bin
for(int i=0;i<cols-1;i++) {
float histValue = hist.at<float>(i,0);
float nextValue = hist.at<float>(i+1,0);
Point pt1 = Point(i*scaleX, rows*scaleY);
Point pt2 = Point(i*scaleX+scaleX, rows*scaleY);
Point pt3 = Point(i*scaleX+scaleX, (rows-nextValue*rows/maxVal)*scaleY);
Point pt4 = Point(i*scaleX, (rows-nextValue*rows/maxVal)*scaleY);
int numPts = 5;
Point pts[] = {pt1, pt2, pt3, pt4, pt1};
fillConvexPoly(histImg, pts, numPts, Scalar(255,255,255));
}
return histImg;
}
有人對直方圖函數做了詳盡的測試 可見 http://blog.csdn.net/ljbsdu/article/details/7420429
這是OpenCV給出的示例程序
#include <cv.h> #include <highgui.h> using namespace cv; int main( int argc, char** argv ) { Mat src, hsv; if( argc != 2 || !(src=imread(argv[1], 1)).data ) return -1; cvtColor(src, hsv, CV_BGR2HSV); // Quantize the hue to 30 levels // and the saturation to 32 levels int hbins = 30, sbins = 32; int histSize[] = {hbins, sbins}; // hue varies from 0 to 179, see cvtColor float hranges[] = { 0, 180 }; // saturation varies from 0 (black-gray-white) to // 255 (pure spectrum color) float sranges[] = { 0, 256 }; const float* ranges[] = { hranges, sranges }; MatND hist; // we compute the histogram from the 0-th and 1-st channels int channels[] = {0, 1}; calcHist( &hsv, 1, channels, Mat(), // do not use mask hist, 2, histSize, ranges, true, // the histogram is uniform false ); double maxVal=0; minMaxLoc(hist, 0, &maxVal, 0, 0); int scale = 10; Mat histImg = Mat::zeros(sbins*scale, hbins*10, CV_8UC3); for( int h = 0; h < hbins; h++ ) for( int s = 0; s < sbins; s++ ) { float binVal = hist.at<float>(h, s); int intensity = cvRound(binVal*255/maxVal); rectangle( histImg, Point(h*scale, s*scale), Point( (h+1)*scale - 1, (s+1)*scale - 1), Scalar::all(intensity), CV_FILLED ); } namedWindow( "Source", 1 ); imshow( "Source", src ); namedWindow( "H-S Histogram", 1 ); imshow( "H-S Histogram", histImg ); waitKey(); }