Commit 9aa74df0 authored by Lim's avatar Lim

new module: intensity_transform (includes gamma correction, log transfomration,…

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

removing trailing whitespace

using std::array for LUT argument
parent bb5dadd7
set(the_description "Intensity transformations")
ocv_define_module(intensity_transform opencv_core WRAP python)
Intensity Transformations
========================
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.
\ No newline at end of file
@book{Gonzalez2018,
title={Digital Image Processing 4th Edition},
author={Rafael C. Gonzalez, Richard E. Woods},
year={2018},
publisher={Pearson}
}
@misc{lcs435lab,
title={CS425 Lab: Intensity Transformations and Spatial Filtering},
url={http://www.cs.uregina.ca/Links/class-info/425/Lab3/}
}
@misc{theailearner,
title={Contrast Stretching},
url={https://theailearner.com/2019/01/30/contrast-stretching/}
}
\ No newline at end of file
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef OPENCV_INTENSITY_TRANSFORM_H
#define OPENCV_INTENSITY_TRANSFORM_H
#include "opencv2/core.hpp"
/**
* @defgroup intensity_transform The module brings implementations of intensity transformation algorithms to adjust image contrast.
*
* Namespace for all functions is cv::intensity_trasnform.
*
* ### Supported Algorithms
* - Autoscaling
* - Log Transformations
* - Power-Law (Gamma) Transformations
* - Contrast Stretching
*
* Reference from following book and websites:
* - Digital Image Processing 4th Edition Chapter 3 [Rafael C. Gonzalez, Richard E. Woods] @cite Gonzalez2018
* - http://www.cs.uregina.ca/Links/class-info/425/Lab3/ @cite lcs435lab
* - https://theailearner.com/2019/01/30/contrast-stretching/ @cite theailearner
*/
namespace cv {
namespace intensity_transform {
//! @addtogroup intensity_transform
//! @{
/**
* @brief Given an input bgr or grayscale image and constant c, apply log transformation to the image
* on domain [0, 255] and return the resulting image.
*
* @param input input bgr or grayscale image.
* @param output resulting image of log transformations.
*/
CV_EXPORTS_W void logTransform(const Mat input, Mat& output);
/**
* @brief Given an input bgr or grayscale image and constant gamma, apply power-law transformation,
* a.k.a. gamma correction to the image on domain [0, 255] and return the resulting image.
*
* @param input input bgr or grayscale image.
* @param output resulting image of gamma corrections.
* @param gamma constant in c*r^gamma where r is pixel value.
*/
CV_EXPORTS_W void gammaCorrection(const Mat input, Mat& output, const float gamma);
/**
* @brief Given an input bgr or grayscale image, apply autoscaling on domain [0, 255] to increase
* the contrast of the input image and return the resulting image.
*
* @param input input bgr or grayscale image.
* @param output resulting image of autoscaling.
*/
CV_EXPORTS_W void autoscaling(const Mat input, Mat& output);
/**
* @brief Given an input bgr or grayscale image, apply linear contrast stretching on domain [0, 255]
* and return the resulting image.
*
* @param input input bgr or grayscale image.
* @param output resulting image of contrast stretching.
* @param r1 x coordinate of first point (r1, s1) in the transformation function.
* @param s1 y coordinate of first point (r1, s1) in the transformation function.
* @param r2 x coordinate of second point (r2, s2) in the transformation function.
* @param s2 y coordinate of second point (r2, s2) in the transformation function.
*/
CV_EXPORTS_W void contrastStretching(const Mat input, Mat& output, const int r1, const int s1, const int r2, const int s2);
//! @}
}} // cv::intensity_transform::
#endif
\ No newline at end of file
#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;
int main(int argc, char **argv)
{
if (argc != 2)
{
cerr << "Must input the path of the input image. Ex: intensity_transform image.jpg" << endl;
return -1;
}
// Read input image
Mat image = imread(argv[1]);
// Apply intensity transformations
Mat imgGamma, imgAutoscaled, imgLog, contrastStretch;
gammaCorrection(image, imgGamma, (float)(0.4));
autoscaling(image, imgAutoscaled);
logTransform(image, imgLog);
contrastStretching(image, contrastStretch, 70, 15, 120, 240);
// Display intensity transformation results
imshow("Original Image", image);
imshow("Autoscale", imgAutoscaled);
imshow("Gamma Correction", imgGamma);
imshow("Log Transformation", imgLog);
imshow("Contrast Stretching", contrastStretch);
waitKey(0);
return 0;
}
\ No newline at end of file
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "precomp.hpp"
using namespace cv;
using namespace std;
namespace cv {
namespace intensity_transform {
void logTransform(const Mat input, Mat& output)
{
double maxVal;
minMaxLoc(input, NULL, &maxVal, NULL, NULL);
const double c = 255 / log(1 + maxVal);
Mat add_one_64f;
input.convertTo(add_one_64f, CV_64F, 1, 1.0f);
Mat log_64f;
cv::log(add_one_64f, log_64f);
log_64f.convertTo(output, CV_8UC3, c, 0.0f);
}
void gammaCorrection(const Mat input, Mat& output, const float gamma)
{
std::array<uchar, 256> table;
for (int i = 0; i < 256; i++)
{
table[i] = saturate_cast<uchar>(pow((i / 255.0), gamma) * 255.0);
}
LUT(input, table, output);
}
void autoscaling(const Mat input, Mat& output)
{
double minVal, maxVal;
minMaxLoc(input, &minVal, &maxVal, NULL, NULL);
output = 255 * (input - minVal) / (maxVal - minVal);
}
void contrastStretching(const Mat input, Mat& output, const int r1, const int s1, const int r2, const int s2)
{
std::array<uchar, 256> table;
for (int i = 0; i < 256; i++)
{
if (i <= r1)
{
table[i] = saturate_cast<uchar>(((float)s1 / (float)r1) * i);
}
else if (r1 < i && i <= r2)
{
table[i] = saturate_cast<uchar>(((float)(s2 - s1)/(float)(r2 - r1)) * (i - r1) + s1);
}
else // (r2 < i)
{
table[i] = saturate_cast<uchar>(((float)(255 - s2)/(float)(255 - r2)) * (i - r2) + s2);
}
}
LUT(input, table, output);
}
}} // cv::intensity_transform::
\ No newline at end of file
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef OPENCV_INTENSITY_TRANSFORM_PRECOMP_H
#define OPENCV_INTENSITY_TRANSFORM_PRECOMP_H
#include "opencv2/core.hpp"
#include "opencv2/intensity_transform.hpp"
#include <array>
#endif
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
using namespace cv;
namespace opencv_test { namespace {
TEST(intensity_transform_logTransform, accuracy)
{
uchar image_data[] = {
51, 211, 212, 38, 48, 25, 189, 16, 64, 197,
104, 137, 60, 10, 78, 234, 186, 149, 37, 236,
128, 80, 6, 53, 7, 65, 233, 15, 216, 42,
108, 132, 136, 194, 117, 128, 214, 46, 220, 119,
101, 126, 148, 22, 86, 206, 91, 125, 234, 24,
162, 136, 46, 247, 245, 81, 157, 126, 73, 173,
120, 230, 117, 111, 145, 168, 169, 187, 23, 109,
0, 184, 23, 43, 108, 201, 13, 170, 249, 228,
107, 59, 73, 254, 116, 156, 209, 155, 149, 95,
24, 245, 136, 107, 192, 114, 69, 80, 199, 8
};
Mat_<uchar> image(10, 10, image_data);
Mat res;
cv::intensity_transform::logTransform(image, res);
uchar expectedRes_data[] = {
182, 247, 247, 169, 179, 150, 241, 130, 192, 243,
214, 227, 189, 110, 201, 251, 241, 231, 167, 252,
224, 202, 90, 184, 96, 193, 251, 128, 248, 173,
216, 225, 226, 243, 220, 224, 247, 177, 248, 220,
213, 223, 230, 144, 206, 245, 208, 223, 251, 148,
234, 226, 177, 254, 253, 203, 233, 223, 198, 237,
221, 250, 220, 217, 229, 236, 236, 241, 146, 216,
0, 240, 146, 174, 216, 244, 121, 237, 254, 250,
215, 188, 198, 255, 219, 233, 246, 232, 231, 210,
148, 253, 226, 215, 242, 218, 196, 202, 244, 101
};
Mat_<uchar> expectedRes(10, 10, expectedRes_data);
EXPECT_LE(cvtest::norm(res, expectedRes, NORM_INF), 1);
}
TEST(intensity_transform_gammaCorrection, accuracy1)
{
uchar image_data[] = {
51, 211, 212, 38, 48, 25, 189, 16, 64, 197,
104, 137, 60, 10, 78, 234, 186, 149, 37, 236,
128, 80, 6, 53, 7, 65, 233, 15, 216, 42,
108, 132, 136, 194, 117, 128, 214, 46, 220, 119,
101, 126, 148, 22, 86, 206, 91, 125, 234, 24,
162, 136, 46, 247, 245, 81, 157, 126, 73, 173,
120, 230, 117, 111, 145, 168, 169, 187, 23, 109,
0, 184, 23, 43, 108, 201, 13, 170, 249, 228,
107, 59, 73, 254, 116, 156, 209, 155, 149, 95,
24, 245, 136, 107, 192, 114, 69, 80, 199, 8
};
Mat_<uchar> image(10, 10, image_data);
Mat res;
cv::intensity_transform::gammaCorrection(image, res, 1.0);
uchar expectedRes_data[] = {
51, 211, 212, 38, 48, 25, 189, 16, 64, 197,
104, 137, 60, 10, 78, 234, 186, 149, 37, 236,
128, 80, 6, 53, 7, 65, 233, 15, 216, 42,
108, 132, 136, 194, 117, 128, 214, 46, 220, 119,
101, 126, 148, 22, 86, 206, 91, 125, 234, 24,
162, 136, 46, 247, 245, 81, 157, 126, 73, 173,
120, 230, 117, 111, 145, 168, 169, 187, 23, 109,
0, 184, 23, 43, 108, 201, 13, 170, 249, 228,
107, 59, 73, 254, 116, 156, 209, 155, 149, 95,
24, 245, 136, 107, 192, 114, 69, 80, 199, 8
};
Mat_<uchar> expectedRes(10, 10, expectedRes_data);
EXPECT_LE(cvtest::norm(res, expectedRes, NORM_INF), 1);
}
TEST(intensity_transform_gammaCorrection, accuracy2)
{
uchar image_data[] = {
51, 211, 212, 38, 48, 25, 189, 16, 64, 197,
104, 137, 60, 10, 78, 234, 186, 149, 37, 236,
128, 80, 6, 53, 7, 65, 233, 15, 216, 42,
108, 132, 136, 194, 117, 128, 214, 46, 220, 119,
101, 126, 148, 22, 86, 206, 91, 125, 234, 24,
162, 136, 46, 247, 245, 81, 157, 126, 73, 173,
120, 230, 117, 111, 145, 168, 169, 187, 23, 109,
0, 184, 23, 43, 108, 201, 13, 170, 249, 228,
107, 59, 73, 254, 116, 156, 209, 155, 149, 95,
24, 245, 136, 107, 192, 114, 69, 80, 199, 8
};
Mat_<uchar> image(10, 10, image_data);
Mat res;
cv::intensity_transform::gammaCorrection(image, res, (float)(0.4));
uchar expectedRes_data[] = {
133, 236, 236, 119, 130, 100, 226, 84, 146, 229,
178, 198, 142, 69, 158, 246, 224, 205, 117, 247,
193, 160, 56, 136, 60, 147, 245, 82, 238, 123,
180, 195, 198, 228, 186, 193, 237, 128, 240, 187,
176, 192, 205, 95, 165, 234, 168, 191, 246, 99,
212, 198, 128, 251, 250, 161, 210, 192, 154, 218,
188, 244, 186, 182, 203, 215, 216, 225, 97, 181,
0, 223, 97, 125, 180, 231, 77, 216, 252, 243,
180, 141, 154, 254, 186, 209, 235, 208, 205, 171,
99, 250, 198, 180, 227, 184, 151, 160, 230, 63
};
Mat_<uchar> expectedRes(10, 10, expectedRes_data);
EXPECT_LE(cvtest::norm(res, expectedRes, NORM_INF), 1);
}
TEST(intensity_transform_autoscaling, accuracy)
{
uchar image_data[] = {
32, 59, 164, 127, 151, 107, 167, 62, 195, 143,
54, 166, 104, 27, 152, 20, 35, 135, 12, 198,
107, 63, 90, 169, 67, 135, 136, 14, 94, 115,
34, 150, 169, 171, 130, 39, 190, 108, 103, 32,
57, 83, 146, 37, 81, 143, 144, 47, 87, 49,
32, 108, 17, 165, 127, 137, 108, 35, 179, 175,
40, 148, 174, 79, 146, 119, 103, 168, 167, 160,
66, 107, 164, 19, 85, 126, 58, 95, 15, 131,
88, 58, 162, 90, 147, 125, 61, 157, 60, 104,
128, 193, 69, 104, 94, 196, 11, 66, 18, 179
};
Mat_<uchar> image(10, 10, image_data);
Mat res;
cv::intensity_transform::autoscaling(image, res);
uchar expectedRes_data[] = {
29, 65, 209, 158, 191, 131, 213, 70, 251, 180,
59, 211, 127, 22, 192, 12, 33, 169, 1, 255,
131, 71, 108, 215, 76, 169, 170, 4, 113, 142,
31, 190, 215, 218, 162, 38, 244, 132, 125, 29,
63, 98, 184, 35, 95, 180, 181, 49, 104, 52,
29, 132, 8, 210, 158, 172, 132, 33, 229, 224,
40, 187, 222, 93, 184, 147, 125, 214, 213, 203,
75, 131, 209, 11, 101, 157, 64, 115, 5, 164,
105, 64, 206, 108, 185, 155, 68, 199, 67, 127,
160, 248, 79, 127, 113, 252, 0, 75, 10, 229
};
Mat_<uchar> expectedRes(10, 10, expectedRes_data);
EXPECT_LE(cvtest::norm(res, expectedRes, NORM_INF), 1);
}
TEST(intensity_transform_contrastStretching, accuracy)
{
uchar image_data[] = {
32, 59, 164, 127, 151, 107, 167, 62, 195, 143,
54, 166, 104, 27, 152, 20, 35, 135, 12, 198,
107, 63, 90, 169, 67, 135, 136, 14, 94, 115,
34, 150, 169, 171, 130, 39, 190, 108, 103, 32,
57, 83, 146, 37, 81, 143, 144, 47, 87, 49,
32, 108, 17, 165, 127, 137, 108, 35, 179, 175,
40, 148, 174, 79, 146, 119, 103, 168, 167, 160,
66, 107, 164, 19, 85, 126, 58, 95, 15, 131,
88, 58, 162, 90, 147, 125, 61, 157, 60, 104,
128, 193, 69, 104, 94, 196, 11, 66, 18, 179
};
Mat_<uchar> image(10, 10, image_data);
Mat res;
cv::intensity_transform::contrastStretching(image, res, 70, 15, 120, 240);
uchar expectedRes_data[] = {
6, 12, 244, 240, 243, 181, 245, 13, 248, 242,
11, 245, 168, 5, 243, 4, 7, 241, 2, 248,
181, 13, 105, 245, 14, 241, 241, 3, 123, 217,
7, 243, 245, 245, 241, 8, 247, 186, 163, 6,
12, 73, 242, 7, 64, 242, 242, 10, 91, 10,
6, 186, 3, 245, 240, 241, 186, 7, 246, 246,
8, 243, 246, 55, 242, 235, 163, 245, 245, 244,
14, 181, 244, 4, 82, 240, 12, 127, 3, 241,
96, 12, 244, 105, 243, 240, 13, 244, 12, 168,
240, 248, 14, 168, 123, 248, 2, 14, 3, 246
};
Mat_<uchar> expectedRes(10, 10, expectedRes_data);
EXPECT_LE(cvtest::norm(res, expectedRes, NORM_INF), 1);
}
}} // namespace
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
CV_TEST_MAIN("cv")
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef __OPENCV_TEST_PRECOMP_HPP__
#define __OPENCV_TEST_PRECOMP_HPP__
#include "opencv2/ts.hpp"
#include "opencv2/intensity_transform.hpp"
namespace opencv_test {
using namespace cv::intensity_transform;
}
#endif
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment