图像灰度变换

Content

  • 灰度图像二值化处理

    具体内容:设置并调整阈值对图像进行二值化处理。

  • 灰度图像的对数变换

    具体内容:设置并调整 r 值对图像进行对数变换。

  • 灰度图像的伽马变换

    具体内容:设置并调整γ值对图像进行伽马变换。

  • 彩色图像的补色变换

    具体内容:对彩色图像进行补色变换。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;



// 灰度图像二值化
Mat thresholdImage(const Mat& inputImage, int thresholdValue) {
Mat grayImage, binaryImage;
cvtColor(inputImage, grayImage, COLOR_BGR2GRAY); // 转换为灰度图像
threshold(grayImage, binaryImage, thresholdValue, 255, THRESH_BINARY); // 二值化
return binaryImage;
}

Mat thresholdImage2(const Mat& inputImage, int thresholdValue) {
Mat grayImage;
cvtColor(inputImage, grayImage, COLOR_BGR2GRAY); // 转换为灰度图像
Mat binaryImage = grayImage.clone();
for (int i = 0; i < grayImage.rows; i++) {
for (int j = 0; j < grayImage.cols; j++) {
if (grayImage.at<uchar>(i, j) > thresholdValue) {
binaryImage.at<uchar>(i, j) = 255;
}
else {
binaryImage.at<uchar>(i, j) = 0;
}
}
}
return binaryImage;
}

// 灰度图像对数变换
Mat logTransform(const Mat& inputImage, double c)
{
Mat resultImage = inputImage.clone();
resultImage.convertTo(resultImage, CV_32F); //转换为32位浮点型
resultImage = resultImage + 1;
log(resultImage, resultImage);
resultImage = c * resultImage;
normalize(resultImage, resultImage, 0, 255, NORM_MINMAX); //归一化到0~255
resultImage.convertTo(resultImage, CV_8U); //转换为8位无符号整型
return resultImage;
}

// 灰度图像伽马变换
Mat gammaTransform(const Mat& inputImage, double gamma, double c)
{
Mat resultImage = inputImage.clone();
resultImage.convertTo(resultImage, CV_32F); //转换为32位浮点型
pow(resultImage, gamma, resultImage);
resultImage = c * resultImage;
normalize(resultImage, resultImage, 0, 255, NORM_MINMAX); //归一化到0~255
resultImage.convertTo(resultImage, CV_8U); //转换为8位无符号整型
return resultImage;
}

// 灰度图像反色变换
Mat colorComplementTransform(const Mat& inputImage)
{
Mat resultImage = inputImage.clone();
resultImage = Scalar(255,255,255) - resultImage;
return resultImage;
}

int main()
{

Mat myImage1 = imread("test.jpg"); //载入图像到test
//imshow("initialImage", myImage1);

Mat grayImage1;
cvtColor(myImage1, grayImage1, COLOR_BGR2GRAY); // 转换为灰度图像
imshow("grayImage", grayImage1);
imwrite("grayImage.jpg", grayImage1);

int thresholdValue = 128;
Mat binaryImage1 = thresholdImage2(myImage1, thresholdValue); // 二值化
imshow("binaryImage",binaryImage1);
imwrite("binaryImage.jpg", binaryImage1);

double logC = 1;
Mat logImage1 = logTransform(grayImage1, logC); //对数变换
imshow("logImage", logImage1);
imwrite("logImage.jpg", logImage1);

double gamma = 2;
double gammaC = 1;
Mat gammaImage1 = gammaTransform(grayImage1, gamma, gammaC); //伽马变换
imshow("gammaImage", gammaImage1);
imwrite("gammaImage.jpg", gammaImage1);

Mat colorComplementImage1 = colorComplementTransform(myImage1); //反色变换
imshow("colorComplementImage", colorComplementImage1);
imwrite("colorComplementImage.jpg", colorComplementImage1);

waitKey(0);
return 0;
}

Output

彩图 灰图
test grayImage
灰度图像二值化处理 灰度图像的对数变换
binaryImage logImage
灰度图像的伽马变换 **彩色图像的补色变换 **
gammaImage colorComplementImage