OpenCV4入门教程090:强度变换

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

图像强度

英文名称是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
#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 {
// 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) {
// Read input image
g_image = imread("1.png");
if(g_image.empty()) {
std::cout<<"Image is empty!"<<std::endl;
}

// 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;
}

流程为

  • 使用autoscaling函数将图片的值缩放到[0,255]范围内,用以增强图片对比度,这就是自动变换
  • gamma变换
  • 对数变换
  • 对比度拉伸
  • 生物启发的低光图像增强多曝光融合框架(A Bio-Inspired Multi-Exposure Fusion Framework for Low-light Image Enhancement,BIMEF)。低光图像由于可见性低,不利于人类观测和计算机视觉算法。虽然许多人提出了许多图像增强技术来解决这一问题,但现有的方法不可避免地会引入对比度欠增强和过增强。受人类视觉系统的启发,我们设计了一个用于弱光图像增强的多曝光融合框架。在此框架上,我们提出了一种双曝光融合算法,以提供准确的对比度和亮度增强。具体地说,我们首先利用照明估计技术设计了图像融合的权重矩阵。然后,我们引入了我们的相机响应模型来合成多曝光图像。接下来,我们找到最佳的曝光率,以便合成图像在原始图像曝光不足的区域被很好地曝光。最后,根据权重矩阵将输入图像与合成图像进行融合,得到了增强效果。实验表明,与几种最先进的方法相比,我们的方法可以获得较少的对比度和光失真的结果。

效果为:

result


OpenCV4入门教程090:强度变换
https://feater.top/opencv/opencv-image-intensity-transform/
作者
JackeyLea
发布于
2020年9月20日
许可协议