直方图均衡化

#include "highgui.h" #include "cv.h" #include <iostream> #include <iomanip> using namespace std; #define HDIM 256 //直方图的维数 int main() { IplImage* src = cvLoadImage("lena.jpg",0); IplImage* dst = NULL; CvHistogram* hist =0; int n = HDIM; double nn[HDIM]; uchar T[HDIM]; CvMat *T_mat; int x; int sum;//图像中像素总和 double val = 0; cvNamedWindow("source",1); cvNamedWindow("result",1); //计算直方图 hist = cvCreateHist(1,&n,CV_HIST_ARRAY,0,1); cvCalcHist(&src,hist,0,0); val = 0; for (x=0;x<n;x++) { val = val + cvGetReal1D(hist->bins,x); nn[x] = val; } //归一化直方图 sum = src->height*src->width; for (x=0;x<n;x++) { T[x] = (uchar)(255*nn[x]/sum); } dst = cvCloneImage(src); T_mat = cvCreateMatHeader(1,256,CV_8UC1); cvSetData(T_mat,T,0); //直接调用内部函数完成look-up-table的过程 cvLUT(src,dst,T_mat); cvShowImage("source",src); cvShowImage("result",dst); cvWaitKey(0); cvReleaseImage(&src); cvReleaseImage(&dst); cvDestroyAllWindows(); }  

你可能感兴趣的:(直方图均衡化)