hsm_test00.c
Go to the documentation of this file.
1 #include <string.h>
2 #include <pgm.h>
3 #include <options/options.h>
4 
5 #include <csm/csm_all.h>
6 
7 #include "hsm.h"
8 #include "hsm_interface.h"
9 
10 const char * banner =
11  "Reads an image, computes HT, de-computes it \n\n";
12 
13 struct {
14  const char * file_input1;
15  const char * file_input2;
16 
17  const char * prefix;
18  int debug;
19 
20  struct hsm_params hsmp;
21 
22 } p;
23 
24 hsm_buffer create_ht_for_image(struct hsm_params*p, FILE*in, const double base[3]);
25 void write_ht_on_image(hsm_buffer b, FILE*out);
26 void write_function_on_image(int n, const double*f, int rows, FILE*out);
27 
28 
29 int main(int argc, const char**argv) {
30  pgm_init(&argc, argv);
32 
33 
34  struct option* ops = options_allocate(20);
35  options_string(ops, "in1", &p.file_input1, "stdin", "Input file 1");
36  options_string(ops, "in2", &p.file_input2, "", "Input file 2");
37  options_string(ops, "out", &p.prefix, "test00", "Output file prefix ");
38 
39  hsm_add_options(ops, &p.hsmp);
40  p.hsmp.linear_cell_size = 1; /* 1 pixel */
41 
42  options_int(ops, "debug", &p.debug, 0, "Shows debug information");
43 
44  if(!options_parse_args(ops, argc, argv)) {
45  options_print_help(ops, stderr);
46  return -1;
47  }
48 
49  sm_debug_write(p.debug);
50 
51 
52  FILE * in1 = open_file_for_reading(p.file_input1); if(!in1) return -2;
53 
54  sm_debug("Computing HT for image %s.\n", p.file_input1);
55 
56  hsm_buffer b1 = create_ht_for_image(&(p.hsmp), in1, 0); if(!b1) return -3;
58 
59 
60  if(!strcmp(p.file_input2,"")) {
61  p.file_input2 = p.file_input1;
62  p.hsmp.debug_true_x_valid = 1;
63  p.hsmp.debug_true_x[0] = 20;
64  p.hsmp.debug_true_x[1] = 50;
65  p.hsmp.debug_true_x[2] = 0; /*deg2rad(40.0);*/
66  } else {
67  p.hsmp.debug_true_x_valid = 0;
68  }
69 
70  FILE * in2 = open_file_for_reading(p.file_input2); if(!in2 ) return -2;
71  sm_debug("Computing HT for image %s.\n", p.file_input2);
72  double *base = p.hsmp.debug_true_x_valid ? p.hsmp.debug_true_x : 0;
73  hsm_buffer b2 = create_ht_for_image(&(p.hsmp), in2, base); if(!b2) return -3;
75 
76 
77 
78  sm_debug("Doing scan-matching..\n");
79  p.hsmp.max_translation = max(b1->rho_max, b2->rho_max);
80 
81  hsm_match(&(p.hsmp),b1,b2);
82 
83  char filename_ht1[256]; sprintf(filename_ht1, "%s_ht1.pgm", p.prefix);
84  char filename_ht2[256]; sprintf(filename_ht2, "%s_ht2.pgm", p.prefix);
85  char filename_hs1[256]; sprintf(filename_hs1, "%s_hs1.pgm", p.prefix);
86  char filename_hs2[256]; sprintf(filename_hs2, "%s_hs2.pgm", p.prefix);
87  char filename_hs_xc[256]; sprintf(filename_hs_xc, "%s_hs_xc.pgm", p.prefix);
88 
89  FILE * file_ht1 = open_file_for_writing(filename_ht1);
90  FILE * file_ht2 = open_file_for_writing(filename_ht2);
91  FILE * file_hs1 = open_file_for_writing(filename_hs1);
92  FILE * file_hs2 = open_file_for_writing(filename_hs2);
93  FILE * file_hs_xc = open_file_for_writing(filename_hs_xc);
94 
95  if(!file_ht1 | !file_ht2) return -4;
96  if(!file_hs1 | !file_hs2) return -4;
97  if(!file_hs_xc) return -5;
98 
99  write_ht_on_image(b1,file_ht1);
100  write_ht_on_image(b2,file_ht2);
101  write_function_on_image(b1->num_angular_cells, b1->hs, 200, file_hs1);
102  write_function_on_image(b2->num_angular_cells, b2->hs, 200, file_hs2);
103  write_function_on_image(b1->num_angular_cells, b1->hs_cross_corr, 200, file_hs_xc);
104 
105 
106 }
107 
108 
109 void write_function_on_image(int n, const double*f, int rows, FILE*out) {
110  int cols = n;
111  gray ** grays = pgm_allocarray(cols, rows);
112 
113  double maxvalue=0;
114  for(int i=0;i<n;i++)
115  if(f[i]>0) /* case NAN */
116  maxvalue = GSL_MAX(maxvalue, f[i]);
117  if(maxvalue==0)maxvalue=1;
118 
119  gray maxgray = 255;
120 
121  for(int y=0;y<rows;y++)
122  for(int x=0;x<cols;x++)
123  grays[y][x] = 0;
124 
125  for(int x=0;x<cols;x++) {
126  int y = round((rows-2)*f[x]/maxvalue);
127  y = (rows-1) - y;
128  if(y>=0 && y<rows )
129  grays[y][x] = maxgray;
130  }
131 
132  pgm_writepgm(out,grays,cols,rows,maxgray,0);
133 
134  pgm_freearray(grays,rows);
135 }
136 
137 
138 void write_ht_on_image(hsm_buffer b, FILE*out) {
139  int rows = b->num_angular_cells;
140  int cols = b->num_linear_cells;
141  gray ** grays = pgm_allocarray(cols, rows);
142 
143  double maxvalue=0;
144  for(int t=0;t<b->num_angular_cells;t++)
145  for(int r=0;r<b->num_linear_cells;r++)
146  maxvalue = GSL_MAX(maxvalue, b->ht[t][r]);
147 
148  gray maxgray = 255;
149 
150  for(int t=0;t<b->num_angular_cells;t++)
151  for(int r=0;r<b->num_linear_cells;r++)
152  grays[t][r] = (gray) ceil(b->ht[t][r] * (maxgray-1) / maxvalue);
153 
154  pgm_writepgm(out,grays,cols,rows,maxgray,0);
155 
156  pgm_freearray(grays,rows);
157 }
158 
159 hsm_buffer create_ht_for_image(struct hsm_params*p, FILE*in, const double base[3]) {
160  int cols, rows; gray max;
161  gray **image = pgm_readpgm(in, &cols, &rows, &max);
162  if(!image) { return 0; }
163 
164  p->max_norm = 1.1 * hypot(cols/2.0, rows/2.0);
166 
168  if(base)
169  hsm_compute_ht_base(b, base);
170 
171  int npoints = 0;
172  for (int v=0; v<rows; v++)
173  for (int u=0; u<cols; u++) {
174  double x = u - cols/2;
175  double y = v - rows/2;
176  if(image[v][u]==0) continue;
177  hsm_compute_ht_point(b, x, y, ((double)image[v][u])/max);
178  npoints ++;
179  }
180  sm_debug("Used %d points.\n", npoints);
181  /* write the modified image to stdout */
182 /* pgm_writepgm(stdout, image, cols, rows, max, 1); */
183  pgm_freearray(image, rows);
184  return b;
185 }
void options_banner(const char *message)
Definition: options.c:33
const char * file_input2
Definition: hsm_test00.c:15
void hsm_compute_ht_base(hsm_buffer b, const double base_pose[3])
Definition: hsm.c:82
#define max(a, b)
Definition: bits.h:20
void write_function_on_image(int n, const double *f, int rows, FILE *out)
Definition: hsm_test00.c:109
hsm_buffer create_ht_for_image(struct hsm_params *p, FILE *in, const double base[3])
Definition: hsm_test00.c:159
void hsm_compute_ht_point(hsm_buffer b, double x0, double y0, double weight)
Definition: hsm.c:90
struct option * ops
Definition: rb_sm.c:31
void hsm_compute_spectrum(hsm_buffer b)
Definition: hsm.c:140
hsm_buffer hsm_buffer_alloc(struct hsm_params *p)
Definition: hsm.c:10
FILE * open_file_for_writing(const char *filename)
Definition: utils.c:25
double rho_max
Definition: hsm.h:59
Definition: options.h:49
double * hs_cross_corr
Definition: hsm.h:89
int num_angular_cells
Definition: hsm.h:50
struct hsm_params hsmp
Definition: hsm_test00.c:20
int main(int argc, const char **argv)
Definition: hsm_test00.c:29
struct option * options_allocate(int n)
void hsm_match(struct hsm_params *p, hsm_buffer b1, hsm_buffer b2)
Definition: hsm.c:156
void options_int(struct option *, const char *name, int *p, int def_value, const char *desc)
const char * file_input1
Definition: hsm_test00.c:14
const char * prefix
Definition: hsm_test00.c:17
void hsm_add_options(struct option *ops, struct hsm_params *p)
Definition: hsm_interface.c:6
void write_ht_on_image(hsm_buffer b, FILE *out)
Definition: hsm_test00.c:138
void sm_debug_write(int flag)
Definition: logging.c:16
Definition: hsm.h:7
FILE * open_file_for_reading(const char *filename)
Definition: utils.c:19
const char * banner
Definition: hsm_test00.c:10
double max_norm
Definition: hsm.h:9
int num_linear_cells
Definition: hsm.h:53
void options_print_help(struct option *options, FILE *f)
Definition: options.c:398
double ** ht
Definition: hsm.h:62
void sm_debug(const char *msg,...)
Definition: logging.c:88
double x[3]
Definition: algos.h:143
struct @4 p
void options_string(struct option *, const char *name, const char **p, const char *def_balue, const char *desc)
double * hs
Definition: hsm.h:65
int debug
Definition: hsm_test00.c:18
int options_parse_args(struct option *ops, int argc, const char *argv[])
Definition: options.c:66


csm
Author(s): Andrea Censi
autogenerated on Tue May 11 2021 02:18:23