Skip to content

Commit 9383248

Browse files
committed
new module: intensity_transform (includes gamma correction, log transfomration, autoscaling, contrast stretching)
build error fix: remove trailing whitespaces, casting types minor edits based on alalek's feedback
1 parent bb5dadd commit 9383248

10 files changed

+449
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
set(the_description "Intensity transformations")
2+
ocv_define_module(intensity_transform opencv_core WRAP python)

modules/intensity_transform/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Intensity Transformations
2+
========================
3+
4+
This module contains some of the intensity transformation methods used to adjust the constrast of an image. The methods in the module include autoscaling, gamma correction, log transformations, and contrast stretching.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@book{Gonzalez2018,
2+
title={Digital Image Processing 4th Edition},
3+
author={Rafael C. Gonzalez, Richard E. Woods},
4+
year={2018},
5+
publisher={Pearson}
6+
}
7+
8+
@misc{lcs435lab,
9+
title={CS425 Lab: Intensity Transformations and Spatial Filtering},
10+
url={http://www.cs.uregina.ca/Links/class-info/425/Lab3/}
11+
}
12+
13+
@misc{theailearner,
14+
title={Contrast Stretching},
15+
url={https://theailearner.com/2019/01/30/contrast-stretching/}
16+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// This file is part of OpenCV project.
2+
// It is subject to the license terms in the LICENSE file found in the top-level directory
3+
// of this distribution and at http://opencv.org/license.html.
4+
5+
#ifndef OPENCV_INTENSITY_TRANSFORM_H
6+
#define OPENCV_INTENSITY_TRANSFORM_H
7+
8+
#include "opencv2/core.hpp"
9+
10+
/**
11+
* @defgroup intensity_transform The module brings implementations of intensity transformation algorithms to adjust image contrast.
12+
*
13+
* Namespace for all functions is cv::intensity_trasnform.
14+
*
15+
* ### Supported Algorithms
16+
* - Autoscaling
17+
* - Log Transformations
18+
* - Power-Law (Gamma) Transformations
19+
* - Contrast Stretching
20+
*
21+
* Reference from following book and websites:
22+
* - Digital Image Processing 4th Edition Chapter 3 [Rafael C. Gonzalez, Richard E. Woods] @cite Gonzalez2018
23+
* - http://www.cs.uregina.ca/Links/class-info/425/Lab3/ @cite lcs435lab
24+
* - https://theailearner.com/2019/01/30/contrast-stretching/ @cite theailearner
25+
*/
26+
27+
namespace cv {
28+
namespace intensity_transform {
29+
30+
//! @addtogroup intensity_transform
31+
//! @{
32+
33+
/**
34+
* @brief Given an input bgr or grayscale image and constant c, apply log transformation to the image
35+
* on domain [0, 255] and return the resulting image.
36+
*
37+
* @param input input bgr or grayscale image.
38+
* @param output resulting image of log transformations.
39+
*/
40+
CV_EXPORTS_W void logTransform(const Mat input, Mat& output);
41+
42+
/**
43+
* @brief Given an input bgr or grayscale image and constant gamma, apply power-law transformation,
44+
* a.k.a. gamma correction to the image on domain [0, 255] and return the resulting image.
45+
*
46+
* @param input input bgr or grayscale image.
47+
* @param output resulting image of gamma corrections.
48+
* @param gamma constant in c*r^gamma where r is pixel value.
49+
*/
50+
CV_EXPORTS_W void gammaCorrection(const Mat input, Mat& output, const float gamma);
51+
52+
/**
53+
* @brief Given an input bgr or grayscale image, apply autoscaling on domain [0, 255] to increase
54+
* the contrast of the input image and return the resulting image.
55+
*
56+
* @param input input bgr or grayscale image.
57+
* @param output resulting image of autoscaling.
58+
*/
59+
CV_EXPORTS_W void autoscaling(const Mat input, Mat& output);
60+
61+
/**
62+
* @brief Given an input bgr or grayscale image, apply linear contrast stretching on domain [0, 255]
63+
* and return the resulting image.
64+
*
65+
* @param input input bgr or grayscale image.
66+
* @param output resulting image of contrast stretching.
67+
* @param r1 x coordinate of first point (r1, s1) in the transformation function.
68+
* @param s1 y coordinate of first point (r1, s1) in the transformation function.
69+
* @param r2 x coordinate of second point (r2, s2) in the transformation function.
70+
* @param s2 y coordinate of second point (r2, s2) in the transformation function.
71+
*/
72+
CV_EXPORTS_W void contrastStretching(const Mat input, Mat& output, const int r1, const int s1, const int r2, const int s2);
73+
74+
//! @}
75+
76+
}} // cv::intensity_transform::
77+
78+
#endif
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "opencv2/core.hpp"
2+
#include "opencv2/imgcodecs.hpp"
3+
#include "opencv2/highgui.hpp"
4+
#include "opencv2/intensity_transform.hpp"
5+
6+
#include <iostream>
7+
8+
using namespace std;
9+
using namespace cv;
10+
using namespace cv::intensity_transform;
11+
12+
int main(int argc, char **argv)
13+
{
14+
if (argc != 2)
15+
{
16+
cerr << "Must input the path of the input image. Ex: intensity_transform image.jpg" << endl;
17+
return -1;
18+
}
19+
20+
// Read input image
21+
Mat image = imread(argv[1]);
22+
23+
// Apply intensity transformations
24+
Mat imgGamma, imgAutoscaled, imgLog, contrastStretch;
25+
gammaCorrection(image, imgGamma, (float)(0.4));
26+
autoscaling(image, imgAutoscaled);
27+
logTransform(image, imgLog);
28+
contrastStretching(image, contrastStretch, 70, 15, 120, 240);
29+
30+
// Display intensity transformation results
31+
imshow("Original Image", image);
32+
imshow("Autoscale", imgAutoscaled);
33+
imshow("Gamma Correction", imgGamma);
34+
imshow("Log Transformation", imgLog);
35+
imshow("Contrast Stretching", contrastStretch);
36+
waitKey(0);
37+
38+
return 0;
39+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// This file is part of OpenCV project.
2+
// It is subject to the license terms in the LICENSE file found in the top-level directory
3+
// of this distribution and at http://opencv.org/license.html.
4+
5+
#include "precomp.hpp"
6+
7+
using namespace cv;
8+
using namespace std;
9+
10+
namespace cv {
11+
namespace intensity_transform {
12+
13+
void logTransform(const Mat input, Mat& output)
14+
{
15+
double maxVal;
16+
minMaxLoc(input, NULL, &maxVal, NULL, NULL);
17+
const double c = 255 / log(1 + maxVal);
18+
19+
if (input.channels() == 1)
20+
{
21+
output = input + 1;
22+
}
23+
else
24+
{
25+
output = input + Scalar(1, 1, 1);
26+
}
27+
28+
output.convertTo(output, CV_64F);
29+
cv::log(output, output);
30+
output *= c;
31+
output.convertTo(output, CV_8UC3);
32+
}
33+
34+
void gammaCorrection(const Mat input, Mat& output, const float gamma)
35+
{
36+
vector<int> table(256);
37+
for (int i = 0; i < 256; i++)
38+
{
39+
table[i] = (int)(pow((i / 255.0), gamma) * 255.0);
40+
}
41+
42+
LUT(input, table, output);
43+
output.convertTo(output, CV_8UC3);
44+
}
45+
46+
void autoscaling(const Mat input, Mat& output)
47+
{
48+
double minVal, maxVal;
49+
minMaxLoc(input, &minVal, &maxVal, NULL, NULL);
50+
output = 255 * (input - minVal) / (maxVal - minVal);
51+
}
52+
53+
void contrastStretching(const Mat input, Mat& output, const int r1, const int s1, const int r2, const int s2)
54+
{
55+
vector<int> table(256);
56+
for (int i = 0; i < 256; i++)
57+
{
58+
if (i <= r1)
59+
{
60+
table[i] = (int)(((float)s1 / (float)r1) * i);
61+
}
62+
else if (r1 < i && i <= r2)
63+
{
64+
table[i] = (int)(((float)(s2 - s1)/(float)(r2 - r1)) * (i - r1) + s1);
65+
}
66+
else // (r2 < i)
67+
{
68+
table[i] = (int)(((float)(255 - s2)/(float)(255 - r2)) * (i - r2) + s2);
69+
}
70+
}
71+
72+
LUT(input, table, output);
73+
output.convertTo(output, CV_8UC3);
74+
}
75+
76+
}} // cv::intensity_transform::
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// This file is part of OpenCV project.
2+
// It is subject to the license terms in the LICENSE file found in the top-level directory
3+
// of this distribution and at http://opencv.org/license.html.
4+
5+
#ifndef OPENCV_INTENSITY_TRANSFORM_PRECOMP_H
6+
#define OPENCV_INTENSITY_TRANSFORM_PRECOMP_H
7+
8+
#include "opencv2/core.hpp"
9+
#include "opencv2/intensity_transform.hpp"
10+
11+
#endif

0 commit comments

Comments
 (0)