基于opencv的CLAHE(2)

//这是基于一个HSV的图像增强;效果虽然差些,但是也以用
IplImage* oCLAHE(IplImage* HSV){


	//RGB->HSV
	/*IplImage* HSV = cvCreateImage( cvGetSize(img), img->depth, 3);	
	cvCvtColor(img,  HSV, CV_RGB2HSV);*/
	IplImage* img=cvCreateImage( cvGetSize(HSV), HSV->depth, 3);
	cvCopyImage(HSV,img);


	IplImage* h = cvCreateImage( cvGetSize(img), img->depth, 1);	
	IplImage* s = cvCreateImage( cvGetSize(img), img->depth, 1);	
	IplImage* v = cvCreateImage( cvGetSize(img), img->depth, 1);	
	cvCvtPixToPlane(HSV,h,s,v,NULL); 
   


    /**---------------------------clahe----------------------------**/
	//get the hist
	int N=(img->width) * (img->height);		
	int hist[ 256 ]={0};
	double cdf[ 256 ]={0.0};
	double clippedHist[ 256 ]={0.0};


    //get the hist of entire image
	for(int x=0;xwidth;x++){
		for(int y=0;yheight;y++){
			CvScalar c = cvGet2D(v,y,x);
			int pixel=(int)c.val[0];
			++hist[pixel];
			
		}
	}


	//Calculate the histogram of the input image
	for(int i=0;i<256;i++){
		double value=(double)hist[i];
		clippedHist[i]=value/N;	
	}


	//Calculate the cumulative density function of the histogram
	for(int i=0;i<256;i++){
		if(clippedHist[i]!=0.0)
			for(int j=0;j<=i;j++)			
				cdf[i]+=clippedHist[j];			
	}   


    //Loop through the n pixels in the entire image and replace the value at each i'th point
	for(int x=0;xwidth;x++){
		for(int y=0;yheight;y++){
			CvScalar c = cvGet2D(v,y,x);
			int pixel=(int)c.val[0];
			pixel=255*80000*((cdf[pixel]-cdf[0])/(N-cdf[0]));			
			cvSetReal2D(v,y,x,pixel);
		}
	}


	cvMerge(h,s,v,NULL,HSV);
	IplImage* img2 = cvCreateImage( cvGetSize(img), img->depth, 3);	
	cvCvtColor( HSV,img2, CV_HSV2RGB);
	


	cvReleaseImage(&h);
	cvReleaseImage(&s);
	cvReleaseImage(&v);
	cvReleaseImage(&HSV);
	//cvReleaseImage(&img);


	return img2;
}


你可能感兴趣的:(opencv,null,function,image,input,each,c)