#include <opencv\cv.h> #include <opencv\highgui.h> #include <opencv\cxcore.h> #include <stdlib.h> #include <stdio.h> //绘制直方图 IplImage* DrawHistogram(CvHistogram* hist , float scaleX = 1 , float scaleY = 1) { //获取直方图中极大值 float histMax = 0; cvGetMinMaxHistValue(hist , 0 ,&histMax ,0 ,0); //创建图像 该图像用于显示直方图 IplImage* imgHist = cvCreateImage(cvSize(256*scaleX , 64*scaleY) , 8 ,1); //图像置零 cvZero(imgHist); //依次绘制直方图的bin for(int i=0;i<255;i++) { //获取直方图的值 float histValue = cvQueryHistValue_1D(hist , i); float nextValue = cvQueryHistValue_1D(hist , i+1); //获取四边形的四个点的坐标 详情请观看视频的讲解 CvPoint pt1 = cvPoint( i*scaleX , 64*scaleY); CvPoint pt2 = cvPoint( (i+1)*scaleX , 64*scaleY); CvPoint pt3 = cvPoint( (i+1)*scaleX , (64 - (nextValue/histMax)*64) *scaleY ); CvPoint pt4 = cvPoint ( i*scaleX , (64 - (histValue/histMax)*64) *scaleY ); int numPts = 5; CvPoint pts[5]; pts[0] = pt1; pts[1] = pt2; pts[2] = pt3; pts[3] = pt4; pts[4] = pt1; //填充四边形 cvFillConvexPoly(imgHist , pts ,numPts , cvScalar(255)); } return imgHist; } int main(int argc, char* argv[]) { //加载图像 IplImage* src = cvLoadImage("D:\\1.jpg"); cvNamedWindow("sr"); cvShowImage("sr" , src); int dims = 1; // 一维直方图 int size =256; //bin的个数 每一位的直放个数 float range[] = {0,255}; //取值范围 float* ranges[] = {range}; CvHistogram* hist; //创建直方图 hist = cvCreateHist(dims , &size , CV_HIST_ARRAY , ranges , 1 ); //清空直方图 cvClearHist(hist); //给B G R 三个通道的图像分配空间 IplImage* imgRed = cvCreateImage(cvGetSize(src) , 8 ,1); IplImage* imgGreen = cvCreateImage(cvGetSize(src) , 8 ,1); IplImage* imgBlue = cvCreateImage(cvGetSize(src) , 8 ,1); //将图像src 分解成B G R 三个通道 //cvSplit最后一个通道为NULL cvSplit(src , imgBlue , imgGreen , imgRed , NULL); //计算B通道 直方图 cvCalcHist(&imgBlue , hist , 0 , 0 ); //绘制B通道直方图 结果保存在histBlue IplImage* histBlue = DrawHistogram(hist); //将B通道的直方图数据清空 cvClearHist(hist); //计算G通道直方图 cvCalcHist(&imgGreen , hist , 0 , 0 ); //绘制G通道直方图 histGreen IplImage* histGreen = DrawHistogram(hist); //将G通道的直方图数据清空 cvClearHist(hist); //计算R通道 直方图 cvCalcHist(&imgRed , hist , 0 , 0 ); //绘制R通道直方图 histRed IplImage* histRed = DrawHistogram(hist); //将R通道的直方图数据清空 cvClearHist(hist); cvNamedWindow("B"); cvNamedWindow("G"); cvNamedWindow("R"); cvShowImage("B" , histBlue); cvShowImage("G" , histGreen); cvShowImage("R" , histRed); cvWaitKey(0); //释放资源 //(1) 释放源图像 cvReleaseImage(&src); //(2) 释放三个通道的图像 cvReleaseImage(&imgRed); cvReleaseImage(&imgGreen); cvReleaseImage(&imgBlue); //(3)释放三个通道直方图 cvReleaseImage(&histBlue); cvReleaseImage(&histGreen); cvReleaseImage(&histRed); //(4)释放直方图空间 cvReleaseHist(&hist); return 0; }