hsm_test00.c
Go to the documentation of this file.
00001 #include <string.h>
00002 #include <pgm.h>
00003 #include <options/options.h>
00004 
00005 #include <csm/csm_all.h>
00006 
00007 #include "hsm.h"
00008 #include "hsm_interface.h"
00009 
00010 const char * banner = 
00011         "Reads an image, computes HT, de-computes it \n\n";
00012 
00013 struct {
00014         const char * file_input1;
00015         const char * file_input2;
00016         
00017         const char * prefix;
00018         int debug;
00019         
00020         struct hsm_params hsmp;
00021         
00022 } p;
00023 
00024 hsm_buffer create_ht_for_image(struct hsm_params*p, FILE*in, const double base[3]);
00025 void write_ht_on_image(hsm_buffer b, FILE*out);
00026 void write_function_on_image(int n, const double*f, int rows, FILE*out);
00027 
00028 
00029 int main(int argc, const char**argv) {
00030         pgm_init(&argc, argv);
00031         options_banner(banner);
00032         
00033         
00034         struct option* ops = options_allocate(20);
00035         options_string(ops, "in1", &p.file_input1, "stdin", "Input file 1");
00036         options_string(ops, "in2", &p.file_input2, "", "Input file 2");
00037         options_string(ops, "out", &p.prefix, "test00", "Output file prefix ");
00038         
00039         hsm_add_options(ops, &p.hsmp);
00040         p.hsmp.linear_cell_size = 1; /* 1 pixel */
00041                 
00042         options_int(ops, "debug", &p.debug, 0, "Shows debug information");
00043         
00044         if(!options_parse_args(ops, argc, argv)) {
00045                 options_print_help(ops, stderr);
00046                 return -1;
00047         }
00048         
00049         sm_debug_write(p.debug);
00050 
00051 
00052         FILE * in1 = open_file_for_reading(p.file_input1);   if(!in1) return -2;
00053 
00054         sm_debug("Computing HT for image %s.\n", p.file_input1);
00055 
00056         hsm_buffer b1 = create_ht_for_image(&(p.hsmp), in1, 0);  if(!b1) return -3;
00057         hsm_compute_spectrum(b1);
00058 
00059 
00060         if(!strcmp(p.file_input2,"")) {
00061                 p.file_input2 = p.file_input1;
00062                 p.hsmp.debug_true_x_valid = 1;
00063                 p.hsmp.debug_true_x[0] = 20;
00064                 p.hsmp.debug_true_x[1] = 50;
00065                 p.hsmp.debug_true_x[2] = 0; /*deg2rad(40.0);*/
00066         } else {
00067                 p.hsmp.debug_true_x_valid = 0;
00068         }
00069 
00070         FILE * in2 = open_file_for_reading(p.file_input2);       if(!in2 ) return -2;
00071         sm_debug("Computing HT for image %s.\n", p.file_input2);
00072         double *base = p.hsmp.debug_true_x_valid ? p.hsmp.debug_true_x : 0;
00073         hsm_buffer b2 = create_ht_for_image(&(p.hsmp), in2, base);     if(!b2) return -3;       
00074         hsm_compute_spectrum(b2);
00075         
00076         
00077         
00078         sm_debug("Doing scan-matching..\n"); 
00079         p.hsmp.max_translation = max(b1->rho_max, b2->rho_max);
00080         
00081         hsm_match(&(p.hsmp),b1,b2);
00082 
00083         char filename_ht1[256]; sprintf(filename_ht1, "%s_ht1.pgm", p.prefix);
00084         char filename_ht2[256]; sprintf(filename_ht2, "%s_ht2.pgm", p.prefix);
00085         char filename_hs1[256]; sprintf(filename_hs1, "%s_hs1.pgm", p.prefix);
00086         char filename_hs2[256]; sprintf(filename_hs2, "%s_hs2.pgm", p.prefix);
00087         char filename_hs_xc[256]; sprintf(filename_hs_xc, "%s_hs_xc.pgm", p.prefix);
00088         
00089         FILE * file_ht1 = open_file_for_writing(filename_ht1);
00090         FILE * file_ht2 = open_file_for_writing(filename_ht2);
00091         FILE * file_hs1 = open_file_for_writing(filename_hs1);
00092         FILE * file_hs2 = open_file_for_writing(filename_hs2);
00093         FILE * file_hs_xc = open_file_for_writing(filename_hs_xc);
00094         
00095         if(!file_ht1 | !file_ht2) return -4;
00096         if(!file_hs1 | !file_hs2) return -4;
00097         if(!file_hs_xc) return -5;
00098         
00099         write_ht_on_image(b1,file_ht1);
00100         write_ht_on_image(b2,file_ht2);
00101         write_function_on_image(b1->num_angular_cells, b1->hs, 200, file_hs1);
00102         write_function_on_image(b2->num_angular_cells, b2->hs, 200, file_hs2);
00103         write_function_on_image(b1->num_angular_cells, b1->hs_cross_corr, 200, file_hs_xc);
00104         
00105 
00106 }
00107 
00108 
00109 void write_function_on_image(int n, const double*f, int rows, FILE*out) {       
00110         int cols = n;
00111         gray ** grays = pgm_allocarray(cols, rows);
00112         
00113         double maxvalue=0;
00114         for(int i=0;i<n;i++)
00115                 if(f[i]>0) /* case NAN */
00116                 maxvalue = GSL_MAX(maxvalue, f[i]);
00117         if(maxvalue==0)maxvalue=1;
00118 
00119         gray maxgray = 255;
00120 
00121         for(int y=0;y<rows;y++)
00122         for(int x=0;x<cols;x++)
00123         grays[y][x] = 0;
00124         
00125         for(int x=0;x<cols;x++) {
00126                 int y =  round((rows-2)*f[x]/maxvalue);
00127                 y = (rows-1) - y;
00128                 if(y>=0 && y<rows )
00129                 grays[y][x] = maxgray;
00130         }
00131         
00132         pgm_writepgm(out,grays,cols,rows,maxgray,0);
00133         
00134         pgm_freearray(grays,rows);
00135 }
00136 
00137 
00138 void write_ht_on_image(hsm_buffer b, FILE*out) {        
00139         int rows = b->num_angular_cells;
00140         int cols = b->num_linear_cells;
00141         gray ** grays = pgm_allocarray(cols, rows);
00142         
00143         double maxvalue=0;
00144         for(int t=0;t<b->num_angular_cells;t++)
00145         for(int r=0;r<b->num_linear_cells;r++)
00146         maxvalue = GSL_MAX(maxvalue, b->ht[t][r]);
00147 
00148         gray maxgray = 255;
00149 
00150         for(int t=0;t<b->num_angular_cells;t++)
00151         for(int r=0;r<b->num_linear_cells;r++)
00152         grays[t][r] = (gray) ceil(b->ht[t][r] * (maxgray-1) / maxvalue);
00153         
00154         pgm_writepgm(out,grays,cols,rows,maxgray,0);
00155         
00156         pgm_freearray(grays,rows);
00157 }
00158 
00159 hsm_buffer create_ht_for_image(struct hsm_params*p, FILE*in, const double base[3]) {
00160         int cols, rows; gray max;
00161         gray **image = pgm_readpgm(in, &cols, &rows, &max);
00162         if(!image) { return 0; }
00163 
00164         p->max_norm = 1.1 * hypot(cols/2.0, rows/2.0);
00165         hsm_buffer b = hsm_buffer_alloc(p);
00166 
00168         if(base)
00169                 hsm_compute_ht_base(b, base);
00170         
00171         int npoints = 0;
00172    for (int v=0; v<rows; v++)
00173         for (int u=0; u<cols; u++) {
00174                 double x = u - cols/2;
00175                 double y = v - rows/2;
00176                 if(image[v][u]==0) continue;
00177                 hsm_compute_ht_point(b, x, y, ((double)image[v][u])/max);
00178                 npoints ++;
00179         }
00180         sm_debug("Used %d points.\n", npoints);
00181     /* write the modified image to stdout */
00182 /*    pgm_writepgm(stdout, image, cols, rows, max, 1); */
00183     pgm_freearray(image, rows);
00184         return b;
00185 }


csm
Author(s): Andrea Censi
autogenerated on Fri May 17 2019 02:28:33