186 lines
6.3 KiB
C++
186 lines
6.3 KiB
C++
/*
|
|
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"
|
|
#include "SimpleProgressBar.hpp"
|
|
|
|
//Global flag to silent verbose messages
|
|
bool silent;
|
|
|
|
|
|
// Returns the distance and the position of the nearest non-white point
|
|
std::tuple<float, std::tuple<int, int>> nearest_neighbor(std::vector<bool> is_white, int width, int height, int px, int py) {
|
|
|
|
float min_dist = INT_MAX;
|
|
std::tuple<int, int> nearest_index;
|
|
for (auto i=0; i < height; i++) {
|
|
for (auto j=0; j < width; j++) {
|
|
if (!is_white[i*width+j]) {
|
|
int dist = (i-px)*(i-px) + (j-py)*(j-py);
|
|
if (dist < min_dist) {
|
|
min_dist = dist;
|
|
nearest_index = {i, j};
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return {min_dist, nearest_index};
|
|
}
|
|
|
|
std::tuple<float, std::tuple<int, int>> random_walk(std::vector<bool> is_white, int width, int height, int px, int py, int cur_depth, int max_depth) {
|
|
/*
|
|
* a. Set x0=x
|
|
* b. For a given point xi, compute the min distance d to ∂Ω
|
|
* c. Draw a random point xi+1 on ∂B(xi,d) (ball centered at xi with radius d)
|
|
* d. If xi+1 is close to the boundary (ϵ-close), retrieve the boundary conditions value g(xi+1)
|
|
* e. Otherwise go to step b.
|
|
*/
|
|
auto nearest = nearest_neighbor(is_white, width, height, px, py);
|
|
|
|
float distance = std::get<0>(nearest);
|
|
if (distance <= 1) {
|
|
return nearest;
|
|
} else if (max_depth < cur_depth) {
|
|
return {__FLT_MAX__, {px, py}};
|
|
}
|
|
|
|
std::tuple<int, int> near_p = std::get<1>(nearest);
|
|
|
|
float r = distance;//*std::rand()/(float)RAND_MAX;
|
|
float alpha = M_2_PI*std::rand()/(float)RAND_MAX;
|
|
|
|
int next_px = px+r*cos(alpha);
|
|
int next_py = py+r*sin(alpha);
|
|
|
|
//std::cout << "("<<px<<", "<<py<<") -"<<distance<<"-> "<<"("<<r*cos(alpha)<<", "<<r*sin(alpha)<<")" << std::endl;
|
|
|
|
return random_walk(is_white, width, height, next_px, next_py, cur_depth+1, max_depth);
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
CLI::App app{"MCMonteCarlo"};
|
|
std::string sourceImage;
|
|
app.add_option("-i,--input", sourceImage, "Source image")->required()->check(CLI::ExistingFile);;
|
|
std::string outputImage= "output.png";
|
|
app.add_option("-o,--output", outputImage, "Output image")->required();
|
|
unsigned int nbSpp = 100;
|
|
app.add_option("-n,--nbspp", nbSpp, "Number of samples");
|
|
unsigned int nbSteps = 3;
|
|
app.add_option("-s,--steps", nbSteps, "Number of steps");
|
|
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);
|
|
std::vector<bool> is_white(width*height);
|
|
|
|
for(auto i=0; i < width*height*nbChannels; ++i) {
|
|
output[i] = source[i];
|
|
}
|
|
for (auto i=0; i < height; i++) {
|
|
for (auto j=0; j < width; j++) {
|
|
is_white[i*width+j] = (
|
|
source[nbChannels*(i*width+j)] == 255
|
|
&& source[nbChannels*(i*width+j)+1] == 255
|
|
&& source[nbChannels*(i*width+j)+2] == 255
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
SimpleProgressBar::ProgressBar bar(height*width);
|
|
for (auto i=0; i < height; i++) {
|
|
for (auto j=0; j < width; j++) {
|
|
if (is_white[i*width+j]) {
|
|
/*
|
|
* a. Set x0=x
|
|
* b. For a given point xi, compute the min distance d to ∂Ω
|
|
* c. Draw a random point xi+1 on ∂B(xi,d) (ball centered at xi with radius d)
|
|
* d. If xi+1 is close to the boundary (ϵ-close), retrieve the boundary conditions value g(xi+1)
|
|
* e. Otherwise go to step b.
|
|
*/
|
|
std::vector<float> rescol(nbChannels);
|
|
for (auto ch=0; ch < nbChannels; ch++)
|
|
rescol[ch] = 0.;
|
|
|
|
for (auto k=0; k < nbSpp; k++) {
|
|
auto res = random_walk(is_white, width, height, i, j, 0, nbSteps);
|
|
float distance = std::get<0>(res);
|
|
int px = std::get<0>(std::get<1>(res));
|
|
int py = std::get<1>(std::get<1>(res));
|
|
|
|
for (auto ch=0; ch < nbChannels; ch++)
|
|
rescol[ch] += (distance == __FLT_MAX__ ? 255 : source[nbChannels*(px*width+py)+ch])/(float)nbSpp;
|
|
}
|
|
for (auto ch=0; ch < nbChannels; ch++)
|
|
output[nbChannels*(i*width+j)+ch] = rescol[ch];
|
|
}
|
|
bar.increment();
|
|
bar.print();
|
|
}
|
|
}
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
|
|
|
|
//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);
|
|
}
|