📄 linknew.c
字号:
/* * Reads in an image and then links adjacent pixels to forms lists which it * then stores in a file. An improved version of link.c adaptef from link.pas * as run on the Viglen PC To save memory, writes to a file each pixel as it * is found. erases pixel from screen so it cannot be used again. * * July 1992 Added -f option to output floating point instead of integer pixel * values - slows up the process but useful for correct aspect ratio * correction - GAWW */#include <stdio.h>#include <math.h>#include "malloc_image.h"#define MAX_PIX 100000#define BLACK 0#define WHITE 255#define TRUE 1#define FALSE !TRUEint loop1, loop2;int list_no;float xpix[MAX_PIX], ypix[MAX_PIX];int index;int thresh_sig;double weight;int flag;int no_lists_written;float aspect_ratio; /* defined as y/x and divide y value by this */int set_aspect, set_threshold, set_file_out, set_file_in;int closed_only;int floating_point;FILE *fp_out;unsigned char **image;int height, width,depth;char file_out[255], file_in[255];int round();int do_delete_t = FALSE;int lowe = FALSE; /* threshold on average edge magnitude rather than sum */FILE *fp_in;main(argc, argv)int argc;char *argv[];{ char *temp; int count; char ch; set_aspect = FALSE; set_file_out = FALSE; set_file_in = FALSE; set_threshold = FALSE; closed_only = FALSE; aspect_ratio = 1.0; thresh_sig = 1000; floating_point = FALSE; if (argc > 1) { count = 0; do { count++; temp = argv[count]; if (*argv[count] == '-') { ch = *(++temp); switch (ch) { case 'o': count++; strcpy(file_out, argv[count]); set_file_out = TRUE; break; case 'i': count++; strcpy(file_in, argv[count]); set_file_in = TRUE; break; case 'd': do_delete_t = TRUE; break; case 'a': count++; aspect_ratio = atof(argv[count]); set_aspect = TRUE; break; case 't': count++; thresh_sig = atoi(argv[count]); set_threshold = TRUE; break; case 'c': closed_only = TRUE; break; case 'f': floating_point = TRUE; break; case 'l': lowe = TRUE; break; default: printf("error on command line\n"); } } else { printf("error on command line\n"); exit(); } } while (count < argc - 1); /*** if (floating_point == TRUE) printf("output is floating point\n"); else printf("output is integer\n"); if (set_threshold == FALSE) printf("using default threshold %d\n", thresh_sig); if (set_aspect == FALSE) printf("using default aspect_ratio %f\n", aspect_ratio); ***/ link_and_save(1); } else { printf(" PROGRAM LINKNEW\n"); printf("reads in an image with background set to 1 and edge pixels\n"); printf("set to values between 2 and 254\n"); printf("traces around the pixels storing the lists in a file as\n"); printf("lists of pixels\n"); printf("\n\n"); printf(" linknew -i file_in -o file_out -a aspect ratio -t threshold -c\n"); printf("\noptions:\n"); printf(" c find closed lists only\n"); printf(" a input aspect ratio (y/x)\n"); printf(" d delete T junctions\n"); printf(" t threshold - lists below not saved\n"); printf(" i input image file name \n"); printf(" o output list file name\n"); printf(" f floating point outputs (default is integers)\n"); printf(" l Lowe type threshold on average edge magnitude\n"); printf("\n"); printf("options a and t have defaults\n"); exit(); }}link_and_save(option)int option;/* * option used to determine if all lists (0) or only the strongest (1) are * saved to disk * */{ flag = FALSE; /* at start only - no lists */ no_lists_written = 0; read_pgm_header(file_in,&width,&height); image = malloc_char_image(width,height); read_image_body(image,width,height); if (do_delete_t) delete_T(); /* clean up image, make all object 8 connected */ clean(); /* remove isolated points - of no interest to us here */ remove_isolated(); if ((fp_out = fopen(file_out, "w")) == NULL) { printf("file %s cannot be created - aborting\n", file_out); exit(); } /* put magic name at top of file */ if (floating_point == TRUE) fprintf(fp_out, "pixel_float\n"); else fprintf(fp_out, "pixel\n"); /* link open edges */ list_no = 0; /*** printf("generating open lists...\n"); ***/ link_open(option); /* remove isolated points - of no interest to us here */ remove_isolated(); /* link closed edges */ /*** printf("generating closed lists...\n"); ***/ link_closed(option); /* remove isolated points - of no interest to us here */ remove_isolated; fprintf(fp_out, " -1 -1\n"); close(fp_out); printf("total number of lists: %d\n", list_no); if (option == 1) printf("number written to disk: %d\n", no_lists_written);}/* delete 4-way T junctions - prevents 2 sides of a Y fork joining later */delete_T(){ int loop1, loop2; int loop3, loop4; unsigned char i1, i2, i3, i4; int number; counting_pixels(); /*** printf("deleting 2X2 spots...\n\n"); for (loop1 = 2; loop1 < height-1; loop1++) for (loop2 = 2; loop2 < width-1; loop2++) if ((image[loop1][loop2] != BLACK) && (image[loop1-1][loop2] != BLACK) && (image[loop1][loop2-1] != BLACK) && (image[loop1-1][loop2-1] != BLACK)) { for (loop3 = -2; loop3 <= 1; loop3++) for (loop4 = -2; loop4 <= 1; loop4++) image[loop1+loop3][loop2+loop4] = BLACK; } ***/ printf("deleting 4-way T junctions...\n\n"); for (loop1 = 1; loop1 < height - 1; loop1++) for (loop2 = 1; loop2 < width - 1; loop2++) { if ((image[loop1][loop2] != BLACK) && (image[loop1-1][loop2] != BLACK) && (image[loop1+1][loop2] != BLACK) && (image[loop1][loop2+1] != BLACK)) { image[loop1][loop2] = BLACK; image[loop1-1][loop2] = BLACK; image[loop1+1][loop2] = BLACK; image[loop1][loop2+1] = BLACK; } else if ((image[loop1][loop2] != BLACK) && (image[loop1][loop2-1] != BLACK) && (image[loop1][loop2+1] != BLACK) && (image[loop1+1][loop2] != BLACK)) { image[loop1][loop2] = BLACK; image[loop1][loop2-1] = BLACK; image[loop1][loop2+1] = BLACK; image[loop1+1][loop2] = BLACK; } else if ((image[loop1][loop2] != BLACK) && (image[loop1-1][loop2] != BLACK) && (image[loop1+1][loop2] != BLACK) && (image[loop1][loop2-1] != BLACK)) { image[loop1][loop2] = BLACK; image[loop1-1][loop2] = BLACK; image[loop1+1][loop2] = BLACK; image[loop1][loop2-1] = BLACK; } else if ((image[loop1][loop2] != BLACK) && (image[loop1][loop2-1] != BLACK) && (image[loop1][loop2+1] != BLACK) && (image[loop1-1][loop2] != BLACK)) { image[loop1][loop2] = BLACK; image[loop1][loop2-1] = BLACK; image[loop1][loop2+1] = BLACK; image[loop1-1][loop2] = BLACK; } } counting_pixels();}clean(){ int loop1, loop2; unsigned char i1, i2, i3, i4; int number; /* clear border */ for (loop1 = 0; loop1 < height; loop1++) { image[loop1][0] = BLACK; image[loop1][width - 1] = BLACK; } for (loop1 = 0; loop1 < width; loop1++) { image[0][loop1] = BLACK; image[height - 1][loop1] = BLACK; } counting_pixels(); for (loop1 = 1; loop1 < height - 1; loop1++) for (loop2 = 1; loop2 < width - 1; loop2++) if (image[loop1][loop2] != BLACK) { i1 = image[loop1 - 1][loop2]; i2 = image[loop1][loop2 - 1]; i3 = image[loop1 + 1][loop2]; i4 = image[loop1][loop2 + 1]; if ((i1 != BLACK) && (i2 != BLACK)) { image[loop1][loop2] = BLACK; } else if ((i2 != BLACK) && (i3 != BLACK)) { image[loop1][loop2] = BLACK; } else if ((i3 != BLACK) && (i4 != BLACK)) { image[loop1][loop2] = BLACK; } else if ((i4 != BLACK) && (i1 != BLACK)) { image[loop1][loop2] = BLACK; } } counting_pixels();}counting_pixels(){ int loop1, loop2; long number; number = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -