#include <stdio.h>
#include <opencv2\opencv.hpp>
#include "cv.h"
#include "highgui.h"
#define _CRT_SECURE_NO_WARINGS
IplImage* Draw(CvHistogram* Hist,float sclarX=1,float sclarY=1)
{
//归一化参数
float HistMax = 0;
//获取最大值
cvGetMinMaxHistValue(Hist,0,&HistMax,0,0);
//保存直方图书记
IplImage* imgHist = cvCreateImage(cvSize(256*sclarX,64*sclarY),8,1);
cvZero(imgHist);
for(int i=0;i<255;i++)
{
float HistValue = cvQueryHistValue_1D(Hist , i);
float NextValue = cvQueryHistValue_1D(Hist , i+1);
CvPoint pt1 = cvPoint(i*sclarX,64*sclarY);
CvPoint pt2 = cvPoint((i+1)*sclarX,64*sclarY);
CvPoint pt3 = cvPoint((i+1)*sclarX,64*sclarY*(1-(NextValue/HistMax)));
CvPoint pt4 = cvPoint((i)*sclarX,64*sclarY*(1-(HistValue/HistMax)));
int numPt5 = 5;
CvPoint pts[5];
pts[0] = pt1;
pts[1] = pt2;
pts[2] = pt3;
pts[3] = pt4;
pts[4] = pt1;
cvFillConvexPoly(imgHist,pts,numPt5,cvScalar(255));
}
return imgHist;
}
int main( int argc, char* argv[])
{
IplImage* src = cvLoadImage("2.jpg");
//设置直方图参数
int dims = 1 ;
int size = 256 ;
float range[] = {0,255};
float *ranges[]= {range};
CvHistogram *Hist;
//创建直方图
Hist = cvCreateHist(dims,&size,CV_HIST_ARRAY,ranges,1);
cvClearHist(Hist);
//分解图像
IplImage *imgRed = cvCreateImage(cvGetSize(src),8,1);
IplImage *imgGreen = cvCreateImage(cvGetSize(src),8,1);
IplImage *imgBlue = cvCreateImage(cvGetSize(src),8,1);
cvSplit(src,imgBlue,imgGreen,imgBlue,NULL);
//自动计算图像直方图
cvCalcHist(&imgBlue,Hist,0,0);
IplImage* HistBlue =Draw(Hist);
cvClearHist(Hist);
cvCalcHist(&imgGreen,Hist,0,0);
IplImage* HistGreen =Draw(Hist);
cvClearHist(Hist);
cvCalcHist(&imgRed,Hist,0,0);
IplImage* HistRed =Draw(Hist);
cvClearHist(Hist);
cvNamedWindow("src");
cvNamedWindow("HistRed");
cvNamedWindow("HistGreen");
cvNamedWindow("HistBlue");
cvShowImage("src",src);
cvShowImage("HistRed",HistRed);
cvShowImage("HistGreen",HistGreen);
cvShowImage("HistBlue",HistBlue);
cvWaitKey(0);
cvReleaseImage(&src);
cvReleaseImage(&HistRed);
cvReleaseImage(&HistGreen);
cvReleaseImage(&HistBlue);
cvDestroyWindow("src");
cvDestroyWindow("HistRed");
cvDestroyWindow("HistGreen");
cvDestroyWindow("HistBlue");
cvReleaseHist(&Hist);
return 0;
}
这程序不狗血!!!!!
但求大神能指点下,为什么调用函数不能在主函数之后!!!!!