/* Test of dsm_segment2 */ /* Last edited on 2016-04-09 20:11:44 by stolfilocal */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include /* INTERNAL PROTOTYPES */ void test_segment2_shadow_analysis(const char *in_prefix, const char *out_prefix); /* Analysis the effect of shadows on road texture and color. */ void test_segment2_discrepancy(const char *in_prefix, const char *out_prefix); /* Analysis the discrepancy formula. */ void test_segment2_separation(const char *in_prefix, const char *out_prefix); /* Checks whether the basic separation iteration is working. */ void test_segment2_read_full_image(dsm_image_t *full, const char *inprefix, const char *tag, int iframe); void test_segment2_read_pgm ( dsm_image_t *img, int reduc, const char *in_prefix, const char *tag, int iframe, dsm_image_t *full ); void test_segment2_compare_bg_frames ( const char *in_prefix, int frame0, int frame1, int crop_dx, int crop_dy, int crop_nx, int crop_ny, int rhi, /* Blur radius for high freq removal. */ int rlo, /* Blur radius for low freq removal. */ const char *out_prefix ); /* IMPLEMENTATIONS */ int main(int argc, char**argv) { fprintf(stderr, "starting\n"); const char *in_prefix = "in/test1/t"; const char *out_prefix = "out/test1/t"; test_segment2_shadow_analysis(in_prefix, out_prefix); test_segment2_discrepancy(in_prefix, out_prefix); return 0; test_segment2_separation(in_prefix, out_prefix); return 0; } void test_segment2_shadow_analysis(const char *in_prefix, const char *out_prefix) { test_segment2_compare_shadowed_pixels (in_prefix, 40, 1044, 500,200, 1000,650, 4, 24, out_prefix); } void test_segment2_discrepancy(const char *in_prefix, const char *out_prefix) { test_segment2_compare_bg_frames (in_prefix, 40, 41, 500,200, 1000,650, 4, 24, out_prefix); test_segment2_compare_bg_frames (in_prefix, 40, 1044, 500,200, 1000,650, 4, 24, out_prefix); test_segment2_compare_bg_frames (in_prefix, 40, 498, 500,200, 1000,650, 4, 24, out_prefix); } void test_segment2_separation(const char *in_prefix, const char *out_prefix) { int reduc = 4; dsm_image_t full; full.p = NULL; /* Read buffer for full-size video frame. */ /* Get the initial reference background: */ dsm_image_t bg0; /* Reduced background frame */ bg0.p = NULL; /* Let the reader allocate. */ test_segment2_read_pgm(&(bg0), reduc, in_prefix, "bg0", -1, &full); int nx = bg0.nx; int ny = bg0.ny; /* Segmentation module state: */ fprintf(stderr, "initializing segmentation module state\n"); dsm_segment2_state_t st; dsm_segment2_state_initialize(&st, nx, ny, &bg0); dsm_image_t img; img.p = NULL; /* Reduced video frame. */ int frame0 = 100; int frame1 = 200; for (int frame = frame0; frame <= frame1; frame++) { char *debug_prefix = NULL; { int res = asprintf(&debug_prefix, "%s_%06d", out_prefix, frame); assert(res > 0); } test_segment2_read_pgm(&img, reduc, in_prefix, "frame", frame, &full); fprintf(stderr, "identifying background on frame %06d\n", frame); dsm_segment2_identify_background(&img, &st, debug_prefix); fprintf(stderr, "frame %06d mu = %.3f\n", frame, st.mu); } } void test_segment2_read_full_image(dsm_image_t *full, const char *in_prefix, const char *tag, int iframe) { char *fname = NULL; if (iframe >= 0) { asprintf(&fname, "%s_%s_%06d.pgm", in_prefix, tag, iframe); } else { asprintf(&fname, "%s_%s.pgm", in_prefix, tag); } /* Reading full-size image: */ fprintf(stderr, "%s: reading %s\n", __FUNCTION__, fname); FILE *rd = fopen(fname, "r"); assert(rd != NULL); dsm_image_read_pgm_raw(rd, full); fclose(rd); free(fname); } void test_segment2_read_pgm ( dsm_image_t *img, int reduc, const char *in_prefix, const char *tag, int iframe, dsm_image_t *full ) { /* Read full image: */ test_segment2_read_full_image(full, in_prefix, tag, iframe); /* Shrink image: */ int winsize = 2*(2*reduc) + 1; dsm_image_realloc(img, full->nx/reduc, full->ny/reduc, 0); dsm_image_shrink(full, reduc, reduc, winsize, winsize, img); } void test_segment2_compare_bg_frames ( const char *in_prefix, int frame0, int frame1, int crop_dx, int crop_dy, int crop_nx, int crop_ny, int rhi, int rlo, const char *out_prefix ) { int frame[2] = { frame0, frame1 }; dsm_image_t full[2]; dsm_image_t crop[2]; /* Cropped and bandpass filtered */ dsm_image_t craw = dsm_image_alloc(crop_nx, crop_ny, 0); /* Cropped raw. */ dsm_image_t crlo = dsm_image_alloc(crop_nx, crop_ny, 0); /* Cropped blurred {rlo}. */ dsm_image_t crhi = dsm_image_alloc(crop_nx, crop_ny, 0); /* Cropped blurred {rhi}. */ for (int k = 0; k < 2; k++) { /* Read full frame0: */ full[k].p = NULL; /* Let the reader alloc. */ test_segment2_read_full_image(&(full[k]), in_prefix, "frame", frame[k]); /* Crop the portion without moving stuff or shadows: */ dsm_image_crop(&(full[k]), crop_dx, crop_dy, &craw); /* Apply the blur filters for the two low-pass images: */ dsm_image_blur(&craw, rlo, rlo, &crlo); dsm_image_debug(&crlo, out_prefix, frame[k], -1, "0_lopass_rlo"); dsm_image_blur(&craw, rhi, rhi, &crhi); dsm_image_debug(&crhi, out_prefix, frame[k], -1, "1_lopass_rhi"); /* Compute the difference to get the band-pass image: */ crop[k] = dsm_image_alloc(crop_nx, crop_ny, 0); dsm_image_difference(&crhi, &crlo, 1.0, &(crop[k])); dsm_image_debug(&(crop[k]), out_prefix, frame[k], -1, "2_bandpass"); } /* Compare the two cropped and bandpass filtered images: */ dsm_image_t diff = dsm_image_alloc(crop_nx, crop_ny, 0); dsm_image_difference(&(crop[0]), &(crop[1]), 30.0, &(diff)); char *tag = NULL; asprintf(&tag, "diff_%06d_%06d", frame0, frame1); dsm_image_debug(&(diff), out_prefix, -1, -1, tag); for (int k = 0; k < 2; k++) { free(full[k].p); free(crop[k].p); } free(diff.p); free(craw.p); free(crlo.p); free(crhi.p); free(tag); }