灰度变换原理
图像灰度变换变换原理:通过变换函数T将原图像像素灰度值r映射为灰度值s:
2、灰度反转
2.1原理
灰度反转:将图像亮暗对调,可以增强图像中暗色区域细节
�=�(�)=�−1−�
其中L为图像灰度级,0~255灰度图像的灰度级为256.
2.2 c++ opencv代码
#include<iostream>
#include<opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{ Mat image1, output_image, image1_gray; //定义输入图像,输出图像,灰度图像image1 = imread("lena.png"); //读取图像;if (image1.empty()){cout << "读取错误" << endl;return -1;}cvtColor(image1, image1_gray, COLOR_BGR2GRAY); //灰度化imshow(" image1_gray", image1_gray); //显示灰度图像output_image = image1_gray.clone();for (int i = 0; i < image1_gray.rows; i++){for (int j = 0; j < image1_gray.cols; j++){output_image.at<uchar>(i, j) = 255 - image1_gray.at<uchar>(i, j); //灰度反转}}imshow("output_image", output_image); //显示反转图像waitKey(0); //暂停,保持图像显示,等待按键结束return 0;
}
结果:
3、对数变换
3.1原理
对数变换:扩展图像中的暗像素值,压缩高灰度值。
�=�(�)=�∗���(1+�)
3.2 c++ opencv
#include<iostream>
#include<opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{ Mat image1, output_image, image1_gray; //定义输入图像,输出图像,灰度图像image1 = imread("lena.png"); //读取图像;if (image1.empty()){cout << "读取错误" << endl;return -1;}cvtColor(image1, image1_gray, COLOR_BGR2GRAY); //灰度化imshow(" image1_gray", image1_gray); //显示灰度图像output_image = image1_gray.clone();for (int i = 0; i < image1_gray.rows; i++){for (int j = 0; j < image1_gray.cols; j++){output_image.at<uchar>(i, j) =6*log((double)(image1_gray.at<uchar>(i, j))+1); //对数变换 s=6*log(r+1)}}normalize(output_image, output_image, 0, 255, NORM_MINMAX); //图像归一化,转到0~255范围内convertScaleAbs(output_image, output_image); //数据类型转换到CV_8Uimshow(" output_image", output_image); //显示变换图像waitKey(0); //暂停,保持图像显示,等待按键结束return 0;
}
结果:
4、幂律(伽马变换)
4.1原理
幂律变换与对数变换类似:
4.2 c++ opencv代码
#include<iostream>
#include<opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{ Mat image1, output_image, image1_gray; //定义输入图像,输出图像,灰度图像image1 = imread("lena.png"); //读取图像;if (image1.empty()){cout << "读取错误" << endl;return -1;}cvtColor(image1, image1_gray, COLOR_BGR2GRAY); //灰度化imshow(" image1_gray", image1_gray); //显示灰度图像output_image = image1_gray.clone();for (int i = 0; i < image1_gray.rows; i++){for (int j = 0; j < image1_gray.cols; j++){output_image.at<uchar>(i, j) =6*pow((double)image1_gray.at<uchar>(i, j),0.5); //幂律变换 s=6*r^0.5}}normalize(output_image, output_image, 0, 255, NORM_MINMAX); //图像归一化,转到0~255范围内convertScaleAbs(output_image, output_image); //数据类型转换到CV_8Uimshow(" output_image", output_image); //显示变换图像waitKey(0); //暂停,保持图像显示,等待按键结束return 0;
}
结果: