OpenCV直方圖均衡的測試,作一下備份
// histtest.cpp : 定義控制台應用程序的入口點。
//
#include "stdafx.h"
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
int HistogramBins = 256;
float HistogramRange1[2]={0,255};
float *HistogramRange[1]={&HistogramRange1[0]};
float CumulativeNumber;
float CDFArray[256];
uchar LookupTableData[256];
void DrawHistogramImage(CvHistogram *Histogram,IplImage *HistogramImage,int HistogramBins);
int main()
{
IplImage *Image1;
IplImage *Image2;
CvHistogram *Histogram1;
CvHistogram *Histogram2;
CvMat *LookupTableMatrix;
IplImage *GrayHistogramImage;
IplImage *EqualizeHistogramImage;
Image1=cvLoadImage("2.png",0);
Image2=cvCreateImage(cvGetSize(Image1),IPL_DEPTH_8U,1);
Histogram1 = cvCreateHist(1,&HistogramBins,CV_HIST_ARRAY,HistogramRange);
Histogram2 = cvCreateHist(1,&HistogramBins,CV_HIST_ARRAY,HistogramRange);
LookupTableMatrix = cvCreateMat(1,256,CV_8UC1);
GrayHistogramImage=cvCreateImage(cvSize(256,250),IPL_DEPTH_8U,3);
EqualizeHistogramImage=cvCreateImage(cvSize(256,250),IPL_DEPTH_8U,3);
GrayHistogramImage->origin=1;
EqualizeHistogramImage->origin=1;
cvCalcHist(&Image1,Histogram1);
DrawHistogramImage(Histogram1,GrayHistogramImage,HistogramBins);
//Probability Density Function (PDF)
cvNormalizeHist(Histogram1,1);
//End
//Cumulative Distribution Function (CDF)
CumulativeNumber=0;
for(int i=0;i<HistogramBins;i++)
{
CumulativeNumber=CumulativeNumber+cvQueryHistValue_1D(Histogram1,i);
CDFArray[i]=CumulativeNumber;
}
//End
//Make Look-up Table
printf("Look-up Table Number:\n");
for(int i=0;i<HistogramBins;i++)
{
LookupTableData[i]=(uchar)(255*CDFArray[i]);
printf("%f\n",(255*CDFArray[i]));
}
//End
cvSetData(LookupTableMatrix,LookupTableData,CV_AUTOSTEP);
cvLUT(Image1,Image2,LookupTableMatrix);
cvCalcHist(&Image2,Histogram2);
DrawHistogramImage(Histogram2,EqualizeHistogramImage,HistogramBins);
cvNamedWindow("Orig",1);
cvNamedWindow("Equalized",1);
cvNamedWindow("Gray Histogram",1);
cvNamedWindow("Equalize Histogram",1);
cvShowImage("Orig",Image1);
cvShowImage("Equalized",Image2);
cvShowImage("Gray Histogram",GrayHistogramImage);
cvShowImage("Equalize Histogram",EqualizeHistogramImage);
cvWaitKey(0);
}
void DrawHistogramImage(CvHistogram *Histogram,IplImage *HistogramImage,int HistogramBins)
{
CvPoint Point1;
CvPoint Point2;
for(int i=0;i<HistogramBins;i++)
{
Point1=cvPoint(i,(int)(cvQueryHistValue_1D(Histogram,i)/20));
Point2=cvPoint(i,0);
cvLine(HistogramImage,Point1,Point2,CV_RGB(127,127,127));
}
}
--------------------------------------分割線 --------------------------------------
Ubuntu Linux下安裝OpenCV2.4.1所需包 http://www.linuxidc.com/Linux/2012-08/68184.htm
Ubuntu 12.04 安裝 OpenCV2.4.2 http://www.linuxidc.com/Linux/2012-09/70158.htm
CentOS下OpenCV無法讀取視頻文件 http://www.linuxidc.com/Linux/2011-07/39295.htm
Ubuntu 12.04下安裝OpenCV 2.4.5總結 http://www.linuxidc.com/Linux/2013-06/86704.htm
Ubuntu 10.04中安裝OpenCv2.1九步曲 http://www.linuxidc.com/Linux/2010-09/28678.htm
基於QT和OpenCV的人臉識別系統 http://www.linuxidc.com/Linux/2011-11/47806.htm
--------------------------------------分割線 --------------------------------------
OpenCV的詳細介紹:請點這裡
OpenCV的下載地址:請點這裡