diff --git a/image-processing/2-BilateralFilter/c++/CMakeLists.txt b/image-processing/2-BilateralFilter/c++/CMakeLists.txt index 4fb83b9..3a52cd2 100644 --- a/image-processing/2-BilateralFilter/c++/CMakeLists.txt +++ b/image-processing/2-BilateralFilter/c++/CMakeLists.txt @@ -1,6 +1,7 @@ set(EXAMPLES gaussianFilter bilateralFilter + nonSeparableFilter ) foreach(EXAMPLE ${EXAMPLES}) diff --git a/image-processing/2-BilateralFilter/c++/gaussianFilter.cpp b/image-processing/2-BilateralFilter/c++/gaussianFilter.cpp index 2198f80..ed5cfd0 100644 --- a/image-processing/2-BilateralFilter/c++/gaussianFilter.cpp +++ b/image-processing/2-BilateralFilter/c++/gaussianFilter.cpp @@ -38,19 +38,71 @@ #define STB_IMAGE_WRITE_IMPLEMENTATION #include +#define PX_IDX(nbChannels, width, i, j) (nbChannels*(width*j+i)) +#define MIN(a, b) (a < b ? a : b) //Global flag to silent verbose messages bool silent; +unsigned char* applySeparableFilter(unsigned char* source, int nbChannels, int width, int height, int channel, float* filter_a, float* filter_b, int filter_size) { + float* source_a[filter_size]; + + for (int i=0; i < filter_size; i++) { + source_a[i] = (float*)malloc(sizeof(unsigned char)*width*height*nbChannels); + for (int x=0; x < width*height; x++) { + for (int ch=0; ch < MIN(3, nbChannels); ch++) { + //source_a[i][] = source[x]*filter_a[i]; + } + } + } + +} + +float* normalizationFilter(int filter_size) { + /* Simple filter: + 1/9 1/9 1/9 + 1/9 1/9 1/9 + 1/9 1/9 1/9 + */ + assert(filter_size%2 == 1); + + float* filter = (float*)malloc(sizeof(float)*filter_size*filter_size); + for (int i=0; i < filter_size*filter_size; i++) { filter[i] = 1/(float)(filter_size*filter_size); }; + return filter; +} + +float* gaussianFilter(int filter_size, float sigma) { + /* Gaussian filter */ + assert(filter_size%2 == 1); + int beg = filter_size/2; + int ci, cj; + + float* filter = (float*)malloc(sizeof(float)*filter_size*filter_size); + float sum = 0.; + for (int i=0; i < filter_size; i++) { + for (int j=0; j < filter_size; j++) { + ci = abs(i-beg); + cj = abs(j-beg); + filter[i+j*filter_size] = exp(-(ci*ci + cj*cj)/(2.0*sigma*sigma)) / (2.0*M_PI*sigma*sigma); + sum += filter[i+j*filter_size]; + } + } + for (int i=0; i < filter_size*filter_size; i++) {filter[i] = filter[i]/sum;} + + return filter; +} + int main(int argc, char **argv) { - CLI::App app{"colorTransfer"}; + CLI::App app{"gaussianFilter"}; 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(); double sigma = 3.0; app.add_option("--sigma", sigma, "Variance of the Gaussian filter"); + int filter_size = 3; + app.add_option("-f,--filter-size", filter_size, "Size of the filter"); silent = false; app.add_flag("--silent", silent, "No verbose messages"); CLI11_PARSE(app, argc, argv); @@ -63,20 +115,13 @@ int main(int argc, char **argv) //Main computation std::vector output(width*height*nbChannels); - //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; + float* filter = gaussianFilter(filter_size, sigma); + for(auto i = 0 ; i < width ; ++i) { + for(auto j = 0; j < height; ++j) { + auto indexPixel = PX_IDX(nbChannels, width, i, j); + for (auto channel=0; channel < 3; channel++) { + //output[indexPixel+channel] = applyFilter(source, nbChannels, width, height, i, j, channel, filter, filter_size); + } if (nbChannels == 4) //just copying the alpha value if any output[ indexPixel + 3] = source[ indexPixel + 3]; } @@ -91,6 +136,7 @@ int main(int argc, char **argv) exit(errcode); } + free(filter); stbi_image_free(source); exit(0); } diff --git a/image-processing/2-BilateralFilter/c++/nonSeparableFilter.cpp b/image-processing/2-BilateralFilter/c++/nonSeparableFilter.cpp new file mode 100644 index 0000000..8244b17 --- /dev/null +++ b/image-processing/2-BilateralFilter/c++/nonSeparableFilter.cpp @@ -0,0 +1,143 @@ +/* + 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 +#define MIN(a, b) (a < b ? a : b) + +int PX_IDX(int nbChannels, int width, int i, int j) {return (nbChannels*(width*j+i));} +//Global flag to silent verbose messages +bool silent; + + +char applyFilter(unsigned char* source, int nbChannels, int width, int height, int i, int j, int channel, float* filter, int filter_size) { + float out = 0.; + + int beg = filter_size/2; + + for (int loci=0; loci < filter_size; loci++) { + for(int locj=0; locj < filter_size; locj++) { + if (i+loci-beg < 0 || i+loci-beg >= width || j+locj-beg < 0 || j+locj-beg >= height) { continue; } + + out += source[PX_IDX(nbChannels, width, i+loci-beg, j+locj-beg)+channel]*filter[loci+locj*filter_size]; + } + } + return (char)out; +} + +float* normalizationFilter(int filter_size) { + /* Simple filter: + 1/9 1/9 1/9 + 1/9 1/9 1/9 + 1/9 1/9 1/9 + */ + assert(filter_size%2 == 1); + + float* filter = (float*)malloc(sizeof(float)*filter_size*filter_size); + for (int i=0; i < filter_size*filter_size; i++) { filter[i] = 1/(float)(filter_size*filter_size); }; + return filter; +} + +float* gaussianFilter(int filter_size, float sigma) { + /* Gaussian filter */ + assert(filter_size%2 == 1); + int beg = filter_size/2; + int ci, cj; + + float* filter = (float*)malloc(sizeof(float)*filter_size*filter_size); + float sum = 0.; + for (int i=0; i < filter_size; i++) { + for (int j=0; j < filter_size; j++) { + ci = abs(i-beg); + cj = abs(j-beg); + filter[i+j*filter_size] = exp(-(ci*ci + cj*cj)/(2.0*sigma*sigma)) / (2.0*M_PI*sigma*sigma); + sum += filter[i+j*filter_size]; + } + } + for (int i=0; i < filter_size*filter_size; i++) {filter[i] = filter[i]/sum;} + + return filter; +} + +int main(int argc, char **argv) +{ + CLI::App app{"nonSeparableFilter"}; + 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(); + double sigma = 3.0; + app.add_option("--sigma", sigma, "Variance of the Gaussian filter"); + int filter_size = 3; + app.add_option("-f,--filter-size", filter_size, "Size of the filter"); + 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); + + float* filter = gaussianFilter(filter_size, sigma); + for(auto i = 0 ; i < width ; ++i) { + for(auto j = 0; j < height; ++j) { + auto indexPixel = PX_IDX(nbChannels, width, i, j); + for (auto channel=0; channel < MIN(3, nbChannels); channel++) { + output[indexPixel+channel] = applyFilter(source, nbChannels, width, height, i, j, channel, filter, filter_size); + } + if (nbChannels == 4) //just copying the alpha value if any + output[ indexPixel + 3] = source[ indexPixel + 3]; + } + } + + //Final export + if (!silent) std::cout<<"Exporting.."<