TP1
This commit is contained in:
parent
d209851b1d
commit
18d60c5b45
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
build
|
||||
out.*
|
@ -1,5 +1,8 @@
|
||||
set(EXAMPLES
|
||||
colorTransfer
|
||||
gammaCorrection
|
||||
grayscale
|
||||
equalization
|
||||
)
|
||||
|
||||
foreach(EXAMPLE ${EXAMPLES})
|
||||
|
140
image-processing/1-SlicedOptimalTransport/c++/equalization.cpp
Normal file
140
image-processing/1-SlicedOptimalTransport/c++/equalization.cpp
Normal file
@ -0,0 +1,140 @@
|
||||
/*
|
||||
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 <iostream>
|
||||
#include <string>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
//Command-line parsing
|
||||
#include <CLI11.hpp>
|
||||
|
||||
//Image filtering and I/O
|
||||
#define cimg_display 0
|
||||
#include <CImg.h>
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <stb_image.h>
|
||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||
#include <stb_image_write.h>
|
||||
|
||||
unsigned char min(unsigned char a, unsigned char b) {
|
||||
return a < b ? a : b;
|
||||
}
|
||||
unsigned char max(unsigned char a, unsigned char b) {
|
||||
return a > b ? a : b;
|
||||
}
|
||||
|
||||
//Global flag to silent verbose messages
|
||||
bool silent;
|
||||
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
CLI::App app{"colorTransfer"};
|
||||
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();
|
||||
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: "<<width<<"x"<<height<<" ("<<nbChannels<<")"<< std::endl;
|
||||
if (nbChannels < 3)
|
||||
{
|
||||
std::cout<< "Input images must be RGB images."<<std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
//Main computation
|
||||
std::vector<unsigned char> output(width*height*nbChannels);
|
||||
|
||||
//As an example, we just scan the pixels of the source image
|
||||
//and swap the color channels.
|
||||
unsigned int cum_r[255];
|
||||
unsigned int cum_g[255];
|
||||
unsigned int cum_b[255];
|
||||
for (auto i=0; i < 255; i++) {
|
||||
cum_r[i] = cum_g[i] = cum_b[i] = 0;
|
||||
}
|
||||
|
||||
for(auto i = 0 ; i < width ; ++i) {
|
||||
for(auto j = 0; j < height; ++j) {
|
||||
auto indexPixel = nbChannels*(width*j+i);
|
||||
cum_r[source[ indexPixel ]]++;
|
||||
cum_g[source[indexPixel+1]]++;
|
||||
cum_b[source[indexPixel+2]]++;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int distrib_r[255];
|
||||
unsigned int distrib_g[255];
|
||||
unsigned int distrib_b[255];
|
||||
|
||||
int total_px = width*height;
|
||||
int cur_cum_r, cur_cum_g, cur_cum_b;
|
||||
cur_cum_r, cur_cum_g, cur_cum_b = 0;
|
||||
for (auto i=0; i < 255; i++) {
|
||||
cur_cum_r += cum_r[i];
|
||||
cur_cum_g += cum_g[i];
|
||||
cur_cum_b += cum_b[i];
|
||||
distrib_r[i] = 255*cur_cum_r/total_px;
|
||||
distrib_g[i] = 255*cur_cum_g/total_px;
|
||||
distrib_b[i] = 255*cur_cum_b/total_px;
|
||||
}
|
||||
|
||||
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 ] = distrib_r[r];
|
||||
output[ indexPixel + 1 ] = distrib_g[g];
|
||||
output[ indexPixel + 2 ] = distrib_b[b];
|
||||
if (nbChannels == 4) //just copying the alpha value if any
|
||||
output[ indexPixel + 3] = source[ indexPixel + 3];
|
||||
}
|
||||
}
|
||||
|
||||
//Final export
|
||||
if (!silent) std::cout<<"Exporting.."<<std::endl;
|
||||
int errcode = stbi_write_png(outputImage.c_str(), width, height, nbChannels, output.data(), nbChannels*width);
|
||||
if (!errcode)
|
||||
{
|
||||
std::cout<<"Error while exporting the resulting image."<<std::endl;
|
||||
exit(errcode);
|
||||
}
|
||||
|
||||
stbi_image_free(source);
|
||||
exit(0);
|
||||
}
|
@ -0,0 +1,138 @@
|
||||
/*
|
||||
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 <iostream>
|
||||
#include <string>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
//Command-line parsing
|
||||
#include <CLI11.hpp>
|
||||
|
||||
//Image filtering and I/O
|
||||
#define cimg_display 0
|
||||
#include <CImg.h>
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <stb_image.h>
|
||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||
#include <stb_image_write.h>
|
||||
|
||||
unsigned char min(unsigned char a, unsigned char b) {
|
||||
return a < b ? a : b;
|
||||
}
|
||||
unsigned char max(unsigned char a, unsigned char b) {
|
||||
return a > b ? a : b;
|
||||
}
|
||||
|
||||
double gam_correct(unsigned char a, float A, float gamma) {
|
||||
return 256.0*pow(((float)a)/256.0, gamma)*A;
|
||||
}
|
||||
|
||||
//Global flag to silent verbose messages
|
||||
bool silent;
|
||||
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
CLI::App app{"colorTransfer"};
|
||||
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();
|
||||
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;
|
||||
unsigned char *source = stbi_load(sourceImage.c_str(), &width, &height, &nbChannels, 0);
|
||||
if (!silent) std::cout<< "Source image: "<<width<<"x"<<height<<" ("<<nbChannels<<")"<< std::endl;
|
||||
if (nbChannels < 3)
|
||||
{
|
||||
std::cout<< "Input images must be RGB images."<<std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
//Main computation
|
||||
std::vector<unsigned char> output(width*height*nbChannels);
|
||||
|
||||
//As an example, we just scan the pixels of the source image
|
||||
//and swap the color channels.
|
||||
|
||||
/*int min_r, min_g, min_b;
|
||||
int max_r, max_g, max_b;
|
||||
min_r, min_g, min_b = 256;
|
||||
max_r, max_g, max_b = 0;
|
||||
for (auto i=0; i < width; i++) {
|
||||
for (auto j=0; j < height; j++) {
|
||||
auto indexPixel = nbChannels*(width*j+i);
|
||||
min_r = min(source[indexPixel], min_r);
|
||||
min_g = min(source[indexPixel], min_g);
|
||||
min_b = min(source[indexPixel], min_b);
|
||||
|
||||
max_r = max(source[indexPixel], max_r);
|
||||
max_g = max(source[indexPixel], max_g);
|
||||
max_b = max(source[indexPixel], max_b);
|
||||
}
|
||||
}
|
||||
min_r = gam_correct(min_r, A, gamma);
|
||||
min_g = gam_correct(min_g, A, gamma);
|
||||
min_b = gam_correct(min_b, A, gamma);
|
||||
max_r = gam_correct(max_r, A, gamma);
|
||||
max_g = gam_correct(max_g, A, gamma);
|
||||
max_b = gam_correct(max_b, A, gamma);*/
|
||||
|
||||
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 ] = gam_correct(r, A, gamma);
|
||||
output[ indexPixel + 1 ] = gam_correct(g, A, gamma);
|
||||
output[ indexPixel + 2 ] = gam_correct(b, A, gamma);
|
||||
if (nbChannels == 4) //just copying the alpha value if any
|
||||
output[ indexPixel + 3] = source[ indexPixel + 3];
|
||||
}
|
||||
}
|
||||
|
||||
//Final export
|
||||
if (!silent) std::cout<<"Exporting.."<<std::endl;
|
||||
int errcode = stbi_write_png(outputImage.c_str(), width, height, nbChannels, output.data(), nbChannels*width);
|
||||
if (!errcode)
|
||||
{
|
||||
std::cout<<"Error while exporting the resulting image."<<std::endl;
|
||||
exit(errcode);
|
||||
}
|
||||
|
||||
stbi_image_free(source);
|
||||
exit(0);
|
||||
}
|
98
image-processing/1-SlicedOptimalTransport/c++/grayscale.cpp
Normal file
98
image-processing/1-SlicedOptimalTransport/c++/grayscale.cpp
Normal file
@ -0,0 +1,98 @@
|
||||
/*
|
||||
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 <iostream>
|
||||
#include <string>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
//Command-line parsing
|
||||
#include <CLI11.hpp>
|
||||
|
||||
//Image filtering and I/O
|
||||
#define cimg_display 0
|
||||
#include <CImg.h>
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <stb_image.h>
|
||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||
#include <stb_image_write.h>
|
||||
|
||||
//Global flag to silent verbose messages
|
||||
bool silent;
|
||||
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
CLI::App app{"colorTransfer"};
|
||||
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();
|
||||
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: "<<width<<"x"<<height<<" ("<<nbChannels<<")"<< std::endl;
|
||||
if (nbChannels < 3)
|
||||
{
|
||||
std::cout<< "Input images must be RGB images."<<std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
//Main computation
|
||||
std::vector<unsigned char> output(width*height);
|
||||
|
||||
// to grayscale
|
||||
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];
|
||||
|
||||
unsigned char avg = (0.21*r+0.72*g+0.07*b);
|
||||
//Swapping the channels
|
||||
output[ width*j+i ] = avg;
|
||||
}
|
||||
}
|
||||
|
||||
//Final export
|
||||
if (!silent) std::cout<<"Exporting.."<<std::endl;
|
||||
int errcode = stbi_write_png(outputImage.c_str(), width, height, 1, output.data(), width);
|
||||
if (!errcode)
|
||||
{
|
||||
std::cout<<"Error while exporting the resulting image."<<std::endl;
|
||||
exit(errcode);
|
||||
}
|
||||
|
||||
stbi_image_free(source);
|
||||
exit(0);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user