#include <opencv2/opencv.hpp>
#include "cv.h" int main() { IplImage *src=cvLoadImage("e:\\kankan\\baihe.jpg"); IplImage *r=cvCreateImage(cvGetSize(src), 8, 1); IplImage *b=cvCreateImage(cvGetSize(src), 8, 1); IplImage *g=cvCreateImage(cvGetSize(src), 8, 1); cvSplit(src,b, g, r, NULL); IplImage *gray=cvCreateImage(cvGetSize(src),8,1); cvCvtColor(src,gray,CV_BGR2GRAY); int size=256; float range[]={0,255}; float *ranges[]={range}; CvHistogram *r_hist=cvCreateHist(1, &size, CV_HIST_ARRAY, ranges,1); CvHistogram *g_hist=cvCreateHist(1, &size, CV_HIST_ARRAY, ranges,1); CvHistogram *b_hist=cvCreateHist(1, &size, CV_HIST_ARRAY, ranges,1); CvHistogram *hist=cvCreateHist(1, &size, CV_HIST_ARRAY, ranges,1); cvCalcHist(&r, r_hist, 0, NULL); IplImage *r_dst=cvCreateImage(cvSize(400,300),8,3); cvSet(r_dst,cvScalarAll(255),0); float r_max=0; cvGetMinMaxHistValue(r_hist, NULL, &r_max, NULL, NULL); double r_bin_width=(double)r_dst->width/size; double r_bin_unith=(double)r_dst->height/r_max; for (int i=0; i<size; i++) { CvPoint p0=cvPoint(i*r_bin_width,r_dst->height); CvPoint p1=cvPoint((i+1)*r_bin_width,r_dst->height-cvGetReal1D(r_hist->bins,i)*r_bin_unith); cvRectangle(r_dst, p0, p1, cvScalar(255,0,0),-1,8,0); } cvCalcHist(&g, g_hist, 0, NULL); IplImage* g_dst=cvCreateImage(cvSize(400,300),8,3); cvSet(g_dst,cvScalarAll(255),0); float g_max=0; cvGetMinMaxHistValue(g_hist,NULL,&g_max,NULL,NULL); double g_bin_width=(double)g_dst->width/size; double g_bin_unith=(double)g_dst->height/g_max; for(int i=0;i<size;i++) { CvPoint p0=cvPoint(i*g_bin_width,g_dst->height); CvPoint p1=cvPoint((i+1)*g_bin_width,g_dst->height-cvGetReal1D(g_hist->bins,i)*g_bin_unith); cvRectangle(g_dst,p0,p1,cvScalar(0,255,0),-1,8,0); } cvCalcHist(&b,b_hist,0,NULL); IplImage* b_dst=cvCreateImage(cvSize(400,300),8,3); cvSet(b_dst,cvScalarAll(255),0); float b_max=0; cvGetMinMaxHistValue(b_hist,NULL,&b_max,NULL,NULL); double b_bin_width=(double)b_dst->width/size; double b_bin_unith=(double)b_dst->height/b_max; for(int i=0;i<size;i++) { CvPoint p0=cvPoint(i*b_bin_width,b_dst->height); CvPoint p1=cvPoint((i+1)*b_bin_width,b_dst->height-cvGetReal1D(b_hist->bins,i)*b_bin_unith); cvRectangle(b_dst,p0,p1,cvScalar(0,0,255),-1,8,0); } cvCalcHist(&gray,hist,0,NULL); IplImage* gray_dst=cvCreateImage(cvSize(400,300),8,3); cvSet(gray_dst,cvScalarAll(255),0); float max=0; cvGetMinMaxHistValue(hist,NULL,&max,NULL,NULL); double bin_width=(double)gray_dst->width/size; double bin_unith=(double)gray_dst->height/max; for(int i=0;i<size;i++) { CvPoint p0=cvPoint(i*bin_width,gray_dst->height); CvPoint p1=cvPoint((i+1)*bin_width,gray_dst->height-cvGetReal1D(hist->bins,i)*bin_unith); cvRectangle(gray_dst,p0,p1,cvScalar(0),-1,8,0); } cvNamedWindow("r"); cvShowImage("r",r_dst); cvNamedWindow("g"); cvShowImage("g",g_dst); cvNamedWindow("b"); cvShowImage("b",b_dst); cvNamedWindow("gray"); cvShowImage("gray",gray_dst); cvReleaseImage(&r_dst); cvReleaseImage(&g_dst); cvReleaseImage(&b_dst); cvReleaseImage(&gray_dst); cvWaitKey(0); return 0; }