void save_files() { char fn1[255], fn2[255]; FILE *f1, *f2; gray **image1, **image2; image1 = pgm_allocarray(wid, hgt); image2 = pgm_allocarray(wid, hgt); for (int i=0; i<wid*hgt; i++) { image1[i/wid][i%wid] = (gray) data1[i]; image2[i/wid][i%wid] = (gray) data2[i]; } snprintf(fn1, 256, "%s_%+.1f_.pgm", fname1, angle_value->value()); snprintf(fn2, 256, "%s_%+.1f_.pgm", fname2, angle_value->value()); f1 = fopen(fn1, "w"); f2 = fopen(fn2, "w"); pgm_writepgm(f1, image1, wid, hgt, 65535, false); pgm_writepgm(f2, image2, wid, hgt, 65535, false); fclose(f1); fclose(f2); pgm_freearray(image1, hgt); pgm_freearray(image2, hgt); fl_beep(FL_BEEP_MESSAGE); }
int main(int argc, char *argv[]) { int saca_pgm(); FILE *fd; FILE *fd1; int cols, rows, i, j, bucle, carpeta, nresultados; float carac[11]; gray maxval; gray **imagen; gray **imagen1; struct dirent **resultados=NULL; // inicializamos todas las variables pgm_init(&argc, argv); nresultados=0; printf("Haciendo magia...\n"); fd = fopen(filepatron, "r"); if (fd == NULL ) { pm_error("error: no puedo abrir los ficheros"); exit(-1); } imagen = pgm_readpgm(fd, &cols, &rows, &maxval); if (imgpatron == NULL ) { pm_error("error: no puedo leer las imagenes"); exit(-1); } // abrimos el fichero de imagen en modo lectura fd1 = fopen(argv[1], "r"); if (fd1 == NULL ) { pm_error("error: no puedo abrir los ficheros"); exit(-1); } // leemos la imagen imagen1 = pgm_readpgm(fd1, &cols, &rows, &maxval); if (imagen == NULL ) { pm_error("error: no puedo leer las imagenes"); exit(-1); } //*******************************************************************// //en 'imagen' tenemos la foto a analizar para el fichero clasificador// //*******************************************************************// for(i=0; i<rows;i++){ for(j=0; j<cols; j++){ //Retiramos fondo por division imagen[i][j] = MIN(255, 255*((float)imagen[i][j]/(float)imgpatron[i][j]) ); //Aplicamos umbral if(imagen[i][j] <= 210) imagen[i][j] = 0; else imagen[i][j] = 255; } } // <----- falta erosionar y dilatar!!! /* dilate */ pgm_dilate(imagen, cols, rows, 2); /* erode */ pgm_erode(imagen, cols, rows, 2); pgm_writepgm(stdout, imagen, cols, rows, 255, 0); fclose(fd); //Liberamos la memoria de la imagen, para cargar otra pgm_freearray(imagen, rows); printf("Fin de la magia...\n"); fclose(patron); exit(0); }
int main(int argc, char *argv[]) { FILE *fp, *fp_out; gray maxval; int format; int width = 0; int height = 0; int i, j; gray **pgm_data; int threshold = 127; int c; extern char *optarg; extern int optind; while((c = getopt(argc, argv, "t:")) != EOF) { switch( c ) { case 't': sscanf(optarg, "%d", &threshold); break; } } // printf("argc: %d\n", argc); // printf("optind: %d\n", optind); // printf("threshold: %d\n", threshold); // optind -- index in argv of the first argv-element that is not an option if((argc - optind) != 2) { fprintf(stderr, "Usage error\n"); fprintf(stderr, "Usage: %s <input image> <output image>\n", argv[0]); fprintf(stderr, "Option:\n"); fprintf(stderr, "-t <threshold>\n"); exit(1); } // all PGM programs must call pgm_init() just after invocation, // before they process their arguments. pgm_init(&argc, argv); // PBM function for reading, which is almost equivalent // to f = fopen(filename, "rb"); fp = pm_openr(argv[optind]); // read the PGM image header pgm_readpgminit(fp, &width, &height, &maxval, &format); printf("Succesfully read the header!\n\n"); printf("= PGM image information =\n"); printf("Width: %d\n", width); printf("Height: %d\n", height); printf("Max color: %d\n", maxval); // printf("Format: %c\n\n", PGM_FORMAT_TYPE(format)); // close then open file again for reading data fclose(fp); fp = pm_openr(argv[optind]); pgm_data = pgm_readpgm(fp, &width, &height, &maxval); printf("Succesfully get the PGM image data!\n\n"); tImage img; img.width = width; img.height = height; img.pixelType = GRAY8; img.pPixel = (UCHAR *)malloc(width * height * sizeof(UCHAR)); // copy data to our format for(i = 0; i < height; i++) { for(j = 0; j < width; j++) { img.pPixel[i * width + j] = pgm_data[i][j]; } } img = binarize(img, threshold); // convert back to the pgm format for(i = 0; i < height; i++) { for(j = 0; j < width; j++) { pgm_data[i][j] = img.pPixel[i * width + j]; } } fp_out = pm_openw(argv[optind + 1]); // forceplain is a logical value that tells pgm_writepgminit() to // write a header for a plain PGM format file, as opposed to a raw // PGM format file. // 1 -> not a binary format int forceplain = 0; pgm_writepgm(fp_out, pgm_data, width, height, maxval, forceplain); printf("Succesfully write the binarized PGM image to disk!\n\n"); free(img.pPixel); fclose(fp); fclose(fp_out); return 1; }
int main(int argc, const char ** argv) { FILE * ifP; gray ** grays; int argn; unsigned int rows, cols; unsigned int divisor; unsigned int * obuf; /* malloced */ const char * const usage = "[-d <val>] height width [asciifile]"; pm_proginit(&argc, argv); rows = 0; /* initial value */ cols = 0; /* initial value */ divisor = 1; /* initial value */ argn = 1; if ( argc < 3 || argc > 6 ) pm_usage( usage ); if ( argv[argn][0] == '-' ) { if ( streq( argv[argn], "-d" ) ) { if ( argc == argn + 1 ) pm_usage( usage ); if ( sscanf( argv[argn+1], "%u", &divisor ) != 1 ) pm_usage( usage ); argn += 2; } else pm_usage( usage ); } if ( sscanf( argv[argn++], "%u", &rows ) != 1 ) pm_usage( usage ); if ( sscanf( argv[argn++], "%u", &cols ) != 1 ) pm_usage( usage ); if ( rows < 1 ) pm_error( "height is less than 1" ); if ( cols < 1 ) pm_error( "width is less than 1" ); if ( argc > argn + 1 ) pm_usage( usage ); if ( argc == argn + 1 ) ifP = pm_openr(argv[argn]); else ifP = stdin; MALLOCARRAY(obuf, cols); if (obuf == NULL) pm_error("Unable to allocate memory for %u columns", cols); grays = pgm_allocarray(cols, rows); convertAsciiToPgm(ifP, cols, rows, divisor, maxval, grays, obuf); pm_close(ifP); pgm_writepgm(stdout, grays, cols, rows, maxval, 0); free(obuf); pgm_freearray(grays, rows); return 0; }
/** write a scrap of the data, * return the filename, * if the file exist do nothing * argument fname is optional * the coordinates of the image part are geodata e.g. Gauss Krueger **/ QString GeoImage::part(float west, float north, float east, float south, QString fname) { if (fname.isEmpty()) { //create output filname Q_ASSERT(contains("key")); QString dir = CleanUp::getTmpDir(); fname.sprintf("%s/%s_%f_%f_%f_%f", dir.toLatin1().constData(), value("key").toLatin1().constData(), west, north, east, south); } qDebug("# GeoImage::part %s (%f, %f, %f, %f)", fname.toLatin1().constData(), west, north, east, south); QFile f(fname); if (f.exists()) return fname; const void *data_p = data(); //get pointer to data Q_ASSERT(data_p); if (type_ == UNKNOWN) return 0; int dx, dy, i, j, rx1, ry1, rx2, ry2; picBBox(west, north, east, south, rx1, ry2, rx2, ry1); dx = rx2 - rx1 + 1; dy = ry2 - ry1 + 1; if (dx <= 0 || dy <= 0) { qDebug("# (ERROR) GeoImage::part: (dx=%d=%d-%d || dy=%d=%d-%d)", dx, rx2, rx1, dy, ry2, ry1); throw ImageException(rx1,rx2,dx,ry1,ry2,dy); } FILE *of = fopen(fname.toLatin1().constData(), "w"); if (!of) { throw FileIOException(FileIOException::OPEN_FAILED,fname); } switch (type_) { case PBM:{ bit *bpi, *bpo, **dat_b_out = pbm_allocarray(dx, dy); bit **dat_b = (bit **) data_p; for (i = 0; i < dy; i++) { bpi = dat_b[i + ry1] + rx1; bpo = dat_b_out[i]; for (j = 0; j < dx; j++) { *bpo = *bpi; bpo++; bpi++; } } pbm_writepbm(of, (bit **) dat_b_out, dx, dy, 0); pbm_freearray(dat_b_out, dy); } break; case PGM:{ gray *gpi, *gpo, **dat_g_out = pgm_allocarray(dx, dy); gray **dat_g = (gray **) data_p; gray maxval = 0; for (i = 0; i < dy; i++) { gpi = dat_g[i + ry1] + rx1; gpo = dat_g_out[i]; for (j = 0; j < dx; j++) { *gpo = *gpi; if (*gpi > maxval) maxval = *gpi; gpo++; gpi++; } } pgm_writepgm(of, (gray **) dat_g_out, dx, dy, maxval, 0); pgm_freearray(dat_g_out, dy); } break; case PPM:{ pixel *ppi, *ppo, **dat_p_out = ppm_allocarray(dx, dy); pixel **dat_p = (pixel **) data_p; pixval maxval = 255; //! should be calculated for (i = 0; i < dy; i++) { ppi = dat_p[i + ry1] + rx1; ppo = dat_p_out[i]; for (j = 0; j < dx; j++) { *ppo = *ppi; ppo++; ppi++; } } ppm_writeppm(of, (pixel **) dat_p_out, dx, dy, maxval, 0); ppm_freearray(dat_p_out, dy); } break; default: pfm_geo_set(geoWest(),geoNorth(),geoEast(),geoSouth()); pfm_writepfm_region_type(of, data_, cols_, rows_, minval_, maxval_, rx1, ry1, rx2, ry2, type_); break; } fclose(of); return fname; }