伽马变换一种灰度变换,属于简单图像增强,也称幂律变换
先介绍两个函数
归一化
normalize(imageGamma, imageGamma, 0, 255, CV_MINMAX);
可实现图像增强等相关操作的快速运算,then by calculating an absolute value of the result.
计算绝对值。
cv::convertScaleAbs()用于实现对整个图像数组中的每一个元素进行输入输出操作
void cv::convertScaleAbs(
cv::InputArray src, // 输入数组
cv::OutputArray dst, // 输出数组
double alpha = 1.0, // 乘数因子
double beta = 0.0 // 偏移量
);
使用以上两步骤先把增强后的图片归一化后进行取绝对值,就得到了我们的效果
#include
#include
using namespace cv;
int main(int argc, char *argv[])
{
Mat image = imread("images/girl.jpg");
Mat imageGamma(image.size(), CV_32FC3);
Mat imageGamma1(image.size(), CV_32FC3);
Mat imageGamma2(image.size(), CV_32FC3);
for (int i = 0; i < image.rows; i++)
{
for (int j = 0; j < image.cols; j++)
{
imageGamma.at<Vec3f>(i, j)[0] = (image.at<Vec3b>(i, j)[0])*(image.at<Vec3b>(i, j)[0])*(image.at<Vec3b>(i, j)[0]);
imageGamma.at<Vec3f>(i, j)[1] = (image.at<Vec3b>(i, j)[1])*(image.at<Vec3b>(i, j)[1])*(image.at<Vec3b>(i, j)[1]);
imageGamma.at<Vec3f>(i, j)[2] = (image.at<Vec3b>(i, j)[2])*(image.at<Vec3b>(i, j)[2])*(image.at<Vec3b>(i, j)[2]);
imageGamma1.at<Vec3f>(i, j)[0] = (image.at<Vec3b>(i, j)[0])*(image.at<Vec3b>(i, j)[0]);//*(image.at(i, j)[0]+100);
imageGamma1.at<Vec3f>(i, j)[1] = (image.at<Vec3b>(i, j)[1])*(image.at<Vec3b>(i, j)[1]);// *(image.at(i, j)[1] + 100);
imageGamma1.at<Vec3f>(i, j)[2] = (image.at<Vec3b>(i, j)[2])*(image.at<Vec3b>(i, j)[2]);// *(image.at(i, j)[2] + 100);
imageGamma2.at<Vec3f>(i, j)[0] = (image.at<Vec3b>(i, j)[0])*255;//*(image.at(i, j)[0]+100);
imageGamma2.at<Vec3f>(i, j)[1] = (image.at<Vec3b>(i, j)[1])*255;// *(image.at(i, j)[1] + 100);
imageGamma2.at<Vec3f>(i, j)[2] = (image.at<Vec3b>(i, j)[2])*255;// *(image.at(i, j)[2] + 100);
imageGamma0.at<Vec3f>(i, j)[0] = (image.at<Vec3b>(i, j)[0])/20;//*(image.at(i, j)[0]+100);
imageGamma0.at<Vec3f>(i, j)[1] = (image.at<Vec3b>(i, j)[1])/20;// *(image.at(i, j)[1] + 100);
imageGamma0.at<Vec3f>(i, j)[2] = (image.at<Vec3b>(i, j)[2])/20;// *(image.at(i, j)[2] + 100);
}
}
//归一化到0~255
normalize(imageGamma, imageGamma, 0, 255, CV_MINMAX);
normalize(imageGamma1, imageGamma1, 0, 255, CV_MINMAX);
normalize(imageGamma2, imageGamma2, 0, 255, CV_MINMAX);//转换成8bit图像显示
convertScaleAbs(imageGamma, imageGamma);
convertScaleAbs(imageGamma1, imageGamma1);
convertScaleAbs(imageGamma2, imageGamma2);
imshow("原图", image);
imshow("伽马变换图像增强效果", imageGamma);
imshow("伽马变换图像增强效果1", imageGamma1);
imshow("伽马变换图像增强效果2", imageGamma2);
waitKey();
return 0;
}