From 056d015b3866f064c9af3c05522dcfedde665006 Mon Sep 17 00:00:00 2001 From: Alexandre Benoit Date: Mon, 12 Dec 2011 15:53:05 +0000 Subject: [PATCH] added tone mapping video processing from OpenEXR images sequences --- ...s_HighDynamicRange_Retina_toneMapping_video.cpp | 325 +++++++++++++++++++++ 1 file changed, 325 insertions(+) create mode 100644 samples/cpp/OpenEXRimages_HighDynamicRange_Retina_toneMapping_video.cpp diff --git a/samples/cpp/OpenEXRimages_HighDynamicRange_Retina_toneMapping_video.cpp b/samples/cpp/OpenEXRimages_HighDynamicRange_Retina_toneMapping_video.cpp new file mode 100644 index 0000000..1d050f0 --- /dev/null +++ b/samples/cpp/OpenEXRimages_HighDynamicRange_Retina_toneMapping_video.cpp @@ -0,0 +1,325 @@ + +//============================================================================ +// Name : HighDynamicRange_RetinaCompression.cpp +// Author : Alexandre Benoit (benoit.alexandre.vision@gmail.com) +// Version : 0.1 +// Copyright : Alexandre Benoit, LISTIC Lab, december 2011 +// Description : HighDynamicRange compression (tone mapping) with the help of the Gipsa/Listic's retina in C++, Ansi-style +//============================================================================ + +#include +#include +#include + +#include "opencv2/opencv.hpp" + +void help(std::string errorMessage) +{ + std::cout<<"Program init error : "< WARNING : image index number of digits cannot exceed 10"<(i))), + cv::Scalar::all(0), -1, 8, 0 ); + rectangle( displayedCurveImage, cv::Point(0, 0), + cv::Point((lowerLimit)*binW, 200), + cv::Scalar::all(128), -1, 8, 0 ); + rectangle( displayedCurveImage, cv::Point(displayedCurveImage.cols, 0), + cv::Point((upperLimit)*binW, 200), + cv::Scalar::all(128), -1, 8, 0 ); + + cv::imshow(figureTitle, displayedCurveImage); +} +/* + * objective : get the gray level map of the input image and rescale it to the range [0-255] if rescale0_255=TRUE, simply trunks else + */void rescaleGrayLevelMat(const cv::Mat &inputMat, cv::Mat &outputMat, const float histogramClippingLimit, const bool rescale0_255) + { + + // adjust output matrix wrt the input size but single channel + std::cout<<"Input image rescaling with histogram edges cutting (in order to eliminate bad pixels created during the HDR image creation) :"< image size (h,w,channels) = "< pixel coding (nbchannel, bytes per channel) = "<(0)=normalizedHist.at(0); + int histLowerLimit=0, histUpperLimit=0; + for (int i=1;i(i)=denseProb.at(i-1)+normalizedHist.at(i); + //std::cout<(i)<<", "<(i)<(i)(i)<1-histogramClippingLimit) + histUpperLimit=i; + } + // deduce min and max admitted gray levels + float minInputValue = (float)histLowerLimit/histSize*255; + float maxInputValue = (float)histUpperLimit/histSize*255; + + std::cout<<"=> Histogram limits " + <<"\n\t"< normalizedHist value = "<(histLowerLimit)<<" => input gray level = "< normalizedHist value = "<(histUpperLimit)<<" => input gray level = "< image size (h,w) = "< the main application is tone mapping of HDR images (i.e. see on a 8bit display a more than 8bits coded (up to 16bits) image with details in high and low luminance ranges"< It applies a spectral whithening (mid-frequency details enhancement)"< high frequency spatio-temporal noise reduction"< low frequency luminance to be reduced (luminance range compression)"< local logarithmic luminance compression allows details to be enhanced in low light conditions\n"< reports comments/remarks at benoit.alexandre.vision@gmail.com"< more informations and papers at : http://sites.google.com/site/benoitalexandrevision/"< 1. take a set of photos from the same viewpoint using bracketing ***"< 2. generate an OpenEXR image with tools like qtpfsgui.sourceforge.net ***"< 3. apply tone mapping with this program ***"< if the last parameter is 'log', then activate log sampling (favour foveal vision and subsamples peripheral vision) + */ + if (useLogSampling) + { + retina = new cv::Retina(inputImage.size(),true, cv::RETINA_COLOR_BAYER, true, 2.0, 10.0); + } + else// -> else allocate "classical" retina : + retina = new cv::Retina(inputImage.size()); + + // save default retina parameters file in order to let you see this and maybe modify it and reload using method "setup" + retina->write("RetinaDefaultParameters.xml"); + + // desactivate Magnocellular pathway processing (motion information extraction) since it is not usefull here + retina->activateMovingContoursProcessing(false); + + // declare retina output buffers + cv::Mat retinaOutput_parvo; + + ///////////////////////////////////////////// + // prepare displays and interactions + histogramClippingValue=0; // default value... updated with interface slider + + std::string retinaInputCorrected("Retina input image (with cut edges histogram for basic pixels error avoidance)"); + cv::namedWindow(retinaInputCorrected,1); + cv::createTrackbar("histogram edges clipping limit", "Retina input image (with cut edges histogram for basic pixels error avoidance)",&histogramClippingValue,50,callBack_rescaleGrayLevelMat); + + std::string RetinaParvoWindow("Retina Parvocellular pathway output : 16bit=>8bit image retina tonemapping"); + cv::namedWindow(RetinaParvoWindow, 1); + colorSaturationFactor=3; + cv::createTrackbar("Color saturation", "Retina Parvocellular pathway output : 16bit=>8bit image retina tonemapping", &colorSaturationFactor,5,callback_saturateColors); + + retinaHcellsGain=40; + cv::createTrackbar("Hcells gain", "Retina Parvocellular pathway output : 16bit=>8bit image retina tonemapping",&retinaHcellsGain,100,callBack_updateRetinaParams); + + localAdaptation_photoreceptors=197; + localAdaptation_Gcells=190; + cv::createTrackbar("Ph sensitivity", "Retina Parvocellular pathway output : 16bit=>8bit image retina tonemapping", &localAdaptation_photoreceptors,199,callBack_updateRetinaParams); + cv::createTrackbar("Gcells sensitivity", "Retina Parvocellular pathway output : 16bit=>8bit image retina tonemapping", &localAdaptation_Gcells,199,callBack_updateRetinaParams); + + std::string powerTransformedInput("EXR image with basic processing : 16bits=>8bits with gamma correction"); + + ///////////////////////////////////////////// + // apply default parameters of user interaction variables + callBack_updateRetinaParams(1,NULL); // first call for default parameters setup + callback_saturateColors(1, NULL); + + // processing loop with stop condition + currentFrameIndex=startFrameIndex; + while(currentFrameIndex <= endFrameIndex) + { + cv::Mat currentFrame = loadNewFrame(inputImageNamePrototype, startFrameIndex, false); + + if (currentFrame.empty()) + { + std::cout<<"Could not load new image (index = "<8bits linear rescaling ", currentFrame); + cv::Mat gammaTransformedImage; + cv::pow(currentFrame, 1./5, gammaTransformedImage); // apply gamma curve: img = img ** (1./5) + imshow(powerTransformedInput, gammaTransformedImage); + // run retina filter + retina->run(imageInputRescaled); + // Retrieve and display retina output + retina->getParvo(retinaOutput_parvo); + cv::imshow(retinaInputCorrected, imageInputRescaled/255.0); + cv::imshow(RetinaParvoWindow, retinaOutput_parvo); + cv::waitKey(4); + // jump to next frame + ++currentFrameIndex; + } + }catch(cv::Exception e) + { + std::cerr<<"Error using Retina : "<