OpenCV4入门系列教程81:强度变换

索引地址:系列教程索引地址

上一篇:OpenCV4入门系列教程80:自定义图像直方图

图像强度

英文名称是image intensity,意思是单通道图像像素的值大小。在灰度图像中,图像强度是就是图像的灰度级。在RGB颜色空间中,可以理解为RGB三个通道的像素灰度值,即RGB包含三种图像强度。其他颜色空间也是同样的道理。

测试代码:

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/intensity_transform.hpp"

#include <iostream>

using namespace std;
using namespace cv;
using namespace cv::intensity_transform;

namespace {
static std::string keys = "{ help h | | Print help message. }"
"{ input i | | Path to the input image. }";

// global variables
Mat g_image;

int g_gamma = 40;
const int g_gammaMax = 500;
Mat g_imgGamma;
const std::string g_gammaWinName = "Gamma Correction";

Mat g_contrastStretch;
int g_r1 = 70;
int g_s1 = 15;
int g_r2 = 120;
int g_s2 = 240;
const std::string g_contrastWinName = "Contrast Stretching";

Mat g_imgBIMEF;
int g_mu = 50;
const int g_muMax = 100;
const std::string g_BIMEFWinName = "BIMEF";

static void onTrackbarGamma(int, void *) {
float gamma = g_gamma / 100.0f;
gammaCorrection(g_image, g_imgGamma, gamma);
imshow(g_gammaWinName, g_imgGamma);
}

static void onTrackbarContrastR1(int, void *) {
contrastStretching(g_image, g_contrastStretch, g_r1, g_s1, g_r2, g_s2);
imshow("Contrast Stretching", g_contrastStretch);
}

static void onTrackbarContrastS1(int, void *) {
contrastStretching(g_image, g_contrastStretch, g_r1, g_s1, g_r2, g_s2);
imshow("Contrast Stretching", g_contrastStretch);
}

static void onTrackbarContrastR2(int, void *) {
contrastStretching(g_image, g_contrastStretch, g_r1, g_s1, g_r2, g_s2);
imshow("Contrast Stretching", g_contrastStretch);
}

static void onTrackbarContrastS2(int, void *) {
contrastStretching(g_image, g_contrastStretch, g_r1, g_s1, g_r2, g_s2);
imshow("Contrast Stretching", g_contrastStretch);
}

static void onTrackbarBIMEF(int, void *) {
float mu = g_mu / 100.0f;
BIMEF(g_image, g_imgBIMEF, mu);
imshow(g_BIMEFWinName, g_imgBIMEF);
}
} // namespace

int main(int argc, char **argv) {
CommandLineParser parser(argc, argv, keys);

const std::string inputFilename = parser.get<String>("input");
parser.about("Use this script to apply intensity transformation on an input image.");
if (parser.has("help") || inputFilename.empty()) {
parser.printMessage();
return 0;
}

// Read input image
g_image = imread(inputFilename);

// Create trackbars
namedWindow(g_gammaWinName);
createTrackbar("Gamma value", g_gammaWinName, &g_gamma, g_gammaMax, onTrackbarGamma);

namedWindow(g_contrastWinName);
createTrackbar("Contrast R1", g_contrastWinName, &g_r1, 256, onTrackbarContrastR1);
createTrackbar("Contrast S1", g_contrastWinName, &g_s1, 256, onTrackbarContrastS1);
createTrackbar("Contrast R2", g_contrastWinName, &g_r2, 256, onTrackbarContrastR2);
createTrackbar("Contrast S2", g_contrastWinName, &g_s2, 256, onTrackbarContrastS2);

namedWindow(g_BIMEFWinName);
createTrackbar("Enhancement ratio mu", g_BIMEFWinName, &g_mu, g_muMax, onTrackbarBIMEF);

// Apply intensity transformations
Mat imgAutoscaled, imgLog;
autoscaling(g_image, imgAutoscaled);
gammaCorrection(g_image, g_imgGamma, g_gamma / 100.0f);
logTransform(g_image, imgLog);
contrastStretching(g_image, g_contrastStretch, g_r1, g_s1, g_r2, g_s2);
BIMEF(g_image, g_imgBIMEF, g_mu / 100.0f);

// Display intensity transformation results
imshow("Original Image", g_image);
imshow("Autoscale", imgAutoscaled);
imshow(g_gammaWinName, g_imgGamma);
imshow("Log Transformation", imgLog);
imshow(g_contrastWinName, g_contrastStretch);
imshow(g_BIMEFWinName, g_imgBIMEF);

waitKey(0);
return 0;
}

效果为:

result

下一篇:OpenCV4入门系列教程82:图像轮廓发现