1.灰度线性变换:F(x)=a*f(x)+b
int IMGChange(IplImage *src,IplImage *dest, float slope,float intercept)
{
if(NULL == src)
return -1;
if(src->nChannels!=1 || dest->nChannels!=1)
{
cout<<"It's not gray image!"<<endl;
return -1;
}
float gray = 0;
for(int i = 0;i < src->height;i++){
for(int j = 0; j < src->width; j++){
gray = (float)src->imageData[src->widthStep * i + j ];
gray = slope*gray + intercept;
dest->imageData[dest->widthStep * i + j ]=gray;
}
}
return 0;
}
当slope=-1 intercept=255即为图像反转
2.伽马变换,s=c*(r)γ,可用于图像增强等预处理。
int IMGgammaChange(IplImage *src,IplImage *dest, double gamma,double comp)
{
if(NULL == src)
return -1;
if(src->nChannels!=1 || dest->nChannels!=1)
{
cout<<"It's not gray image!"<<endl;
return -1;
}
double gray = 0;
for(int i = 0;i < src->height;i++){
for(int j = 0; j < src->width; j++){
gray = (double)src->imageData[src->widthStep * i + j ];
gray =pow((gray+comp)/255.0,gamma)*255;
dest->imageData[dest->widthStep * i + j ]=gray;
}
}
return 0;
}