今天找到了个简单亮度校正功能的方法感觉效果还可以,使用于简单背景,代码如下
int _tmain(int argc, _TCHAR* argv[])
{
IplImage* src;
// 第一条命令行参数确定了图像的文件名。
char* filename = (char*)"C:\\Users\\chenchao\\Desktop\\结晶\\25.bmp";
if( (src = cvLoadImage( filename, 0)) == 0 )
return -1;
cvNamedWindow( "src", 1 );
cvShowImage( "src", src );
IplImage* mean=cvCreateImage(cvGetSize(src), 8, 1 );
IplImage* dst=cvCreateImage(cvGetSize(src), 8, 1 );
cvSmooth( src, mean, CV_MEDIAN , 101, 101, 0, 0 );
cvNamedWindow( "mean", 1 );
cvShowImage( "mean", mean );
int i,j;
int k;
double val,val_mean,val_dst;
float factor=0.7;
int step_src=src->widthStep/sizeof(uchar);
int step_mean=mean->widthStep/sizeof(uchar);
for(j=0;j<src->height;j++)
{
for(i=0;i<src->width;i++)
{
k=j*step_src+i;
val=((uchar*)(src->imageData))[k]; //获取源图像像素点数据
val_mean=((uchar*)(mean->imageData))[k];//获取平滑处理后的图像像素点
val_dst=(int)( (127-val_mean)*factor + val ); //计算目标像素值
//防止数据越界
if(val_dst<0)
val_dst=0;
//转换成整数复制
((uchar*)(dst->imageData))[k]=(int)val_dst;
}
}
cvNamedWindow( "dst", 1 );
cvShowImage( "dst", dst );
cvWaitKey(0);
cvSaveImage("C:\\Users\\chenchao\\Desktop\\1.bmp",dst);
cvReleaseImage(&src);
cvReleaseImage(&mean);
cvReleaseImage(&dst);
cvDestroyWindow("src");
cvDestroyWindow("mean");
cvDestroyWindow("dst");
return 0;
}