//计算直方图,并用图片显示出来直方图结果图像; //原创crazyMu void DrawHis(IplImage *image1,IplImage *image2) { int size=256; float range[]={0,255}; float* ranges[]={range}; double binnum[256]={0}; CvHistogram* hist1=cvCreateHist(1,&size,CV_HIST_ARRAY,ranges,1); CvHistogram* hist2=cvCreateHist(1,&size,CV_HIST_ARRAY,ranges,1); cvCalcHist(&image1,hist1,0,NULL); cvCalcHist(&image2,hist2,0,NULL); float max1=0; float max2=0; cvGetMinMaxHistValue(hist1,NULL,&max1,NULL,NULL); cvGetMinMaxHistValue(hist2,NULL,&max2,NULL,NULL); IplImage* dst=cvCreateImage(cvSize(400,300),8,3); cvSet(dst,cvScalarAll(255),0); double bin_width=(double)dst->width/size; double bin_unith1=(double)dst->height/max1; double bin_unith2=(double)dst->height/max2; for(int i=0;i<size;i++) { CvPoint p0=cvPoint(i*bin_width,dst->height); CvPoint p1=cvPoint((i+1)*bin_width,dst->height-cvGetReal1D(hist1->bins,i)*bin_unith1); cvRectangle(dst,p0,p1,cvScalar(0,0,255),-1,8,0); //用红色显示直方图 CvPoint p2=cvPoint((i+1)*bin_width,dst->height-cvGetReal1D(hist2->bins,i)*bin_unith2); cvRectangle(dst,p0,p2,cvScalar(255,0,0),-1,8,0); //用绿色显示直方图 } cvShowImage("gray image histogram",dst); cvReleaseHist(&hist1); cvReleaseHist(&hist2); }