#include <cv.h> #include <highgui.h> #pragma comment( lib, "cv.lib" ) #pragma comment( lib, "cxcore.lib" ) #pragma comment( lib, "highgui.lib" ) int main() { IplImage* src=cvLoadImage("lena.jpg",0); int width=src->width; int height=src->height; int step=src->widthStep; uchar* data=(uchar *)src->imageData; int hist[256]={0}; for(int i=0;i<height;i++) { for(int j=0;j<width;j++) { hist[data[i*step+j]]++; } } int max=0; for(i=0;i<256;i++) { if(hist[i]>max) { max=hist[i]; } } IplImage* dst=cvCreateImage(cvSize(400,300),8,3); cvSet(dst,cvScalarAll(255),0); double bin_width=(double)dst->width/256; double bin_unith=(double)dst->height/max; for(i=0;i<256;i++) { CvPoint p0=cvPoint(i*bin_width,dst->height); CvPoint p1=cvPoint((i+1)*bin_width,dst->height-hist[i]*bin_unith); cvRectangle(dst,p0,p1,cvScalar(0,255),-1,8,0); } cvNamedWindow("src",1); cvShowImage("src",src); cvNamedWindow("dst",1); cvShowImage("dst",dst); cvWaitKey(0); cvDestroyAllWindows(); cvReleaseImage(&src); cvReleaseImage(&dst); return 0; }
代码之二