opencv——灰度直方图的绘制

#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;
}
这程序不狗血!!!!!
但求大神能指点下,为什么调用函数不能在主函数之后!!!!!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值