openCV 直方图统计
直方图显示
1 #include <opencv2/opencv.hpp> 2 3 using namespace std; 4 using namespace cv; 5 6 int main(int argc, char* argv[]) 7 { 8 //声明IplImage指针 9 IplImage* pImg = NULL; 10 IplImage* pCannyImg = NULL; 11 12 int HistogramBins = 256; 13 float HistogramRange1[2]={0,255}; 14 float *HistogramRange[1]={&HistogramRange1[0]}; 15 16 //载入图像,强制转化为Gray 17 if( (pImg = cvLoadImage("D:\\Documents\\1.bmp", CV_LOAD_IMAGE_GRAYSCALE)) != NULL ) 18 { 19 IplImage *hist_img = cvCreateImage(cvSize(256, 300), IPL_DEPTH_8U, 1); 20 CvHistogram *Histogram1=cvCreateHist(1,&HistogramBins,CV_HIST_ARRAY,HistogramRange); 21 cvCalcHist(&pImg, Histogram1); 22 //画图 23 float max_value; 24 cvGetMinMaxHistValue(Histogram1, 0, &max_value, 0, 0); 25 int val = 0; 26 for (int i = 0 ; i < HistogramBins; ++i ) 27 { 28 val = int(cvGetReal1D(Histogram1->bins, i)/max_value * 300); 29 cvLine(hist_img, cvPoint(i, 300), cvPoint(i, 300 - val), cvScalar(255, 255, 255, 0), 1); 30 } 31 32 //为canny边缘图像申请空间 33 pCannyImg = cvCreateImage(cvGetSize(pImg), 34 IPL_DEPTH_8U, 35 1); 36 //canny边缘检测 37 cvCanny(pImg, pCannyImg, 90, 160, 3); 38 cvNamedWindow("canny",1); 39 cvNamedWindow("Image1",1); 40 cvNamedWindow("Histogram",1); 41 cvShowImage("Image1",pImg); 42 cvShowImage("Histogram", hist_img); 43 cvShowImage( "canny", pCannyImg ); 44 45 cvWaitKey(0); 46 cvReleaseImage(&pImg); 47 cvReleaseImage(&hist_img); 48 cvReleaseImage(&pCannyImg); 49 return 0; 50 } 51 52 return -1; 53 }
效果图
不经历风雨,怎么见彩虹!