一、 反相
1.1 公式:灰度级范围为[0, L-1]的一幅图像的反相图像, s=L-1-r
1.2 代码
#include <iostream>
#include "pch.h"
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
/* Inverse 反相 */
void inverse(Mat& img) {
for (int i = 0; i < img.rows; i++) {
for (int j = 0; j < img.cols; j++) {
img.at<Vec3b>(i, j)[0] = 255 - img.at<Vec3b>(i, j)[0];
img.at<Vec3b>(i, j)[1] = 255 - img.at<Vec3b>(i, j)[1];
img.at<Vec3b>(i, j)[2] = 255 - img.at<Vec3b>(i, j)[2];
}
}
}
int main()
{
Mat img = imread("x.jpg");
// 原图
imshow("Original", img);
// 反相处理
inverse(img);
imshow("Inversed", img);
waitKey();
return 0;
}
1.3 效果
二、对数变换
2.1 公式:s=clog(r+1)
2.2 代码
#include <iostream>
#include "pch.h"
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/types_c.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
/* Logarithmic transformation 对数变换 */
Mat logTransform_Gray(Mat& img,double c) {
Mat dst(img.rows, img.cols, CV_8UC1, Scalar(0));
double pixels[256] = { 0 };
if (img.type() == CV_8UC3)
cvtColor(img, img, CV_BGR2GRAY);
img.copyTo(dst);
for (int i = 0; i < 256; i++)
pixels[i] = log(i + 1);
for (int i = 0; i < dst.rows; i++)
for (int j = 0; j < dst.cols; j++)
dst.at<uchar>(i, j) = pixels[dst.at<uchar>(i, j)];
normalize(dst, dst, 0, 255, CV_MINMAX);
return dst;
}
Mat logTransform_RGB(Mat& img, double c) {
Mat temp, dst;
img.copyTo(temp);
if (temp.type() == CV_8UC1)
cvtColor(temp, temp, CV_GRAY2BGR);
Mat matArray[3];
split(temp, matArray);
for (int i = 0; i < 3; i++)
matArray[i] = logTransform_Gray(matArray[i], c);
merge(matArray, 3, dst);
return dst;
}
int main() {
Mat src, dst;
src = imread("./TestImages/Fig0305(a)(DFT_no_log).tif");
if (!src.data)
return -1;
dst = logTransform_RGB(src,1);
imshow("Raw", src);
imshow("LogTransform", dst);
waitKey();
return 0;
}
2.3 效果
三、伽马变换
3.1 公式:s=cr^γ
3.2 代码
#include <iostream>
#include "pch.h"
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/types_c.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
/* Gamma Correction */
Mat gammaCorrection_Gray(Mat& img, double c, double gamma) {
Mat dst(img.rows, img.cols, CV_8UC1, Scalar(0));
double pixels[256] = { 0 };
if (img.type() == CV_8UC3)
cvtColor(img, img, CV_BGR2GRAY);
img.copyTo(dst);
for (int i = 0; i < 256; i++) {
pixels[i] = c * pow(((double)i / (double)255.0), gamma) * 255;
if (pixels[i] > 255)
pixels[i] = 255;
else if (pixels[i] < 0)
pixels[i] = 0;
}
for (int i = 0; i < dst.rows; i++)
for (int j = 0; j < dst.cols; j++)
dst.at<uchar>(i, j) = pixels[dst.at<uchar>(i, j)];
normalize(dst, dst, 0, 255, CV_MINMAX);
return dst;
}
Mat gammaCorrection_RGB(Mat& img, double c, double gamma) {
Mat temp, dst;
img.copyTo(temp);
if (temp.type() == CV_8UC1)
cvtColor(temp, temp, CV_GRAY2BGR);
Mat matArray[3];
split(temp, matArray);
for (int i = 0; i < 3; i++)
matArray[i] = gammaCorrection_Gray(matArray[i], c, gamma);
merge(matArray, 3, dst);
return dst;
}
int main()
{
Mat src, dst, t, a;
src = imread("./TestImages/Fig0308(a)(fractured_spine).tif");
if (!src.data)
return -1;
dst = gammaCorrection_RGB(src, 1, 0.6);
t = gammaCorrection_RGB(src, 1, 0.4);
a = gammaCorrection_RGB(src, 1, 0.3);
imshow("Raw", src);
imshow("c=1, gamma=0.6", dst);
imshow("c=1, gamma=0.4", t);
imshow("c=1, gamma=0.3", a);
waitKey();
return 0;
}
3.3 效果