diff --git a/image-processing/1-SlicedOptimalTransport/c++/CMakeLists.txt b/image-processing/1-SlicedOptimalTransport/c++/CMakeLists.txt index 4508c53..fe6fa25 100644 --- a/image-processing/1-SlicedOptimalTransport/c++/CMakeLists.txt +++ b/image-processing/1-SlicedOptimalTransport/c++/CMakeLists.txt @@ -3,6 +3,7 @@ set(EXAMPLES gammaCorrection grayscale equalization + optimalTransport ) foreach(EXAMPLE ${EXAMPLES}) diff --git a/image-processing/1-SlicedOptimalTransport/c++/equalization.cpp b/image-processing/1-SlicedOptimalTransport/c++/equalization.cpp index 650c162..85a65d5 100644 --- a/image-processing/1-SlicedOptimalTransport/c++/equalization.cpp +++ b/image-processing/1-SlicedOptimalTransport/c++/equalization.cpp @@ -51,7 +51,7 @@ bool silent; int main(int argc, char **argv) { - CLI::App app{"colorTransfer"}; + CLI::App app{"equalization"}; std::string sourceImage; app.add_option("-s,--source", sourceImage, "Source image")->required()->check(CLI::ExistingFile); std::string outputImage= "output.png"; diff --git a/image-processing/1-SlicedOptimalTransport/c++/gammaCorrection.cpp b/image-processing/1-SlicedOptimalTransport/c++/gammaCorrection.cpp index b66d617..662d866 100644 --- a/image-processing/1-SlicedOptimalTransport/c++/gammaCorrection.cpp +++ b/image-processing/1-SlicedOptimalTransport/c++/gammaCorrection.cpp @@ -55,20 +55,20 @@ bool silent; int main(int argc, char **argv) { - CLI::App app{"colorTransfer"}; + CLI::App app{"gammaCorrection"}; std::string sourceImage; app.add_option("-s,--source", sourceImage, "Source image")->required()->check(CLI::ExistingFile); std::string outputImage= "output.png"; app.add_option("-o,--output", outputImage, "Output image")->required(); + float A = 1.0; + app.add_option("-A", A, "A (1.0)"); + float gamma = 1.4; + app.add_option("-g,--gamma", gamma, "gamma value (1.4)"); unsigned int nbSteps = 3; app.add_option("-n,--nbsteps", nbSteps, "Number of sliced steps (3)"); silent = false; app.add_flag("--silent", silent, "No verbose messages"); CLI11_PARSE(app, argc, argv); - - float A, gamma; - A = 1.0; - gamma = 1.4; //Image loading int width,height, nbChannels; @@ -79,6 +79,11 @@ int main(int argc, char **argv) std::cout<< "Input images must be RGB images."< 1) { + std::cout<< "A should be between -1 and 1."< output(width*height*nbChannels); diff --git a/image-processing/1-SlicedOptimalTransport/c++/grayscale.cpp b/image-processing/1-SlicedOptimalTransport/c++/grayscale.cpp index 61b8858..157dac0 100644 --- a/image-processing/1-SlicedOptimalTransport/c++/grayscale.cpp +++ b/image-processing/1-SlicedOptimalTransport/c++/grayscale.cpp @@ -44,7 +44,7 @@ bool silent; int main(int argc, char **argv) { - CLI::App app{"colorTransfer"}; + CLI::App app{"grayscale"}; std::string sourceImage; app.add_option("-s,--source", sourceImage, "Source image")->required()->check(CLI::ExistingFile);; std::string outputImage= "output.png"; diff --git a/image-processing/1-SlicedOptimalTransport/c++/optimalTransport.cpp b/image-processing/1-SlicedOptimalTransport/c++/optimalTransport.cpp new file mode 100644 index 0000000..bd1d9f4 --- /dev/null +++ b/image-processing/1-SlicedOptimalTransport/c++/optimalTransport.cpp @@ -0,0 +1,137 @@ +/* + Copyright (c) 2020 CNRS + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + 2. Redistributions 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. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIEDi + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER 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. + */ +#include +#include +#include +#include +//Command-line parsing +#include + +//Image filtering and I/O +#define cimg_display 0 +#include +#define STB_IMAGE_IMPLEMENTATION +#include +#define STB_IMAGE_WRITE_IMPLEMENTATION +#include + +//Global flag to silent verbose messages +bool silent; + + +int main(int argc, char **argv) +{ + CLI::App app{"optimalTransport"}; + std::string sourceImage; + app.add_option("-s,--source", sourceImage, "Source image")->required()->check(CLI::ExistingFile);; + std::string targetImage; + app.add_option("-t,--target", targetImage, "Target image")->required()->check(CLI::ExistingFile);; + std::string outputImage= "output.png"; + app.add_option("-o,--output", outputImage, "Output image")->required(); + unsigned int nbSteps = 3; + app.add_option("-n,--nbsteps", nbSteps, "Number of sliced steps (3)"); + silent = false; + app.add_flag("--silent", silent, "No verbose messages"); + CLI11_PARSE(app, argc, argv); + + //Image loading + int width,height, nbChannels; + unsigned char *source = stbi_load(sourceImage.c_str(), &width, &height, &nbChannels, 0); + if (!silent) std::cout<< "Source image: "< output(width*height*nbChannels); + std::vector indexA(width*height), indexB(width*height); + for (auto i=0; i < width*height; i++) { + indexA[i] = i; + indexB[i] = i; + } + + std::sort(indexA.begin(), indexA.end(), + [&](const unsigned int ida, const unsigned int idb) { return source[ida] < source[idb]; }); + std::sort(indexA.begin(), indexA.end(), + [&](const unsigned int ida, const unsigned int idb) { return target[ida] < target[idb]; }); + + for (auto i=0; i < width*height*nbChannels; i++) { + output[i] = source[i]; + } + + for (auto i=0; i < width; ++i) { + for (auto j=0; j < height; ++j) { + auto indexPixel = (width*j+i); + auto a = target[indexB[indexPixel]]; + output[indexA[indexPixel]] = a; + } + } + + std::random_device rd{}; + std::mt19937 gen{rd()}; + + //As an example, we just scan the pixels of the source image + //and swap the color channels. + for(auto i = 0 ; i < width ; ++i) + { + for(auto j = 0; j < height; ++j) + { + auto indexPixel = nbChannels*(width*j+i); + unsigned char r = source[ indexPixel ]; + unsigned char g = source[ indexPixel + 1]; + unsigned char b = source[ indexPixel + 2]; + //Swapping the channels + output[ indexPixel ] = b; + output[ indexPixel + 1 ] = g; + output[ indexPixel + 2 ] = r; + if (nbChannels == 4) //just copying the alpha value if any + output[ indexPixel + 3] = source[ indexPixel + 3]; + } + } + + //Final export + if (!silent) std::cout<<"Exporting.."<