色偏矫正-灰平衡

int makeavg(uchar* pData, int heihgt, int width, int stride, int* avg)
{
for (int i = 0; i < heihgt; ++i)
{
uchar* pc = pData + i * stride;
for (int j = 0; j < width; ++j)
{
avg[0] += pc[j * 3];
avg[1] += pc[j * 3 + 1];
avg[2] += pc[j * 3 + 2];
}
}
avg[0] /= (width*heihgt);
avg[1] /= (width*heihgt);
avg[2] /= (width*heihgt);


return 0;
}


void graybanlance(IplImage* img)
{
int avg[3] = {0};
makeavg((uchar*)img->imageData, img->height, img->width, img->widthStep,  avg);


int x = (avg[0] + avg[1] + avg[2])/3;
float bscale = x*1.0/avg[0];
float gscale = x*1.0/avg[1];
float rscale = x*1.0/avg[2];


for (int i = 0; i < img->height; ++i)
{
uchar* pp = (uchar*)img->imageData + i * img->widthStep;
for (int j = 0; j < img->width; ++j)
{
pp[3*j] = (pp[3*j]*bscale) > 255 ? 255 : (pp[3*j]*bscale) ;
pp[3*j+1] = (pp[3*j+1] * gscale) > 255 ? 255 : (pp[3*j+1] * gscale);
pp[3*j+2] = (pp[3*j+2] *rscale) > 255 ? 255 : (pp[3*j+2] *rscale);
}
}
}

你可能感兴趣的:(色偏矫正-灰平衡)