void _KLTWriteAbsFloatImageToPGM( _KLT_FloatImage img, char *filename,float scale) { int npixs = img->ncols * img->nrows; float fact; float *ptr; uchar *byteimg, *ptrout; int i; float tmp; /* Allocate memory to hold converted image */ byteimg = (uchar *) malloc(npixs * sizeof(uchar)); /* Convert image from float to uchar */ fact = 255.0f / scale; ptr = img->data; ptrout = byteimg; for (i = 0 ; i < npixs ; i++) { tmp = (float) (fabs(*ptr++) * fact); if(tmp > 255.0) tmp = 255.0; *ptrout++ = (uchar) tmp; } /* Write uchar image to PGM */ pgmWriteFile(filename, byteimg, img->ncols, img->nrows); /* Free memory */ free(byteimg); }
int write_int_img_pgm(char *filename, int *gx2, int cols, int rows) { unsigned char *img; int i, j; int imax, imin; imax =0; imin =100000000; img = (unsigned char *)malloc(sizeof(char)*cols*rows); for(i = 0; i < cols*rows; ++i) { if(gx2[i] > imax) imax = gx2[i]; if(gx2[i] < imin && gx2[i] != 0) imin = gx2[i]; } for(i = 0; i < cols*rows; ++i) { img[i] = (gx2[i] - imin)*250/(imax-imin); //if(abs(gx2[i]) < 1000) //{ //img[i] = 0; //} } pgmWriteFile(filename, img, cols, rows); free(img); return 1; }
void saveIGtoPGM(int *no_igL, char *fname) { uchar *tmp; int val; int tx, ty; tmp = malloc(g_rows*(g_cols+g_slop)*sizeof(uchar)); if (tmp == NULL) error("(saveIGtoPGM) Memory not allocated"); for (ty=0 ; ty<g_rows ; ty++) { for (tx=0 ; tx<g_cols+g_slop ; tx++) { val = no_igL[(g_cols+g_slop)*ty+tx]; if (val<0) val=0; if (val>255) val=255; tmp[(g_cols+g_slop)*ty+tx] = (uchar) val; } } pgmWriteFile(fname, (uchar *) tmp, g_cols+g_slop, g_rows); free(tmp); }
void _KLTWriteFloatImageToPGM( _KLT_FloatImage img, char *filename) { int npixs = img->ncols * img->nrows; float mmax = -999999.9f, mmin = 999999.9f; float fact; float *ptr; uchar *byteimg, *ptrout; int i; /* Calculate minimum and maximum values of float image */ ptr = img->data; for (i = 0 ; i < npixs ; i++) { mmax = max(mmax, *ptr); mmin = min(mmin, *ptr); ptr++; } /* Allocate memory to hold converted image */ byteimg = (uchar *) malloc((unsigned)npixs * sizeof(uchar)); /* Convert image from float to uchar */ fact = 255.0f / (mmax-mmin); ptr = img->data; ptrout = byteimg; for (i = 0 ; i < npixs ; i++) { *ptrout++ = (uchar) ((*ptr++ - mmin) * fact); } /* Write uchar image to PGM */ pgmWriteFile(filename, byteimg, img->ncols, img->nrows); /* Free memory */ free(byteimg); }
int main(int argc, char *argv[]) { BOOL leftImageHasBeenRead = FALSE; BOOL rightImageHasBeenRead = FALSE; BOOL disparityMapHasBeenRead = FALSE; BOOL do_matching = TRUE; BOOL do_postprocessing = TRUE; int tempCols, tempRows; clock_t time1, time2, time3, time4; char *ptr_ig = NULL; int i; /* Left and right images */ uchar *imgL, *imgR; /* Results after matching the scanlines independently */ uchar *disparity_map1, *depth_discontinuities1; /* Results after postprocessing the first disparity map */ uchar *disparity_map2, *depth_discontinuities2; g_maxdisp = -1; /* Parse command line */ for (i = 1 ; i < argc ; i++) { if (strcmp(argv[i], "-h") == 0) usage(argv[0]); else if (strcmp(argv[i], "-o") == 0) { i++; if (i == argc) error("Missing argument."); setOcclusionPenalty(atoi(argv[i])); } else if (strcmp(argv[i], "-r") == 0) { i++; if (i == argc) error("Missing argument."); setReward(atoi(argv[i])); } else if (strcmp(argv[i], "-d") == 0) { i++; if (i == argc) error("Missing argument."); strcpy(dir_in, argv[i]); } else if (strcmp(argv[i], "-rel") == 0) { i++; if (i == argc) error("Missing argument."); setReliableThreshold(atoi(argv[i])); } else if (strcmp(argv[i], "-alpha") == 0) { i++; if (i == argc) error("Missing argument."); setAlpha(atof(argv[i])); } else if (strcmp(argv[i], "-ma") == 0) { i++; if (i == argc) error("Missing argument."); setMaxAttractionThreshold(atoi(argv[i])); } else if (strcmp(argv[i], "-b") == 0) writeIntermediateResults = TRUE; else if (strcmp(argv[i], "-np") == 0) do_postprocessing = FALSE; else if (strcmp(argv[i], "-jp") == 0) do_matching = FALSE; else if (strcmp(argv[i], "-wpi") == 0) writePostprocessingIntermediateResults = TRUE; else if (!leftImageHasBeenRead) { strcpy(basenameL, argv[i]); leftImageHasBeenRead = TRUE; } else if (strcmp(argv[i], "-ig") == 0) { i++; if (i == argc) error("Missing argument."); strcpy(basename_ig, argv[i]); ptr_ig = (char *) basename_ig; } else if (rightImageHasBeenRead) { g_maxdisp = atoi((argv[i])); g_slop = g_maxdisp + 1; } else if (!rightImageHasBeenRead) { strcpy(basenameR, argv[i]); rightImageHasBeenRead = TRUE; } else if (!disparityMapHasBeenRead) { strcpy(basename_dm_in, argv[i]); disparityMapHasBeenRead = TRUE; } else warning("Unknown argument."); } if (!leftImageHasBeenRead || !rightImageHasBeenRead || (!do_matching && !disparityMapHasBeenRead) || (g_maxdisp == -1) || (g_maxdisp < 14) || (g_maxdisp > 50)) usage(argv[0]); sprintf(fnameL, "%s/%s", dir_in, basenameL); sprintf(fnameR, "%s/%s", dir_in, basenameR); /* Read images (and maybe disparity map and intensity gradients) */ printf("Attempting to read %s and\n" " %s\n", fnameL, fnameR); imgL = pgmReadFile(fnameL, &g_cols, &g_rows); disparity_map1 = malloc(g_rows*g_cols*sizeof(uchar)); if (disparity_map1 == NULL) error("(main) Memory not allocated"); depth_discontinuities1 = malloc(g_rows*g_cols*sizeof(uchar)); if (depth_discontinuities1 == NULL) error("(main) Memory not allocated"); disparity_map2 = malloc(g_rows*g_cols*sizeof(uchar)); if (disparity_map2 == NULL) error("(main) Memory not allocated"); depth_discontinuities2 = malloc(g_rows*g_cols*sizeof(uchar)); if (depth_discontinuities2 == NULL) error("(main) Memory not allocated"); imgR = pgmReadFile(fnameR, &tempCols, &tempRows); if (tempCols != g_cols || tempRows != g_rows) size_error(tempCols, tempRows); printf("Images successfully read. Their size is %3d by %3d\n", g_cols, g_rows); if (!do_matching) { printf("Attempting to read %s\n", basename_dm_in); disparity_map1 = pgmReadFile(basename_dm_in, &tempCols, &tempRows); if (tempCols != g_cols || tempRows != g_rows) size_error(tempCols, tempRows); } printf("Using maximum disparity of %d\n", g_maxdisp); if (do_matching) { time1 = clock(); /* Match scanlines using dynamic programming */ printf("Matching scanlines independently ...\n"); matchScanlines(imgL, imgR, disparity_map1, depth_discontinuities1, ptr_ig); /* Check the time */ time2 = clock(); printf("Done. Independent processing took %4.1f seconds " "of processor time.\n", (((float) time2 - time1)/ CLOCKS_PER_SEC)); } if (do_postprocessing) { time3 = clock(); /* Postprocess disparity map */ printf("Postprocessing disparity map ...\n"); postprocess(imgL, imgR, disparity_map1, disparity_map2, depth_discontinuities2); /* Check the time */ time4 = clock(); printf("Done. Postprocessing took %4.1f seconds of processor time.\n", (((float) time4 - time3)/ CLOCKS_PER_SEC)); } if (do_matching && do_postprocessing) { printf("Total processor time was %4.1f seconds.\n", (((float) time4 - time1)/ CLOCKS_PER_SEC)); } /* Write results */ if (do_matching && writeIntermediateResults) { printf("Writing to %s and %s\n", fname_dm_intermediate, fname_dd_intermediate); pgmWriteFile(fname_dm_intermediate, (unsigned char *) disparity_map1, g_cols, g_rows); pgmWriteFile(fname_dd_intermediate, (unsigned char *) depth_discontinuities1, g_cols, g_rows); } if (do_postprocessing) { printf("Writing to %s and %s\n", fname_dm, fname_dd); pgmWriteFile(fname_dm, (unsigned char *) disparity_map2, g_cols, g_rows); pgmWriteFile(fname_dd, (unsigned char *) depth_discontinuities2, g_cols, g_rows); } printf("\n"); free(imgL); free(imgR); free(disparity_map1); free(depth_discontinuities1); free(disparity_map2); free(depth_discontinuities2); }