File: intensity_transform.cpp

package info (click to toggle)
opencv 4.5.1%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 268,248 kB
  • sloc: cpp: 969,170; xml: 682,525; python: 36,732; lisp: 30,170; java: 25,155; ansic: 7,927; javascript: 5,643; objc: 2,041; sh: 935; cs: 601; perl: 494; makefile: 145
file content (123 lines) | stat: -rw-r--r-- 3,561 bytes parent folder | download | duplicates (3)
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
114
115
116
117
118
119
120
121
122
123
#include "opencv2/core.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.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);
}
}

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