直方图实在是这个世界上最有用的工具之一了,做做统计 做做均衡化,干啥都要用到它。
下面给出自己用的一段简单的程序,将图像的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(); }