-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
91 lines (74 loc) · 2.46 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "synth_texture.h"
#include <sstream>
#include <string>
synth_texture::synth_texture(Mat texture, Size size){
out_size = size;
raw_image = texture;
max_iter=50;
level=0;
update_level(level);
color=COLOR_RGB; // for source optimization. /or COLOR_RGB
};
Mat synth_texture::extract_neighborhood(Mat M, int h, int v){
// output as a 1xp vector
return M(Range(v-grid_step, v+grid_step), Range(h-grid_step, h+grid_step)).clone().reshape(1,1);
}
void synth_texture::synthetize(){
namedWindow( "Output", WINDOW_AUTOSIZE ); // Create windows for display
namedWindow( "Texture", WINDOW_AUTOSIZE );
moveWindow("Output", 10, 50);
moveWindow("Texture", 10, 500); waitKey(10);
// Initialize neighboroods randomly
randu(Zp, Scalar(grid_step+1,grid_step+1), Scalar(in_width-grid_step-1, in_height-grid_step-1));
int iter_at_scale=1;
for(int iter=1;iter<max_iter;++iter){
// Alternatively optimize for energy and source locations
cout << "==== iteration [" << iter << "] ===="<<endl;
minimize_energy();
imshow( "Output", out_image );imshow( "Texture", image );waitKey(1);
update_neighborhoods();
// Change scale if needed
++iter_at_scale;
double improv = abs(energy_previous-energy)/energy_previous;
double treshold = pow(10,-level-1);
cout << "...improvement: " << improv;
cout << " (next level: 10e" << -level-1 << ")" << endl;
if(improv<treshold || iter_at_scale>9){ // we refine progressively
iter_at_scale=1;
if(level>5) return;
update_level(++level);
update_neighborhoods();
}
}
};
// MAIN FUNCTION
int main(int argc, char** argv )
{
// 1. Load texture image
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR);
if(! image.data )
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
// 2. Create synthesized image
Size size = Size(image.size().width*2,image.size().height*2);
Size size = Size(256,256);
cout << "input size : " << image.size() << endl;
cout << "output size : " << size << endl;
synth_texture out = synth_texture(image,size);
out.synthetize();
// 3. Output and save result
string input_name = string(argv[1]);
string filename = "output/"+input_name;
imwrite(filename,out.out_image);
waitKey(10);
cout << "programm ended" << endl;
return 0;
}