@ -0,0 +1,3 @@ |
||||
set(the_description "Color balance algorithms") |
||||
ocv_warnings_disable(CMAKE_CXX_FLAGS -Wundef) |
||||
ocv_define_module(colorbalance opencv_core opencv_imgproc OPTIONAL opencv_highgui) |
@ -0,0 +1,8 @@ |
||||
******************************************** |
||||
colorbalance. Color balance |
||||
******************************************** |
||||
|
||||
.. toctree:: |
||||
:maxdepth: 2 |
||||
|
||||
Color balance <colorbalance/whitebalance> |
@ -0,0 +1,31 @@ |
||||
Automatic white balance correction |
||||
********************************** |
||||
|
||||
.. highlight:: cpp |
||||
|
||||
balanceWhite |
||||
------------ |
||||
.. ocv:function:: (const Mat &src, Mat &dst, const int algorithmType, |
||||
const float inputMin = 0.0f, const float inputMax = 255.0f, |
||||
const float outputMin = 0.0f, const float outputMax = 255.0f); |
||||
|
||||
The function implements different algorithm of automatic white balance, i.e. |
||||
it tries to map image's white color to perceptual white (this can be violated |
||||
due to specific illumination or camera settings). |
||||
|
||||
:param src : source image |
||||
:param dst : destination image |
||||
:param algorithmType : type of the algorithm to use. |
||||
Use WHITE_BALANCE_SIMPLE to perform |
||||
smart histogram adjustments |
||||
(ignoring 4% pixels with minimal |
||||
and maximal values) for each channel. |
||||
:param inputMin : minimum value in the input image |
||||
:param inputMax : maximum value in the input image |
||||
:param outputMin : minimum value in the output image |
||||
:param outputMax : maximum value in the output image |
||||
|
||||
.. seealso:: |
||||
|
||||
:ocv:class:`cvtColor`, |
||||
:ocv:class:`equalizeHist` |
@ -0,0 +1,48 @@ |
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||
//
|
||||
// By downloading, copying, installing or using the software you agree to this license.
|
||||
// If you do not agree to this license, do not download, install,
|
||||
// copy or use the software.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
||||
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification,
|
||||
// are permitted provided that the following conditions are met:
|
||||
//
|
||||
// * Redistribution's of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// * Redistribution's in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
//
|
||||
// * The name of the copyright holders may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// This software is provided by the copyright holders and contributors "as is" and
|
||||
// any express or implied warranties, including, but not limited to, the implied
|
||||
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
||||
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
||||
// indirect, incidental, special, exemplary, or consequential damages
|
||||
// (including, but not limited to, procurement of substitute goods or services;
|
||||
// loss of use, data, or profits; or business interruption) however caused
|
||||
// and on any theory of liability, whether in contract, strict liability,
|
||||
// or tort (including negligence or otherwise) arising in any way out of
|
||||
// the use of this software, even if advised of the possibility of such damage.
|
||||
//
|
||||
//M*/
|
||||
|
||||
#ifndef __OPENCV_EDGEDETECTION_HPP__ |
||||
#define __OPENCV_EDGEDETECTION_HPP__ |
||||
|
||||
#include "opencv2/core.hpp" |
||||
#include "opencv2/colorbalance/simple_color_balance.hpp" |
||||
#endif |
@ -0,0 +1,81 @@ |
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||
//
|
||||
// By downloading, copying, installing or using the software you agree to this license.
|
||||
// If you do not agree to this license, do not download, install,
|
||||
// copy or use the software.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
||||
// Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification,
|
||||
// are permitted provided that the following conditions are met:
|
||||
//
|
||||
// * Redistribution's of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// * Redistribution's in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
//
|
||||
// * The name of the copyright holders may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// This software is provided by the copyright holders and contributors "as is" and
|
||||
// any express or implied warranties, including, but not limited to, the implied
|
||||
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
||||
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
||||
// indirect, incidental, special, exemplary, or consequential damages
|
||||
// (including, but not limited to, procurement of substitute goods or services;
|
||||
// loss of use, data, or profits; or business interruption) however caused
|
||||
// and on any theory of liability, whether in contract, strict liability,
|
||||
// or tort (including negligence or otherwise) arising in any way out of
|
||||
// the use of this software, even if advised of the possibility of such damage.
|
||||
//
|
||||
//M*/
|
||||
|
||||
#ifndef __OPENCV_SIMPLE_COLOR_BALANCE_HPP__ |
||||
#define __OPENCV_SIMPLE_COLOR_BALANCE_HPP__ |
||||
|
||||
/*
|
||||
* simple_color_balance.hpp |
||||
* |
||||
* Created on: Jun 26, 2014 |
||||
* Author: Yury Gitman |
||||
*/ |
||||
|
||||
#include <opencv2/core.hpp> |
||||
|
||||
/*! \namespace cv
|
||||
Namespace where all the C++ OpenCV functionality resides |
||||
*/ |
||||
namespace cv |
||||
{ |
||||
//! various white balance algorithms
|
||||
enum
|
||||
{ |
||||
WHITE_BALANCE_SIMPLE = 0, |
||||
WHITE_BALANCE_GRAYWORLD = 1 |
||||
}; |
||||
|
||||
/*! This function implements different white balance algorithms
|
||||
* \param src : source image |
||||
* \param dst : destination image |
||||
* \param algorithmType : type of the algorithm to use |
||||
* \param inputMin : minimum input value |
||||
* \param inputMax : maximum output value |
||||
* \param outputMin : minimum input value |
||||
* \param outputMax : maximum output value |
||||
*/ |
||||
CV_EXPORTS_W void balanceWhite(const Mat &src, Mat &dst, const int algorithmType, |
||||
const float inputMin = 0.0f, const float inputMax = 255.0f, |
||||
const float outputMin = 0.0f, const float outputMax = 255.0f); |
||||
} |
||||
|
||||
#endif // __OPENCV_SIMPLE_COLOR_BALANCE_HPP__
|
@ -0,0 +1,60 @@ |
||||
#include "opencv2/colorbalance.hpp" |
||||
|
||||
#include "opencv2/imgproc.hpp" |
||||
#include "opencv2/highgui.hpp" |
||||
|
||||
#include "opencv2/core/utility.hpp" |
||||
#include "opencv2/imgproc/types_c.h" |
||||
|
||||
const char* keys = |
||||
{ |
||||
"{i || input image name}" |
||||
"{o || output image name}" |
||||
}; |
||||
|
||||
int main( int argc, const char** argv ) |
||||
{ |
||||
bool printHelp = ( argc == 1 ); |
||||
printHelp = printHelp || ( argc == 2 && std::string(argv[1]) == "--help" ); |
||||
printHelp = printHelp || ( argc == 2 && std::string(argv[1]) == "-h" ); |
||||
|
||||
if ( printHelp ) |
||||
{ |
||||
printf("\nThis sample demonstrates simple color balance algorithm\n" |
||||
"Call:\n" |
||||
" simple_color_blance -i=in_image_name [-o=out_image_name]\n\n"); |
||||
return 0; |
||||
} |
||||
|
||||
cv::CommandLineParser parser(argc, argv, keys); |
||||
if ( !parser.check() ) |
||||
{ |
||||
parser.printErrors(); |
||||
return -1; |
||||
} |
||||
|
||||
std::string inFilename = parser.get<std::string>("i"); |
||||
std::string outFilename = parser.get<std::string>("o"); |
||||
|
||||
cv::Mat src = cv::imread(inFilename, 1); |
||||
if ( src.empty() ) |
||||
{ |
||||
printf("Cannot read image file: %s\n", inFilename.c_str()); |
||||
return -1; |
||||
} |
||||
|
||||
cv::Mat res(src.size(), src.type()); |
||||
cv::balanceWhite(src, res, cv::WHITE_BALANCE_SIMPLE); |
||||
|
||||
if ( outFilename == "" ) |
||||
{ |
||||
cv::namedWindow("after white balance", 1); |
||||
cv::imshow("after white balance", res); |
||||
|
||||
cv::waitKey(0); |
||||
} |
||||
else |
||||
cv::imwrite(outFilename, res); |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,205 @@ |
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||
//
|
||||
// By downloading, copying, installing or using the software you agree to this license.
|
||||
// If you do not agree to this license, do not download, install,
|
||||
// copy or use the software.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
||||
// Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
//
|
||||
// * Redistribution's of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// * Redistribution's in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
//
|
||||
// * The name of Intel Corporation may not be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// This software is provided by the copyright holders and contributors "as is" and
|
||||
// any express or implied warranties, including, but not limited to, the implied
|
||||
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
||||
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
||||
// indirect, incidental, special, exemplary, or consequential damages
|
||||
// (including, but not limited to, procurement of substitute goods or services;
|
||||
// loss of use, data, or profits; or business interruption) however caused
|
||||
// and on any theory of liability, whether in contract, strict liability,
|
||||
// or tort (including negligence or otherwise) arising in any way out of
|
||||
// the use of this software, even if advised of the possibility of such damage.
|
||||
//
|
||||
//M*/
|
||||
|
||||
#include <vector> |
||||
#include <algorithm> |
||||
#include <iterator> |
||||
#include <iostream> |
||||
|
||||
#include "opencv2/colorbalance.hpp" |
||||
|
||||
#include "opencv2/imgproc.hpp" |
||||
|
||||
#include "opencv2/core.hpp" |
||||
#include "opencv2/core/core_c.h" |
||||
|
||||
#include "opencv2/core/types.hpp" |
||||
#include "opencv2/core/types_c.h" |
||||
|
||||
namespace cv |
||||
{ |
||||
template <typename T> |
||||
void balanceWhite(std::vector < Mat_<T> > &src, Mat &dst, |
||||
const float inputMin, const float inputMax, |
||||
const float outputMin, const float outputMax, const int algorithmType) |
||||
{ |
||||
switch ( algorithmType ) |
||||
{ |
||||
case WHITE_BALANCE_SIMPLE: |
||||
{ |
||||
/********************* Simple white balance *********************/ |
||||
float s1 = 2.0f; // low quantile
|
||||
float s2 = 2.0f; // high quantile
|
||||
|
||||
int depth = 2; // depth of histogram tree
|
||||
if (src[0].depth() != CV_8U) |
||||
++depth; |
||||
int bins = 16; // number of bins at each histogram level
|
||||
|
||||
int nElements = int( pow(bins, depth) ); |
||||
// number of elements in histogram tree
|
||||
|
||||
for (size_t i = 0; i < src.size(); ++i) |
||||
{ |
||||
std::vector <int> hist(nElements, 0); |
||||
|
||||
Mat_<T>::iterator beginIt = src[i].begin(); |
||||
Mat_<T>::iterator endIt = src[i].end(); |
||||
|
||||
for (Mat_<T>::iterator it = beginIt; it != endIt; ++it) |
||||
// histogram filling
|
||||
{ |
||||
int pos = 0; |
||||
float minValue = inputMin - 0.5f; |
||||
float maxValue = inputMax + 0.5f; |
||||
T val = *it; |
||||
|
||||
float interval = float(maxValue - minValue) / bins; |
||||
|
||||
for (int j = 0; j < depth; ++j) |
||||
{ |
||||
int currentBin = (val - minValue + 1e-4) / interval; |
||||
++hist[pos + currentBin]; |
||||
|
||||
pos = (pos + currentBin)*bins; |
||||
|
||||
minValue = minValue + currentBin*interval; |
||||
maxValue = minValue + interval; |
||||
|
||||
interval /= bins; |
||||
} |
||||
} |
||||
|
||||
int total = src[i].total(); |
||||
|
||||
int p1 = 0, p2 = bins - 1; |
||||
int n1 = 0, n2 = total; |
||||
|
||||
float minValue = inputMin - 0.5f; |
||||
float maxValue = inputMax + 0.5f; |
||||
|
||||
float interval = (maxValue - minValue) / float(bins); |
||||
|
||||
for (int j = 0; j < depth; ++j) |
||||
// searching for s1 and s2
|
||||
{ |
||||
while (n1 + hist[p1] < s1 * total / 100.0f) |
||||
{ |
||||
n1 += hist[p1++]; |
||||
minValue += interval; |
||||
} |
||||
p1 *= bins; |
||||
|
||||
while (n2 - hist[p2] > (100.0f - s2) * total / 100.0f) |
||||
{ |
||||
n2 -= hist[p2--]; |
||||
maxValue -= interval; |
||||
} |
||||
p2 = p2*bins - 1; |
||||
|
||||
interval /= bins; |
||||
} |
||||
|
||||
src[i] = (outputMax - outputMin) * (src[i] - minValue) |
||||
/ (maxValue - minValue) + outputMin; |
||||
} |
||||
/****************************************************************/ |
||||
break; |
||||
} |
||||
default: |
||||
CV_Assert( false ); |
||||
} |
||||
|
||||
dst.create(/**/ src[0].size(), CV_MAKETYPE( src[0].depth(), src.size() ) /**/); |
||||
cv::merge(src, dst); |
||||
} |
||||
|
||||
/*!
|
||||
* Wrappers over different white balance algorithm |
||||
* |
||||
* \param src : source image (RGB) |
||||
* \param dst : destination image |
||||
* |
||||
* \param inputMin : minimum input value |
||||
* \param inputMax : maximum input value |
||||
* \param outputMin : minimum output value |
||||
* \param outputMax : maximum output value |
||||
* |
||||
* \param algorithmType : type of the algorithm to use |
||||
*/ |
||||
void balanceWhite(const Mat &src, Mat &dst, const int algorithmType, |
||||
const float inputMin, const float inputMax, |
||||
const float outputMin, const float outputMax) |
||||
{ |
||||
switch ( src.depth() ) |
||||
{ |
||||
case CV_8U: |
||||
{ |
||||
std::vector < Mat_<uchar> > mv; |
||||
split(src, mv); |
||||
balanceWhite(mv, dst, inputMin, inputMax, outputMin, outputMax, algorithmType); |
||||
break; |
||||
} |
||||
case CV_16S: |
||||
{ |
||||
std::vector < Mat_<short> > mv; |
||||
split(src, mv); |
||||
balanceWhite(mv, dst, inputMin, inputMax, outputMin, outputMax, algorithmType); |
||||
break; |
||||
} |
||||
case CV_32S: |
||||
{ |
||||
std::vector < Mat_<int> > mv; |
||||
split(src, mv); |
||||
balanceWhite(mv, dst, inputMin, inputMax, outputMin, outputMax, algorithmType); |
||||
break; |
||||
} |
||||
case CV_32F: |
||||
{ |
||||
std::vector < Mat_<float> > mv; |
||||
split(src, mv); |
||||
balanceWhite(mv, dst, inputMin, inputMax, outputMin, outputMax, algorithmType); |
||||
break; |
||||
} |
||||
default: |
||||
CV_Assert( false ); |
||||
break; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,29 @@ |
||||
#include "test_precomp.hpp" |
||||
|
||||
namespace cvtest |
||||
{ |
||||
TEST(colorbalance_SimpleColorBalance, regression) |
||||
{ |
||||
cv::String dir = cvtest::TS::ptr()->get_data_path(); |
||||
int nTests = 12; |
||||
float threshold = 0.005; |
||||
|
||||
for (int i = 0; i < nTests; ++i) |
||||
{ |
||||
cv::String srcName = dir + cv::format( "sources/%02d.png", i + 1); |
||||
cv::Mat src = cv::imread( srcName, 1 ); |
||||
|
||||
cv::String previousResultName = dir + cv::format( "results/%02d.png", i + 1 ); |
||||
cv::Mat previousResult = cv::imread( previousResultName, 1 ); |
||||
|
||||
cv::Mat currentResult; |
||||
cv::balanceWhite(src, currentResult, cv::WHITE_BALANCE_SIMPLE); |
||||
|
||||
cv::Mat sqrError = ( currentResult - previousResult ) |
||||
.mul( currentResult - previousResult ); |
||||
cv::Scalar mse = cv::sum(sqrError) / cv::Scalar::all( sqrError.total()*sqrError.channels() ); |
||||
|
||||
EXPECT_LE( mse[0]+mse[1]+mse[2]+mse[3], threshold ); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,3 @@ |
||||
#include "test_precomp.hpp" |
||||
|
||||
CV_TEST_MAIN("colorbalance") |
@ -0,0 +1,20 @@ |
||||
#ifdef __GNUC__ |
||||
# pragma GCC diagnostic ignored "-Wmissing-declarations" |
||||
# if defined __clang__ || defined __APPLE__ |
||||
# pragma GCC diagnostic ignored "-Wmissing-prototypes" |
||||
# pragma GCC diagnostic ignored "-Wextra" |
||||
# endif |
||||
#endif |
||||
|
||||
#ifndef __OPENCV_TEST_PRECOMP_HPP__ |
||||
#define __OPENCV_TEST_PRECOMP_HPP__ |
||||
|
||||
#include "opencv2/core.hpp" |
||||
#include "opencv2/imgproc.hpp" |
||||
#include "opencv2/imgproc/types_c.h" |
||||
#include "opencv2/highgui.hpp" |
||||
#include "opencv2/colorbalance.hpp" |
||||
#include "opencv2/ts.hpp" |
||||
#include <iostream> |
||||
|
||||
#endif |
After Width: | Height: | Size: 478 KiB |
After Width: | Height: | Size: 558 KiB |
After Width: | Height: | Size: 548 KiB |
After Width: | Height: | Size: 1.0 MiB |
After Width: | Height: | Size: 124 KiB |
After Width: | Height: | Size: 275 KiB |
After Width: | Height: | Size: 162 KiB |
After Width: | Height: | Size: 565 KiB |
After Width: | Height: | Size: 565 KiB |
After Width: | Height: | Size: 367 KiB |
After Width: | Height: | Size: 693 KiB |
After Width: | Height: | Size: 374 KiB |
After Width: | Height: | Size: 294 KiB |
After Width: | Height: | Size: 252 KiB |
After Width: | Height: | Size: 312 KiB |
After Width: | Height: | Size: 356 KiB |
After Width: | Height: | Size: 461 KiB |
After Width: | Height: | Size: 856 KiB |
After Width: | Height: | Size: 80 KiB |
After Width: | Height: | Size: 250 KiB |
After Width: | Height: | Size: 139 KiB |
After Width: | Height: | Size: 473 KiB |
After Width: | Height: | Size: 473 KiB |
After Width: | Height: | Size: 316 KiB |
After Width: | Height: | Size: 511 KiB |
After Width: | Height: | Size: 297 KiB |
After Width: | Height: | Size: 158 KiB |
After Width: | Height: | Size: 198 KiB |