int main(int c, char *v[]) { // extract optional parameters bool folding = pick_option(&c, &v, "f", NULL); char *fname_in = pick_option(&c, &v, "i", "-"); char *fname_out = pick_option(&c, &v, "o", "-"); // process input arguments if (c != 3) { fprintf(stderr, "usage:\n\t" "%s nrho ntheta <gradient >hough\n", *v); // 0 1 2 return 1; } int nrho = atoi(v[1]); int ntheta = atoi(v[2]); // read input gradient int w, h, pd; float *gradient = iio_read_image_float_vec(fname_in, &w, &h, &pd); if (pd != 2) return fprintf(stderr, "I expect a gradient!\n"); // compute transform float *transform = malloc(nrho * ntheta * sizeof*transform); ghough(transform, nrho, ntheta, gradient, w, h, folding); // save output image iio_save_image_float_vec(fname_out, transform, nrho, ntheta, 1); // cleanup and exti free(gradient); free(transform); return 0; }
static int main_puts(int c, char **v) { int kerning = atoi(pick_option(&c, &v, "k", "0")); char *colorname = pick_option(&c, &v, "c", "000"); if (c != 5 && c != 6 && c != 7) { fprintf(stderr, "usage:\n\t" "%s font.bdf px py \"string\" [in [out]]\n", *v); // 0 1 2 3 4 5 6 return 1; } char *bdf = v[1]; int px = atoi(v[2]); int py = atoi(v[3]); char *text = v[4]; char *filename_in = c > 5 ? v[5] : "-"; char *filename_out = c > 6 ? v[6] : "-"; struct bitmap_font f; font_fill_from_bdf(&f, bdf); int w, h, pd; float *x = iio_read_image_float_vec(filename_in, &w, &h, &pd); float color[10] = {0}; if (pd == (int)strlen(colorname)) for (int i = 0; i < pd; i++) color[i] = (unsigned char)((255*(colorname[i]-'0'))/8); put_string_in_float_image(x, w,h,pd, px,py, color, kerning, &f, text); iio_save_image_float_vec(filename_out, x, w, h, pd); free(f.data); free(x); return 0; }
int main(int c, char *v[]) { bool do_invert = pick_option(&c, &v, "i", NULL); int order = atoi(pick_option(&c, &v, "o", "-3")); if (c < 4 || c > 6) return fprintf(stderr, "usage:\n\t" "%s [-i] [-o {0|1|2|-3|3|5|7}] hom w h [in [out]]\n" //0 1 2 3 4 5 "\t-i\tinvert input homography\n" "\t-o\tchose interpolation order (default -3 = bicubic)\n" , *v); double H_direct[9], H_inv[9]; read_n_doubles_from_string(H_direct, v[1], 9); invert_homography(H_inv, H_direct); double *H = do_invert ? H_inv : H_direct; int ow = atoi(v[2]); int oh = atoi(v[3]); char *filename_in = c > 4 ? v[4] : "-"; char *filename_out = c > 5 ? v[5] : "-"; int w, h, pd; float *x = iio_read_image_float_split(filename_in, &w, &h, &pd); float *y = xmalloc(ow * oh * pd * sizeof*y); int r = 0; for (int i = 0; i < pd; i++) r += shomwarp(y + i*ow*oh, ow, oh, H, x + i*w*h, w, h, order); iio_save_image_float_split(filename_out, y, ow, oh, pd); return r; }
int main_compute(int c, char *v[]) { // input arguments bool do_only_warp = pick_option(&c, &v, "w", NULL); if (do_only_warp) return main_warp(c, v); bool do_center = pick_option(&c, &v, "c", NULL); if (c != 7) { fprintf(stderr, "usage:\n\t" "%s a.png b.png Pa.txt Pb.txt in.tiff out.tiff\n", *v); // 0 1 2 3 4 5 6 return 1; } char *filename_a = v[1]; char *filename_b = v[2]; char *matrix_pa = v[3]; char *matrix_pb = v[4]; char *filename_in = v[5]; char *filename_out = v[6]; // read input images and matrices int wa, wb, wi, ha, hb, hi; float *a = iio_read_image_float(filename_a, &wa, &ha); float *b = iio_read_image_float(filename_b, &wb, &hb); float *h0 = iio_read_image_float(filename_in, &wi, &hi); double PA[8], PB[8]; read_n_doubles_from_string(PA, matrix_pa, 8); read_n_doubles_from_string(PB, matrix_pb, 8); // perform centering, if necessary if (do_center) { center_projection(PA, wa/2, ha/2); center_projection(PB, wb/2, hb/2); } // allocate space for output image float *out = xmalloc(wi * hi * sizeof*out); // run the algorithm float alpha2 = ALPHA()*ALPHA(); int niter = NITER(); int nwarps = NWARPS(); int nscales = NSCALES(); mnehs_affine_ms(out, h0, wi, hi, a, wa, ha, b, wb, hb, PA, PB, alpha2, niter, nscales); //for (int i = 0; i < nwarps; i++) //{ // mnehs_affine(out, h0, wi,hi, a,wa,ha, b,wb,hb, PA, PB, alpha2, niter); // memcpy(h0, out, wi * hi * sizeof*h0); //} // save the output image iio_save_image_float(filename_out, out, wi, hi); // cleanup and exit free(out); free(h0); free(a); free(b); return 0; }
int main(int c, char *v[]) { // read input arguments char *Hstring = pick_option(&c, &v, "h", ""); bool option_t = pick_option(&c, &v, "t", NULL); if (c != 5 && c!= 4 && c != 3) { return fprintf(stderr, "usage:\n\t%s" "width height [-h \"h1 ... h9\"] [clouds.gml [out.png]]\n", *v); // 1 2 3 4 } int out_width = atoi(v[1]); int out_height = atoi(v[2]); char *filename_clg = c > 3 ? v[3] : "-"; char *filename_out = c > 4 ? v[4] : "PNG:-"; // read input cloud file struct cloud_mask m[1]; if (option_t) read_cloud_mask_from_txt_file(m, filename_clg); else read_cloud_mask_from_gml_file(m, filename_clg); // acquire space for output image int w = out_width; int h = out_height; int *x = xmalloc(w*h*sizeof*x); for (int i = 0; i < w*h; i++) x[i] = 0; // scale the co-ordinates of the cloud if (*Hstring) { int nH; double *H = alloc_parse_doubles(9, Hstring, &nH); if (nH != 9) fail("can not read 3x3 matrix from \"%s\"", Hstring); cloud_mask_homography(m, H); free(H); } else cloud_mask_rescale(m, w, h); // draw mask over output image clouds_mask_fill(x, w, h, m); // save output image iio_save_image_int(filename_out, x, w, h); //cleanup free(x); free_cloud(m); return 0; }
int main(int c, char *v[]) { char *filename_m = pick_option(&c, &v, "m", ""); bool old_boundary = pick_option(&c, &v, "o", NULL); bool fan_boundary = pick_option(&c, &v, "f", NULL); bool Fan_boundary = pick_option(&c, &v, "F", NULL); global_parameter_p = atof(pick_option(&c, &v, "p", "NAN")); if ((c != 1 && c != 2 && c != 3) || (c>1 && !strcmp(v[1], "-h"))) { fprintf(stderr, "usage:\n\t%s [in [out]]\n", *v); // 0 1 2 return 1; } char *filename_i = c > 1 ? v[1] : "-"; char *filename_o = c > 2 ? v[2] : "-"; if (old_boundary && !fan_boundary) { fprintf(stderr, "using old boundary\n"); global_boundary_function = construct_symmetric_boundary_old; } if (fan_boundary) { fprintf(stderr, "using fancy boundary\n"); global_boundary_function = construct_symmetric_boundary_fancy; } if (Fan_boundary) { fprintf(stderr, "using Fancy boundary\n"); global_boundary_function = construct_symmetric_boundary_Fancy; } if (isfinite(global_parameter_p)) { fprintf(stderr, "using fancier boundary (p=%g)\n", global_parameter_p); global_boundary_function = construct_symmetric_boundary_fancier; } int w, h, pd; float *x = iio_read_image_float_split(filename_i, &w, &h, &pd); float *y = malloc(w*h*pd*sizeof*y); ppsmooth_split(y, x, w, h, pd); iio_save_image_float_split(filename_o, y, w, h, pd); if (*filename_m) { for (int i = 0; i < w*h*pd; i++) y[i] = x[i] - y[i]; iio_save_image_float_split(filename_m, y, w, h, pd); } return 0; }
int main_vecov(int c, char *v[]) { if (c == 2) if_help_is_requested_print_it_and_exit_the_program(v[1]); char *filename_out = pick_option(&c, &v, "o", "-"); if (c < 3) { fprintf(stderr, "usage:\n\t%s {sum|min|max|avg|weisz} [v1 ...] [-o out]\n", *v); // 0 1 2 3 return EXIT_FAILURE; } int n = c - 2; char *operation_name = v[1]; void (*f)(float*,float*,int,int) = NULL; if (0 == strcmp(operation_name, "sum")) f = float_sum; if (0 == strcmp(operation_name, "mul")) f = float_mul; if (0 == strcmp(operation_name, "prod")) f = float_mul; if (0 == strcmp(operation_name, "cprod")) f = float_cprod; if (0 == strcmp(operation_name, "cmul")) f = float_cprod; if (0 == strcmp(operation_name, "avg")) f = float_avg; if (0 == strcmp(operation_name, "min")) f = float_min; if (0 == strcmp(operation_name, "max")) f = float_max; if (0 == strcmp(operation_name, "med")) f = float_med; if (0 == strcmp(operation_name, "medi")) f = float_med; if (0 == strcmp(operation_name, "modc")) f = float_modc; if (0 == strcmp(operation_name, "weisz")) f = float_weisz; //if (0 == strcmp(operation_name, "medv")) f = float_medv; //if (0 == strcmp(operation_name, "rnd")) f = float_pick; //if (0 == strcmp(operation_name, "first")) f = float_first; if (!f) fail("unrecognized operation \"%s\"", operation_name); float *x[n]; int w[n], h[n], pd[n]; for (int i = 0; i < n; i++) x[i] = iio_read_image_float_vec(v[i+2], w + i, h + i, pd + i); for (int i = 0; i < n; i++) { if (w[i] != *w || h[i] != *h || pd[i] != *pd) fail("%dth image sizes mismatch\n", i); } int out_pd = *pd; float (*y) = xmalloc(*w * *h * out_pd * sizeof*y); #ifdef _OPENMP #pragma omp parallel for #endif for (int i = 0; i < *w * *h; i++) { float tmp[n][*pd]; int ngood = 0; for (int j = 0; j < n; j++) if (isgood(x[j]+i**pd, *pd)) { for (int k = 0; k < *pd; k++) tmp[ngood][k] = x[j][i**pd+k]; ngood += 1; } f(y + i**pd, tmp[0], *pd, ngood); } iio_write_image_float_vec(filename_out, y, *w, *h, *pd); free(y); for (int i = 0; i < n; i++) free(x[i]); return EXIT_SUCCESS; }
int main(int c, char *v[]) { int gpar = atoi(pick_option(&c, &v, "g", "1")); if (c < 4) { fprintf(stderr, "usage:\n\t%s {sum|min|max|avg|mul|med] [v1 ...] > out\n", *v); // 0 1 2 3 return EXIT_FAILURE; } int n = c - 2; char *operation_name = v[1]; float (*f)(float *,int) = NULL; if (0 == strcmp(operation_name, "sum")) f = float_sum; if (0 == strcmp(operation_name, "mul")) f = float_mul; if (0 == strcmp(operation_name, "prod")) f = float_mul; if (0 == strcmp(operation_name, "avg")) f = float_avg; if (0 == strcmp(operation_name, "min")) f = float_min; if (0 == strcmp(operation_name, "max")) f = float_max; if (0 == strcmp(operation_name, "med")) f = float_med; if (0 == strcmp(operation_name, "mod")) f = float_mod; if (0 == strcmp(operation_name, "cnt")) f = float_cnt; if (0 == strcmp(operation_name, "medi")) f = float_med; if (0 == strcmp(operation_name, "medv")) f = float_medv; if (0 == strcmp(operation_name, "rnd")) f = float_pick; if (0 == strcmp(operation_name, "first")) f = float_first; if (*operation_name == 'q') { float p = atof(1 + operation_name); f = float_percentile; f(&p, -1); } if (!f) fail("unrecognized operation \"%s\"", operation_name); bool (*isgood)(float) = NULL; if (0 == gpar) isgood = isgood_finite; if (1 == gpar) isgood = isgood_numeric; if (2 == gpar) isgood = isgood_always; if (!isgood) fail("unrecognized goodness %d", gpar); float *x[n]; int w[n], h[n]; for (int i = 0; i < n; i++) x[i] = iio_read_image_float(v[i+2], w + i, h + i); for (int i = 0; i < n; i++) { if (w[i] != *w || h[i] != *h) fail("%dth image size mismatch\n", i); } float (*y) = xmalloc(*w * *h * sizeof*y); for (int i = 0; i < *w * *h; i++) { float tmp[n]; int ngood = 0; for (int j = 0; j < n; j++) if (isgood(x[j][i])) tmp[ngood++] = x[j][i]; y[i] = f(tmp, ngood); } iio_save_image_float("-", y, *w, *h); return EXIT_SUCCESS; }
int main_warp(int c, char *v[]) { // input arguments bool do_center = pick_option(&c, &v, "c", NULL); if (c != 7) { fprintf(stderr, "usage:\n\t" "%s a.png b.png Pa.txt Pb.txt in.tiff amb.png\n", *v); // 0 1 2 3 4 5 6 return 1; } char *filename_a = v[1]; char *filename_b = v[2]; char *matrix_pa = v[3]; char *matrix_pb = v[4]; char *filename_in = v[5]; char *filename_out = v[6]; // read input images and matrices int wa, wb, wi, ha, hb, hi, pd, pdb; float *a = iio_read_image_float_vec(filename_a, &wa, &ha, &pd); float *b = iio_read_image_float_vec(filename_b, &wb, &hb, &pdb); float *h0 = iio_read_image_float(filename_in, &wi, &hi); double PA[8], PB[8]; read_n_doubles_from_string(PA, matrix_pa, 8); read_n_doubles_from_string(PB, matrix_pb, 8); if (pd != pdb) fail("input pair has different color depth"); // perform centering, if necessary if (do_center) { fprintf(stderr, "centering projection matrices\n"); center_projection(PA, wa/2, ha/2); center_projection(PB, wb/2, hb/2); } // allocate space for output image float *out = xmalloc(wi * hi * pd * sizeof*out); // run the algorithm mnehs_affine_warp(out, h0, wi,hi,pd, a,wa,ha, b,wb,hb, PA, PB); // save the output image iio_save_image_float_vec(filename_out, out, wi, hi, pd); // cleanup and exit free(out); free(h0); free(a); free(b); return 0; }
int main_fpantiff(int c, char *v[]) { TIFFSetWarningHandler(NULL);//suppress warnings // process input arguments char *filename_preview = pick_option(&c, &v, "p", ""); if (c != 2) { fprintf(stderr, "usage:\n\t%s pyrpattern\n", *v); // 0 1 return 1; } char *pyrpattern = v[1]; // read image struct pan_state e[1]; int megabytes = 100; tiff_octaves_init(e->t, pyrpattern, megabytes); e->w = 700; e->h = 500; e->infrared = 4 == e->t->i->spp; e->slog = false; e->preview = NULL; e->do_preview = false; e->a = 1; e->b = 0; if (*filename_preview) add_preview(e, filename_preview); // open window struct FTR f; if (e->preview) f = ftr_new_window(e->pw, e->ph); else f = ftr_new_window(BAD_MIN(e->w,700), BAD_MIN(e->h,500)); f.userdata = e; action_reset_zoom_and_position(&f); ftr_set_handler(&f, "key" , pan_key_handler); ftr_set_handler(&f, "button", pan_button_handler); ftr_set_handler(&f, "motion", pan_motion_handler); ftr_set_handler(&f, "expose", pan_exposer); int r = ftr_loop_run(&f); // cleanup and exit (optional) ftr_close(&f); return r; }
int main_cpu(int c, char *v[]) { // extract named options char *window_title = pick_option(&c, &v, "t", "cpu"); // process input arguments if (c != 2 && c != 1) { fprintf(stderr, "usage:\n\t%s [image]\n", *v); // 0 1 return 1; } char *filename_in = c > 1 ? v[1] : "-"; // read image struct pan_state e[1]; e->i = fancy_image_open(filename_in, "r"); e->w = e->i->w; e->h = e->i->h; // setup fonts (TODO, integrate these calls into fontu's caching stuff) e->font[0] = reformat_font(*xfont_4x6, UNPACKED); e->font[1] = reformat_font(*xfont_6x12, UNPACKED); e->font[2] = reformat_font(*xfont_7x13, UNPACKED); e->font[3] = reformat_font(*xfont_9x15, UNPACKED); e->font[4] = reformat_font(*xfont_10x20, UNPACKED); //e->font[0] = reformat_font(*xfont_5x7, UNPACKED); // open window struct FTR f = ftr_new_window(BAD_MIN(e->w,1000), BAD_MIN(e->h,800)); ftr_change_title(&f, window_title); f.userdata = e; action_reset_zoom_and_position(&f); ftr_set_handler(&f, "expose", pan_exposer); ftr_set_handler(&f, "motion", pan_motion_handler); ftr_set_handler(&f, "button", pan_button_handler); ftr_set_handler(&f, "key" , pan_key_handler); int r = ftr_loop_run(&f); // cleanup and exit (optional) for (int i = 0; i < 5; i++) free(e->font[i].data); ftr_close(&f); fancy_image_close(e->i); return r - 1; }
int main(int c, char *v[]) { // process input arguments _Bool m = pick_option(&c, &v, "m", NULL); if (c < 2 || c > 4) { fprintf(stderr, "usage:\n\t%s alpha [dem_in [dem_out]]\n", *v); // 0 1 2 3 return 1; } float alpha = atof(v[1]); char *filename_in = c > 2 ? v[2] : "-"; char *filename_out = c > 3 ? v[3] : "-"; // read input image int w, h, pd; float *x = iio_read_image_float_split(filename_in, &w, &h, &pd); if (pd != 1) { for (int i = 0; i < w*h; i++) for (int l = 1; l < pd; l++) x[i] += x[i+w*h*l]; for (int i = 0; i < w*h; i++) x[i] /= pd; } // cast the vertical shadows cast_vertical_shadows(x, w, h, alpha); // if mask is requested, create a binary mask if (m) for (int i = 0; i < w*h; i++) x[i] = 255*isnan(x[i]); // save the output image iio_save_image_float_split(filename_out, x, w, h, 1); // cleanup (unnecessary) and exit return 0; }
int main_pan(int c, char *v[]) { // process input arguments char *imask_option = pick_option(&c, &v, "m", ""); if (c > 4) { fprintf(stderr, "usage:\n\t" "%s [in.fft [out.fft [out.mask]]]\n", *v); // 0 1 2 3 return c; } char *filename_in = c > 1 ? v[1] : "-"; char *filename_out = c > 2 ? v[2] : "-"; char *filename_mask = c > 3 ? v[3] : NULL; char *filename_imask = *imask_option ? imask_option : NULL; // read image struct pan_state e[1]; int pd; e->fft = (void*)iio_read_image_float_vec(filename_in, &e->w, &e->h,&pd); if (pd != 2 && pd != 6) return fprintf(stderr, "input must be a fft (got pd=%d)\n", pd); e->pd = pd / 2; create_pyramid(e); if (filename_imask) { int mw, mh; e->mask = iio_read_image_uint8(filename_imask,&mw,&mh); if (mw != e->w || mh != e->h) return fprintf(stderr, "input mask bad size\n"); fprintf(stderr, "using input mask from file \"%s\"\n", filename_imask); } else { e->mask = malloc(e->w * e->h); memset(e->mask, 1, e->w * e->h); } // open window struct FTR f = ftr_new_window(BAD_MIN(e->w,512), BAD_MIN(e->h,512)); f.userdata = e; action_reset_zoom_and_position(&f); ftr_set_handler(&f, "expose", pan_exposer); ftr_set_handler(&f, "motion", pan_motion_handler); ftr_set_handler(&f, "button", pan_button_handler); ftr_set_handler(&f, "key" , pan_key_handler); int r = ftr_loop_run(&f); // apply computed mask for (int i = 0; i < e->w * e->h; i++) if (!e->mask[i]) for (int l = 0; l < e->pd; l++) e->fft[i*e->pd+l] = 0; // save output images iio_save_image_float_vec(filename_out, (void*)e->fft, e->w,e->h, pd); if (filename_mask) iio_save_image_uint8_vec(filename_mask, e->mask, e->w, e->h, 1); // cleanup and exit (optional) ftr_close(&f); return r; }
static int parse_options(int argc, char** argv, int *n_bins, int *n_hist, int *n_ori, int *meth_flag, float *thresh, int *verb_flag, char* label) { int isfound; char val[128]; isfound = pick_option(&argc, &argv, "ori_nbins", val); if (isfound == 1) *n_bins = atoi(val); if (isfound == -1) return EXIT_FAILURE; isfound = pick_option(&argc, &argv, "descr_nhist", val); if (isfound == 1) *n_hist = atoi(val); if (isfound == -1) return EXIT_FAILURE; isfound = pick_option(&argc, &argv, "descr_nori", val); if (isfound == 1) *n_ori = atoi(val); if (isfound == -1) return EXIT_FAILURE; isfound = pick_option(&argc, &argv, "absolute", val); if (isfound == 1){ *meth_flag = 0; *thresh = atof(val); } if (isfound == -1) return EXIT_FAILURE; isfound = pick_option(&argc, &argv, "relative", val); if (isfound == 1){ *meth_flag = 1; *thresh = atof(val); } if (isfound == -1) return EXIT_FAILURE; isfound = pick_option(&argc, &argv, "verb", val); if (isfound == 1){ *verb_flag = 1; strcpy(label, val); } if (isfound == -1) return EXIT_FAILURE; // check for unknown option call for(int i = 0; i < argc; i++){ if (argv[i][0] == '-'){ fprintf(stderr, "Fatal error: option \"-%s\" is unknown.\n", argv[i]+1); print_usage(); return EXIT_FAILURE; } } // check for input image if (argc != 3){ print_usage(); return EXIT_FAILURE; } return EXIT_SUCCESS; }
int main(int c, char *v[]) { if (c < 6 || c > 48) { help(v[0]); return 1; } // utm zone and hemisphere: true for 'N' and false for 'S' int zone; bool hem; const char *utm_string = pick_option(&c, &v, "-utm-zone", "no_utm_zone"); parse_utm_string(&zone, &hem, utm_string); // ascii flag bool ascii = pick_option(&c, &v, "-ascii", NULL); // longitude-latitude bounding box double lon_m = atof(pick_option(&c, &v, "-lon-m", "-inf")); double lon_M = atof(pick_option(&c, &v, "-lon-M", "inf")); double lat_m = atof(pick_option(&c, &v, "-lat-m", "-inf")); double lat_M = atof(pick_option(&c, &v, "-lat-M", "inf")); // x-y bounding box double col_m = atof(pick_option(&c, &v, "-col-m", "-inf")); double col_M = atof(pick_option(&c, &v, "-col-M", "inf")); double row_m = atof(pick_option(&c, &v, "-row-m", "-inf")); double row_M = atof(pick_option(&c, &v, "-row-M", "inf")); // mask on the unrectified image grid const char *msk_orig_fname = pick_option(&c, &v, "-mask-orig", ""); int msk_orig_w, msk_orig_h; float *msk_orig = iio_read_image_float(msk_orig_fname, &msk_orig_w, &msk_orig_h); // rectifying homography double href_inv[9], hsec_inv[9]; int n_hom; const char *hom_string_ref = pick_option(&c, &v, "href", ""); if (*hom_string_ref) { double *hom = alloc_parse_doubles(9, hom_string_ref, &n_hom); if (n_hom != 9) fail("can not read 3x3 matrix from \"%s\"", hom_string_ref); invert_homography(href_inv, hom); } const char *hom_string_sec = pick_option(&c, &v, "hsec", ""); if (*hom_string_sec) { double *hom = alloc_parse_doubles(9, hom_string_sec, &n_hom); if (n_hom != 9) fail("can not read 3x3 matrix from \"%s\"", hom_string_sec); invert_homography(hsec_inv, hom); } // open disp and mask input images int w, h, nch, ww, hh, pd; float *dispy; float *dispx = iio_read_image_float_split(v[2], &w, &h, &nch); if (nch > 1) dispy = dispx + w*h; else dispy = calloc(w*h, sizeof(*dispy)); float *mask = iio_read_image_float(v[3], &ww, &hh); if (w != ww || h != hh) fail("disp and mask image size mismatch\n"); // open color images if provided uint8_t *clr = NULL; if (c > 6) { clr = iio_read_image_uint8_vec(v[6], &ww, &hh, &pd); if (w != ww || h != hh) fail("disp and color image size mismatch\n"); } // read input rpc models struct rpc rpc_ref[1], rpc_sec[1]; read_rpc_file_xml(rpc_ref, v[4]); read_rpc_file_xml(rpc_sec, v[5]); // outputs double p[2], q[2], X[3]; // count number of valid pixels, and determine utm zone int npoints = 0; for (int row=0; row<h; row++) for (int col=0; col<w; col++) { int pix = row*w + col; if (!mask[pix]) continue; // compute coordinates of pix in the full reference image double a[2] = {col, row}; apply_homography(p, href_inv, a); // check that it lies in the image domain bounding box if (round(p[0]) < col_m || round(p[0]) > col_M || round(p[1]) < row_m || round(p[1]) > row_M) continue; // check that it passes the image domain mask int x = (int) round(p[0]) - col_m; int y = (int) round(p[1]) - row_m; if ((x < msk_orig_w) && (y < msk_orig_h)) if (!msk_orig[y * msk_orig_w + x]) continue; // compute (lon, lat, alt) of the 3D point float dx = dispx[pix]; float dy = dispy[pix]; double b[2] = {col + dx, row + dy}; apply_homography(q, hsec_inv, b); intersect_rays(X, p, q, rpc_ref, rpc_sec); // check with lon/lat bounding box if (X[0] < lon_m || X[0] > lon_M || X[1] < lat_m || X[1] > lat_M) continue; // if it passed all these tests then it's a valid point npoints++; // if not defined, utm zone is that of the first point if (zone < 0) utm_zone(&zone, &hem, X[1], X[0]); } // print header for ply file FILE *ply_file = fopen(v[1], "w"); write_ply_header(ply_file, ascii, npoints, zone, hem, (bool) clr, false); // loop over all the pixels of the input disp map // a 3D point is produced for each non-masked disparity for (int row=0; row<h; row++) for (int col=0; col<w; col++) { int pix = row*w + col; if (!mask[pix]) continue; // compute coordinates of pix in the full reference image double a[2] = {col, row}; apply_homography(p, href_inv, a); // check that it lies in the image domain bounding box if (round(p[0]) < col_m || round(p[0]) > col_M || round(p[1]) < row_m || round(p[1]) > row_M) continue; // check that it passes the image domain mask int x = (int) round(p[0]) - col_m; int y = (int) round(p[1]) - row_m; if ((x < msk_orig_w) && (y < msk_orig_h)) if (!msk_orig[y * msk_orig_w + x]) continue; // compute (lon, lat, alt) of the 3D point float dx = dispx[pix]; float dy = dispy[pix]; double b[2] = {col + dx, row + dy}; apply_homography(q, hsec_inv, b); intersect_rays(X, p, q, rpc_ref, rpc_sec); // check with lon/lat bounding box if (X[0] < lon_m || X[0] > lon_M || X[1] < lat_m || X[1] > lat_M) continue; // convert (lon, lat, alt) to utm utm_alt_zone(X, X[1], X[0], zone); // colorization: if greyscale, copy the grey level on each channel uint8_t rgb[3]; if (clr) { for (int k = 0; k < pd; k++) rgb[k] = clr[k + pd*pix]; for (int k = pd; k < 3; k++) rgb[k] = rgb[k-1]; } // write to ply if (ascii) { fprintf(ply_file, "%0.17g %0.17g %0.17g ", X[0], X[1], X[2]); if (clr) fprintf(ply_file, "%d %d %d", rgb[0], rgb[1], rgb[2]); fprintf(ply_file, "\n"); } else { double XX[3] = {X[0], X[1], X[2]}; fwrite(XX, sizeof(double), 3, ply_file); if (clr) { unsigned char C[3] = {rgb[0], rgb[1], rgb[2]}; fwrite(rgb, sizeof(unsigned char), 3, ply_file); } } } fclose(ply_file); return 0; }
int main(int c, char *v[]) { // process input arguments if (c < 2) { print_help(v); return 1; } // optional arguments const char *output_file = pick_option(&c, &v, "o", "stdout"); bool binary = (bool) pick_option(&c, &v, "b", NULL); bool verbose = (bool) pick_option(&c, &v, "-verbose", NULL); int max_nb_pts = atoi(pick_option(&c, &v, "-max-nb-pts", "INT_MAX")); float thresh_dog = atof(pick_option(&c, &v, "-thresh-dog", "0.0133")); int ss_noct = atoi(pick_option(&c, &v, "-scale-space-noct", "8")); int ss_nspo = atoi(pick_option(&c, &v, "-scale-space-nspo", "3")); // initialise time struct timespec ts; portable_gettime(&ts); // define the rectangular region of interest (roi) int x, y, w, h; if (c == 2) { x = 0; y = 0; } else if (c == 6) { x = atoi(v[2]); y = atoi(v[3]); w = atoi(v[4]); h = atoi(v[5]); } else { print_help(v); return 1; } // read the roi in the input image GDALDatasetH hDataset; GDALAllRegister(); hDataset = GDALOpen( v[1], GA_ReadOnly ); if( hDataset == NULL ) { fprintf(stderr, "ERROR : can't open %s\n", v[1]); } GDALRasterBandH hBand; hBand = GDALGetRasterBand( hDataset, 1 ); float *roi; roi = (float *) CPLMalloc(sizeof(float)*w*h); GDALRasterIO( hBand, GF_Read, x, y, w, h, roi, w, h, GDT_Float32, 0, 0 ); GDALClose(hBand); GDALClose(hDataset); if (verbose) print_elapsed_time(&ts, "read ROI", 35); // prepare sift parameters struct sift_parameters* p = sift_assign_default_parameters(); p->C_DoG = thresh_dog; p->n_oct = ss_noct; p->n_spo = ss_nspo; // compute sift keypoints struct sift_scalespace **ss = (struct sift_scalespace**) malloc(4 * sizeof(struct sift_scalespace*)); struct sift_keypoints **kk = (struct sift_keypoints**) malloc(6 * sizeof(struct sift_keypoints*)); for (int i = 0; i < 6; i++) kk[i] = sift_malloc_keypoints(); struct sift_keypoints* kpts = sift_anatomy(roi, w, h, p, ss, kk); if (verbose) print_elapsed_time(&ts, "run SIFT", 35); // add (x, y) offset to keypoints coordinates for (int i = 0; i < kpts->size; i++) { kpts->list[i]->x += y; // in Ives' conventions x is the row index kpts->list[i]->y += x; } if (verbose) print_elapsed_time(&ts, "add offset", 35); // write to standard output FILE *f = fopen(output_file, "w"); fprintf_keypoints(f, kpts, max_nb_pts, binary, 1); fclose(f); if (verbose) print_elapsed_time(&ts, "write output", 35); // cleanup CPLFree(roi); sift_free_keypoints(kpts); for (int i = 0; i < 6; i++) sift_free_keypoints(kk[i]); free(kk); for (int i = 0; i < 4; i++) sift_free_scalespace(ss[i]); free(ss); free(p); return 0; }