setting tp1
This commit is contained in:
parent
01aacdd03d
commit
30a7557ead
61928
1-SlicedOptimalTransport/c++/CImg.h
Normal file
61928
1-SlicedOptimalTransport/c++/CImg.h
Normal file
File diff suppressed because it is too large
Load Diff
6778
1-SlicedOptimalTransport/c++/CLI11.hpp
Normal file
6778
1-SlicedOptimalTransport/c++/CLI11.hpp
Normal file
File diff suppressed because it is too large
Load Diff
15
1-SlicedOptimalTransport/c++/CMakeLists.txt
Normal file
15
1-SlicedOptimalTransport/c++/CMakeLists.txt
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
PROJECT(OTColorTransfer)
|
||||||
|
cmake_minimum_required(VERSION 3.1)
|
||||||
|
|
||||||
|
set (CMAKE_CXX_STANDARD 11)
|
||||||
|
|
||||||
|
set(EXAMPLES
|
||||||
|
colorTransfer
|
||||||
|
)
|
||||||
|
|
||||||
|
foreach(EXAMPLE ${EXAMPLES})
|
||||||
|
add_executable(${EXAMPLE} ${EXAMPLE}.cpp)
|
||||||
|
if(UNIX)
|
||||||
|
target_link_libraries(${EXAMPLE} -lm)
|
||||||
|
endif()
|
||||||
|
endforeach()
|
0
1-SlicedOptimalTransport/c++/README.md
Normal file
0
1-SlicedOptimalTransport/c++/README.md
Normal file
112
1-SlicedOptimalTransport/c++/colorTransfer.cpp
Normal file
112
1-SlicedOptimalTransport/c++/colorTransfer.cpp
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
/*
|
||||||
|
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 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: "<<width<<"x"<<height<<" ("<<nbChannels<<")"<< std::endl;
|
||||||
|
int width_target,height_target, nbChannels_target;
|
||||||
|
unsigned char *target = stbi_load(targetImage.c_str(), &width_target, &height_target, &nbChannels_target, 0);
|
||||||
|
if (!silent) std::cout<< "Target image: "<<width_target<<"x"<<height_target<<" ("<<nbChannels_target<<")"<< std::endl;
|
||||||
|
if ((width*height) != (width_target*height_target))
|
||||||
|
{
|
||||||
|
std::cout<< "Image sizes do not match. "<<std::endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
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.
|
||||||
|
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.."<<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);
|
||||||
|
stbi_image_free(target);
|
||||||
|
exit(0);
|
||||||
|
}
|
7547
1-SlicedOptimalTransport/c++/stb_image.h
Normal file
7547
1-SlicedOptimalTransport/c++/stb_image.h
Normal file
File diff suppressed because it is too large
Load Diff
1617
1-SlicedOptimalTransport/c++/stb_image_write.h
Normal file
1617
1-SlicedOptimalTransport/c++/stb_image_write.h
Normal file
File diff suppressed because it is too large
Load Diff
BIN
1-SlicedOptimalTransport/output.png
Normal file
BIN
1-SlicedOptimalTransport/output.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.0 MiB |
BIN
1-SlicedOptimalTransport/pexelA-0.png
Normal file
BIN
1-SlicedOptimalTransport/pexelA-0.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.0 MiB |
BIN
1-SlicedOptimalTransport/pexelB-0.png
Normal file
BIN
1-SlicedOptimalTransport/pexelB-0.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.5 MiB |
34
1-SlicedOptimalTransport/python/colorTransfer.py
Normal file
34
1-SlicedOptimalTransport/python/colorTransfer.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import numpy as np
|
||||||
|
import cv2
|
||||||
|
|
||||||
|
PATH="../"
|
||||||
|
|
||||||
|
# Load an color image
|
||||||
|
source = cv2.imread(PATH+'pexelA-0.png',cv2.IMREAD_COLOR)
|
||||||
|
print("Source image " + str(source.shape))
|
||||||
|
|
||||||
|
# Load an color image
|
||||||
|
target = cv2.imread(PATH+'pexelB-0.png',cv2.IMREAD_COLOR)
|
||||||
|
print("Target image " + str(target.shape))
|
||||||
|
|
||||||
|
cv2.imshow('source',source)
|
||||||
|
cv2.waitKey(0)
|
||||||
|
cv2.destroyAllWindows()
|
||||||
|
|
||||||
|
cv2.imshow('target',target)
|
||||||
|
cv2.waitKey(0)
|
||||||
|
cv2.destroyAllWindows()
|
||||||
|
|
||||||
|
|
||||||
|
##Swapping the channels
|
||||||
|
output = source
|
||||||
|
rows,cols,nbchannels = source.shape
|
||||||
|
|
||||||
|
for i in range(rows):
|
||||||
|
for j in range(cols):
|
||||||
|
color = source[i,j]
|
||||||
|
color2 = [color[2], color[1], color[0]]
|
||||||
|
output[i,j]=color2;
|
||||||
|
|
||||||
|
print("Saving the output")
|
||||||
|
cv2.imwrite('output.png',output)
|
Loading…
x
Reference in New Issue
Block a user