Newer
Older
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Copyright © 2012, Ignacio Ramírez Paulino <nacho@fing.edu.uy>
* All rights reserved.
*
* This file is part of COSMOS.
*
* COSMOS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* COSMOS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with COSMOS. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file cosmos_mask_test.cpp
* Test masked patch extraction routine
*/
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include "cosmos_image_io.h" // image reading/writing
#include "cosmos_matrix_io.h" // load the dictionary
#include "cosmos_image.h"
#include "cosmos_command_line.h"
#include "cosmos_debug.h"
#include "cosmos_display.h"
#include "cosmos_stats.h"
#include <ctime>
typedef double real_t; ///< type used to represent real values
/**
* Coordinate Descent based coding test
*/
int main ( int argc, const char* argv[] ) {
std::ofstream out;
int debug = cosmos_get_debug_level();
//
//--------------------------------------------------
// parse arguments:
//--------------------------------------------------
//
char const* image = "../../data/img/barbara.pgm";
int w = 32;
//
// training images directory
//
cosmos_cli_params cli_params ( 20 );
cli_params.add ( "image",parse_string,&image );
cli_params.add ( "w",parse_int,&w );
cli_params.add ( "debug",parse_int,&debug );
if ( !cli_params.parse ( argc,argv ) ) {
exit ( 1 );
}
//
// random number generator
//
cosmos_init_rng ( RNG_DEFAULT_SEED );
cosmos_set_debug_level ( debug );
int w2 = w/2;
//
// load image
//
sc_matrix<real_t> I;
sc_matrix<real_t> I2;
cosmos_pgm_read ( image,I );
std::cout << "mi=" << I.m << " ni=" << I.n << std::endl;
//
// decompose
//
cosmos_grid G;
sc_matrix<real_t> X;
cosmos_uniform_grid ( w2, I.n-w2, w2, I.m-w2, w, w, G );
//
// original get_patches function, no fancy effect
//
std::cout << "Original get_patches." << std::endl;
cosmos_get_patches ( I, w, G, X );
cosmos_remove_dc ( X );
I2.free();
cosmos_dict_display ( X,I2,3,1 );
cosmos_write_ascii_matrix ( X,"X.ascii" );
cosmos_pgm_write ( I2,"X.pgm" );
//
// extract with Gaussian Mask
//
std::cout << "Gaussian mask in get_patches_advanced" << std::endl;
sc_matrix<real_t> W;
sc_matrix<short> B;
B.n = B.m = 0;
cosmos_gaussian_mask ( w, double ( w ) /2.0, W );
cosmos_write_ascii_matrix ( W,"W.ascii" );
cosmos_get_patches_advanced ( I, w, G, W, B, X );
cosmos_remove_dc ( X );
cosmos_dict_display ( X,I2,3,1 );
cosmos_write_ascii_matrix ( X,"X_gauss.ascii" );
cosmos_pgm_write ( I2,"X_gauss.pgm" );
//
// disk + weights
//
cosmos_disk_mask ( w,B );
cosmos_write_ascii_matrix ( B,"B.ascii" );
cosmos_get_patches_advanced ( I, w, G, W, B, X );
cosmos_remove_dc ( X );
cosmos_write_ascii_matrix ( X,"X_gauss_disk.ascii" );
cosmos_dict_display ( X,B,I2,3,1 );
cosmos_pgm_write ( I2,"X_gauss_disk.pgm" );
//
// laplacian of I, disk + weioghts
//
cosmos_laplacian ( I,I2 );
cosmos_get_patches_advanced ( I2, w, G, W, B, X );
cosmos_remove_dc ( X );
cosmos_write_ascii_matrix ( X,"X_lap_gauss_disk.ascii" );
cosmos_dict_display ( X,B,I2,3,1 );
cosmos_pgm_write ( I2,"X_lap_gauss_disk.pgm" );
//
// extract disk shaped patches
//
sc_scale ( 255.0,W );
cosmos_pgm_write ( W,"W.pgm" );
W.free();
W.n = 0;
W.m = 0;
cosmos_get_patches_advanced ( I, w, G, W, B, X );
cosmos_remove_dc ( X );
cosmos_write_ascii_matrix ( X,"X_disk.ascii" );
cosmos_dict_display ( X,B,I2,3,1 );
sc_scale ( short ( 255 ),B );
cosmos_pgm_write ( B,"B.pgm" );
cosmos_pgm_write ( I2,"X_disk.pgm" );
B.free();
W.free();
I.free();
X.free();
}