|||
IplImage* NormalizeImage(const IplImage *img)
{
if (!CV_IS_IMAGE(img) || !(IPL_DEPTH_8U ==img->depth) || !(3 == img->nChannels))
{
return NULL;
}
IplImage* imgavg = cvCreateImage(cvGetSize(img), img->depth, img->nChannels);
if (!CV_IS_IMAGE(imgavg) )
{
return NULL;
}
int width = img->width;
int height = img->height;
int redValue, greenValue, blueValue;
double sum, epslon = 0.000001;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
redValue = ((uchar *)(img->imageData + y*img->widthStep))[x*img->nChannels + 2];
greenValue = ((uchar *)(img->imageData + y*img->widthStep))[x*img->nChannels + 1];
blueValue = ((uchar *)(img->imageData + y*img->widthStep))[x*img->nChannels + 0];
sum = redValue + greenValue + blueValue + epslon;
((uchar *)(imgavg->imageData + y*imgavg->widthStep))[x*imgavg->nChannels + 2] = redValue / sum * 255;
((uchar *)(imgavg->imageData + y*imgavg->widthStep))[x*imgavg->nChannels + 1] = greenValue / sum * 255;
((uchar *)(imgavg->imageData + y*imgavg->widthStep))[x*imgavg->nChannels + 0] = blueValue / sum * 255;
}
}
return imgavg;
}
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-12-23 18:29
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社