【OpenCV】繪製長條圖

來源:互聯網
上載者:User

和這一篇《數位影像長條圖》內容是一樣的,只是使用Mat格式實現~

繪製灰色長條圖
//繪製灰階長條圖int main(  ){Mat src,gray;src=imread("baboon.jpg");cvtColor(src,gray,CV_RGB2GRAY);int bins = 256;int hist_size[] = {bins};float range[] = { 0, 256 };const float* ranges[] = { range};MatND hist;int channels[] = {0};calcHist( &gray, 1, channels, Mat(), // do not use maskhist, 1, hist_size, ranges,true, // the histogram is uniformfalse );double max_val;minMaxLoc(hist, 0, &max_val, 0, 0);int scale = 2;int hist_height=256;Mat hist_img = Mat::zeros(hist_height,bins*scale, CV_8UC3);for(int i=0;i<bins;i++){float bin_val = hist.at<float>(i); int intensity = cvRound(bin_val*hist_height/max_val);  //要繪製的高度rectangle(hist_img,Point(i*scale,hist_height-1),Point((i+1)*scale - 1, hist_height - intensity),CV_RGB(255,255,255));}imshow( "Source", src );imshow( "Gray Histogram", hist_img );waitKey(10000000000);return 0;}

實驗結果:繪製RGB三色長條圖

//繪製RGB三色分量長條圖int main(  ){Mat src;src=imread("baboon.jpg");int bins = 256;int hist_size[] = {bins};float range[] = { 0, 256 };const float* ranges[] = { range};MatND hist_r,hist_g,hist_b;int channels_r[] = {0};calcHist( &src, 1, channels_r, Mat(), // do not use maskhist_r, 1, hist_size, ranges,true, // the histogram is uniformfalse );int channels_g[] = {1};calcHist( &src, 1, channels_g, Mat(), // do not use maskhist_g, 1, hist_size, ranges,true, // the histogram is uniformfalse );int channels_b[] = {2};calcHist( &src, 1, channels_b, Mat(), // do not use maskhist_b, 1, hist_size, ranges,true, // the histogram is uniformfalse );double max_val_r,max_val_g,max_val_b;minMaxLoc(hist_r, 0, &max_val_r, 0, 0);minMaxLoc(hist_g, 0, &max_val_g, 0, 0);minMaxLoc(hist_b, 0, &max_val_b, 0, 0);int scale = 1;int hist_height=256;Mat hist_img = Mat::zeros(hist_height,bins*3, CV_8UC3);for(int i=0;i<bins;i++){float bin_val_r = hist_r.at<float>(i); float bin_val_g = hist_g.at<float>(i);float bin_val_b = hist_b.at<float>(i);int intensity_r = cvRound(bin_val_r*hist_height/max_val_r);  //要繪製的高度int intensity_g = cvRound(bin_val_g*hist_height/max_val_g);  //要繪製的高度int intensity_b = cvRound(bin_val_b*hist_height/max_val_b);  //要繪製的高度rectangle(hist_img,Point(i*scale,hist_height-1),Point((i+1)*scale - 1, hist_height - intensity_r),CV_RGB(255,0,0));rectangle(hist_img,Point((i+bins)*scale,hist_height-1),Point((i+bins+1)*scale - 1, hist_height - intensity_g),CV_RGB(0,255,0));rectangle(hist_img,Point((i+bins*2)*scale,hist_height-1),Point((i+bins*2+1)*scale - 1, hist_height - intensity_b),CV_RGB(0,0,255));}imshow( "Source", src );imshow( "RGB Histogram", hist_img );waitKey(10000000000);return 0;}

實驗結果:繪製二維長條圖

//繪製H-S二維長條圖int main( ){Mat src,hsv;src=imread("baboon.jpg");cvtColor(src, hsv, CV_BGR2HSV);// Quantize the hue to 30 levels// and the saturation to 32 levelsint hbins = 256, sbins = 180;int histSize[] = {hbins, sbins};// hue varies from 0 to 179, see cvtColorfloat 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 channelsint channels[] = {0, 1};calcHist( &hsv, 1, channels, Mat(), // do not use maskhist, 2, histSize, ranges,true, // the histogram is uniformfalse );double maxVal=0;minMaxLoc(hist, 0, &maxVal, 0, 0);int scale = 2;Mat histImg = Mat::zeros(sbins*scale, hbins*scale, 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(10000000000);return 0;}

實驗結果:(轉載請註明作者和出處:http://blog.csdn.net/xiaowei_cqu 未經允許請勿用於商業用途)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.