图像的γ校正

1.修改前的版本:
gamma校正完是否需要将图像归一化到0~255,为什么整幅图像的归一化结果都是0。
//gamma校正y,error!?!?!?
void gamma_adjust(IplImage* src, IplImage* dst, const double& gamma)
{
assert(src->depth == IPL_DEPTH_8U && src->nChannels == 1 && gamma > 0);
IplImage* temp = cvCreateImage(cvGetSize(src), IPL_DEPTH_64F, 1);
IplImage* temp2 = cvCloneImage(temp);
cvConvertScale(src, temp);
for (int y = 0; y != src->height; ++y)
{
for (int x = 0; x != src->width; ++x)
{
uchar* p = (uchar*)(src->imageData + y*src->widthStep + x);
double p2 = (double )*p;
double* q = (double *)(temp->imageData + y*temp->widthStep + x);
*q = pow(p2, gamma);
}
}
double max , min;
cvMinMaxLoc(temp, & min, &max );
cvConvertScale(temp, dst, 255.0/( max - min ), -255.0*min/( max - min ));
/*cvMinMaxLoc(temp2, &min, &max);*/
//do_normalize(temp2, temp2);
//do_normalize(temp, dst);
}

2.修改后的版本:
用指针访问矩阵元素的方法比较高效,但是并不安全,出错的原因可能就在此。所以,改为用宏定义访问每个像元CV_MAT_ELEM( matrix, elemtype, row, col )
//gamma校正y
void gamma_adjust(IplImage* src, IplImage* dst, const float& gamma)
{
assert(src->depth == IPL_DEPTH_8U && src->nChannels == 1 && gamma > 0);
CvMat* temp = cvCreateMat(src->height, src->width, CV_32FC1);
CvMat* temp2 = cvCreateMat(src->height, src->width, CV_32FC1);
cvConvertScale(src, temp);
for (int y = 0; y != src->height; ++y)
{
for (int x = 0; x != src->width; ++x)
{
float p = CV_MAT_ELEM(*temp, float , y, x);
*(( float*)CV_MAT_ELEM_PTR(*temp2, y, x)) = pow(p, gamma);
}
}
//double max, min;
//cvMinMaxLoc(temp2, &min, &max);
cvConvertScale(temp, dst, 255.0/(max - min), -255.0*min/(max - min));
cvMinMaxLoc(dst, &min, &max);
//cvConvertScale(temp2, dst, 255.0/(max - min), -255.0*min/(max - min));
do_normalize(temp2, dst);
cvReleaseMat(&temp);
cvReleaseMat(&temp2);
/*cvMinMaxLoc(temp2, &min, &max);*/
//do_normalize(temp2, temp2);
//do_normalize(temp, dst);

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值