歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

OpenCV繪制灰度直方圖

代碼之一

  1. #include <cv.h>   
  2. #include <highgui.h>   
  3. #pragma comment( lib, "cv.lib" )   
  4. #pragma comment( lib, "cxcore.lib" )   
  5. #pragma comment( lib, "highgui.lib" )   
  6. int main()  
  7. {  
  8.          IplImage* src=cvLoadImage("lena.jpg",0);  
  9.          int width=src->width;  
  10.          int height=src->height;  
  11.          int step=src->widthStep;  
  12.          uchar* data=(uchar *)src->imageData;  
  13.          int hist[256]={0};  
  14.          for(int i=0;i<height;i++)  
  15.          {  
  16.                    for(int j=0;j<width;j++)  
  17.                    {  
  18.                             hist[data[i*step+j]]++;  
  19.                    }  
  20.          }  
  21.          int max=0;  
  22.          for(i=0;i<256;i++)  
  23.          {  
  24.                    if(hist[i]>max)  
  25.                    {  
  26.                             max=hist[i];  
  27.                    }  
  28.          }  
  29.          IplImage* dst=cvCreateImage(cvSize(400,300),8,3);  
  30.          cvSet(dst,cvScalarAll(255),0);  
  31.          double bin_width=(double)dst->width/256;  
  32.          double bin_unith=(double)dst->height/max;  
  33.          for(i=0;i<256;i++)  
  34.          {  
  35.                    CvPoint p0=cvPoint(i*bin_width,dst->height);  
  36.                    CvPoint p1=cvPoint((i+1)*bin_width,dst->height-hist[i]*bin_unith);  
  37.                    cvRectangle(dst,p0,p1,cvScalar(0,255),-1,8,0);  
  38.          }  
  39.          cvNamedWindow("src",1);  
  40.          cvShowImage("src",src);  
  41.          cvNamedWindow("dst",1);  
  42.          cvShowImage("dst",dst);  
  43.          cvWaitKey(0);  
  44.          cvDestroyAllWindows();  
  45.          cvReleaseImage(&src);  
  46.          cvReleaseImage(&dst);  
  47.          return 0;  
  48. }  
  1. 代碼之二  
  1. #include <cv.h>   
  2. #include <highgui.h>   
  3. #pragma comment( lib, "cv.lib" )   
  4. #pragma comment( lib, "cxcore.lib" )   
  5. #pragma comment( lib, "highgui.lib" )   
  6. int main()  
  7. {  
  8.          IplImage* src=cvLoadImage("lena.jpg",0);  
  9.          int size=256;  
  10.          float range[]={0,255};  
  11.          float* ranges[]={range};  
  12.          CvHistogram* hist=cvCreateHist(1,&size,CV_HIST_ARRAY,ranges,1);  
  13.          cvCalcHist(&src,hist,0,NULL);  
  14.          float max=0;  
  15.          cvGetMinMaxHistValue(hist,NULL,&max,NULL,NULL);  
  16.          IplImage* dst=cvCreateImage(cvSize(400,300),8,3);  
  17.          cvSet(dst,cvScalarAll(255),0);  
  18.          double bin_width=(double)dst->width/size;  
  19.          double bin_unith=(double)dst->height/max;  
  20.          for(int i=0;i<size;i++)  
  21.          {  
  22.                    CvPoint p0=cvPoint(i*bin_width,dst->height);  
  23.                    CvPoint p1=cvPoint((i+1)*bin_width,dst->height-cvGetReal1D(hist->bins,i)*bin_unith);  
  24.                    cvRectangle(dst,p0,p1,cvScalar(0,255),-1,8,0);  
  25.          }  
  26.          cvNamedWindow("src",1);  
  27.          cvShowImage("src",src);  
  28.          cvNamedWindow("dst",1);  
  29.          cvShowImage("dst",dst);  
  30.          cvWaitKey(0);  
  31.          cvDestroyAllWindows();  
  32.          cvReleaseImage(&src);  
  33.          cvReleaseImage(&dst);  
  34.          return 0;  
  35. }  
Copyright © Linux教程網 All Rights Reserved