Skip to content

new module: intensity_transform #2399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions modules/intensity_transform/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
set(the_description "Intensity transformations")
ocv_define_module(intensity_transform opencv_core WRAP python)
4 changes: 4 additions & 0 deletions modules/intensity_transform/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
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.
16 changes: 16 additions & 0 deletions modules/intensity_transform/doc/intensity_transform.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@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/}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// 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
39 changes: 39 additions & 0 deletions modules/intensity_transform/samples/intensity_transform.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#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;
}
65 changes: 65 additions & 0 deletions modules/intensity_transform/src/intensity_transform.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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::
12 changes: 12 additions & 0 deletions modules/intensity_transform/src/precomp.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// 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
Loading