MORPHOLOGICAL PROCESSING include morph h void main int

  • Slides: 43
Download presentation
MORPHOLOGICAL PROCESSING

MORPHOLOGICAL PROCESSING

#include "morph. h" void main (int argc, char *argv[]) { main SE p; IMAGE

#include "morph. h" void main (int argc, char *argv[]) { main SE p; IMAGE im; int k; Program main that calculates DILATE if (argc < 4) { printf ("Bin. Dil <input> <Structuring element> <output>n"); exit(1); } /* Read the structuring element */ k = get_se (argv[2], &p); if (!k) { printf ("Bad structuring element, file name '%s'. n", argv[2]); exit (2); } printf ("Bin. Dil: Perform a binary dilation on image '%s'. n", argv[1]); printf ("Structuring element is: n"); print_se (p); /* Read the input image */ if (read_pbm (argv[1], &im) == 0) exit(3); /* Perform the dilation */ bin_dilate (im, p); /* Write the result to the specified file */ write_pbm (argv[3], im); } argv[1] = file to read from argv[2] = structuring element, argv[3] = file to write

#include "morph. h" void main (int argc, char *argv[]) { SE p; IMAGE im;

#include "morph. h" void main (int argc, char *argv[]) { SE p; IMAGE im; int k; se = structuring element if (argc < 4) { printf ("Bin. Erode <input> <Structuring element> <output>n"); exit(1); } /* Read the structuring element */ k = get_se (argv[2], &p); if (!k) { printf ("Bad structuring element, file name '%s'. n", argv[2]); exit (2); } printf ("Bin. Erode: Perform a binary erosion on image '%s'. n", argv[1]); printf ("Structuring element is: n"); print_se (p); /* Read the input image */ if (read_pbm (argv[1], &im) == 0) exit(3); /* Perform the erosion */ bin_erode (im, p); /* Write the result to the specified file */ write_pbm (argv[3], im); } Program main that calculates binary ERODE

/* Grey level morphological operators */ #define MAX #include "maxg. h" FILE *files[20]; char

/* Grey level morphological operators */ #define MAX #include "maxg. h" FILE *files[20]; char *filenames[64]; int nfiles = 0; IMAGE Dilate (IMAGE a, IMAGE se) { Structuring element IMAGE Dilate (IMAGE a, IMAGE se) Image a is dilated with the structuring element se int i, j, ii, jj, k, n, m; IMAGE b; b = newimage (a->info->nr, a->info->nc); for (i=0; i<a->info->nr; i++) for (j=0; j<a->info->nc; j++) { k = -10000; for (ii=0; ii<se->info->nr; ii++) for (jj=0; jj<se->info->nc; jj++) { if (range(a, i+ii-se->info->oi, j+jj-se->info->oj) == 0) continue; n = se->data[ii][jj] + a->data[i+(ii-se->info->oi)][j+(jj-se->info->oj)]; if (n > k) k = n; } if (k > 255) b->data[i][j] = 255; else if (k<0) b->data[i][j] = 0; else b->data[i][j] = k; } } return b; Creates a new image with nr rows and nc columns Loops for image Loops for structuring element k=max(k, n)

IMAGE Erode (IMAGE a, IMAGE se) se { int i, j, ii, jj, k,

IMAGE Erode (IMAGE a, IMAGE se) se { int i, j, ii, jj, k, n, m; IMAGE b; Image a is eroded with the structuring element se Creates a new image with b = newimage (a->info->nr, a->info->nc); nr rows and nc columns for (i=0; i<a->info->nr; i++) for (j=0; j<a->info->nc; j++) { Loops for image k = 10000; for (ii=0; ii<se->info->nr; ii++) for (jj=0; jj<se->info->nc; jj++) Loops for structuring element { if (range(a, i+ii-se->info->oi, j+jj-se->info->oj) == 0) continue; n = a->data[i+(ii-se->info->oi)][j+(jj-se->info->oj)] se->data[ii][jj]; if (n < k) k = n; k= min(k, n) } if (k > 255) b->data[i][j] = k; else if (k<0) b->data[i][j] = 0; else b->data[i][j] = k; } } return b; Image a is dilated with the structuring element se

IMAGE Gradient (IMAGE a, IMAGE se) se { int i, j, k; IMAGE c=0;

IMAGE Gradient (IMAGE a, IMAGE se) se { int i, j, k; IMAGE c=0; Copy. Var. Image (&c, &a); Dilate (a, se); Erode (a, se); for (i=0; i<a->info->nr; i++) for (j=0; j<a->info->nc; j++) { k = a->data[i][j] - c->data[i][j]; if (k<0) k = 0; else if (k>255) k = 255; a->data[i][j] = k; } } freeimage (c); return a; Calculates Image Gradient Copy from a to c First dilate next erode Loops for image Calculates difference a - c Cut smaller than zero and larger than 255 Write new pixel value to a

IMAGE Top. Hat (IMAGE a, IMAGE se) { int i, j, k; IMAGE c=0,

IMAGE Top. Hat (IMAGE a, IMAGE se) { int i, j, k; IMAGE c=0, b=0; } IMAGE Top. Hat (IMAGE a, IMAGE se) Pointers to c and a Copy. Var. Image (&c, &a); c = Erode (a, se); /* Open. . . */ b = Dilate (c, se); First erode next dilate Copy. Var. Image (&c, &a); for (i=0; i<a->info->nr; i++) for (j=0; j<a->info->nc; j++) { k = c->data[i][j] - b->data[i][j]; if (k<0) k = 0; else if (k>255) k = 255; a->data[i][j] = k; } freeimage (c); freeimage (b); return a;

/* Check that a pixel index is in range. Return TRUE(1) if so. int

/* Check that a pixel index is in range. Return TRUE(1) if so. int range (IMAGE im, int i, int j) { if ((i<0) || (i>=im->info->nr)) return 0; if ((j<0) || (j>=im->info->nc)) return 0; return 1; } void Subs. Cmd. Line (char *s) { int k, i; char *p; /* Is it a command line arg? */ if (s[0] == '$') { for (i=1; s[i] != ''; i++) if (s[i]<'0'|| s[i]>'9') return; p = &(s[1]); sscanf (p, "%d", &k); if (k >= maxargs || k <= 0) return; strcpy (s, arg[k]); } } */ int range (IMAGE im, int i, int j) void Subs. Cmd. Line (char *s)

print_se /* PRINT_SE - Print a structuring element to the screen */ void print_se

print_se /* PRINT_SE - Print a structuring element to the screen */ void print_se (IMAGE p) { int i, j; } printf ("n===========================n"); if (p == NULL) printf (" Structuring element is NULL. n"); else { printf ("Structuring element: %dx%d origin at (%d, %d)n", p->info->nr, p->info->nc, p->info->oi, p->info->oj); for (i=0; i<p->info->nr; i++) { printf (" "); for (j=0; j<p->info->nc; j++) printf ("%4 d ", p->data[i][j]); printf ("n"); } } printf ("n===========================n");

IMAGE Input_PBM (char *fn) { IMAGE Input_PBM int i, j, k, n, m, bi,

IMAGE Input_PBM (char *fn) { IMAGE Input_PBM int i, j, k, n, m, bi, b; unsigned char ucval; int val; char buf 1[256]; FILE *f; IMAGE im; strcpy (buf 1, fn); if (fn[0] == '$') Subs. Cmd. Line(buf 1); f = fopen (buf 1, "r"); if (f==NULL) { printf ("Can't open the PBM file named '%s'n", buf 1); return 0; } case '1': case '2': case '3': case '4': case '5': case '6': default: pbm_getln (f, buf 1); if (buf 1[0] == 'P') { switch (buf 1[1]) { k=1; break; k=2; break; k=3; break; k=4; break; k=5; break; k=6; break; printf ("Not a PBM/PGM/PPM file. n"); return 0; } } bi = 2; Gets line

IMAGE Input_PBM get_num_pbm (f, buf 1, &bi, &m); /* Number of columns */ get_num_pbm

IMAGE Input_PBM get_num_pbm (f, buf 1, &bi, &m); /* Number of columns */ get_num_pbm (f, buf 1, &bi, &n); /* Number of rows */ if (k!=1 && k!=4) get_num_pbm (f, buf 1, &bi, &b); /* Max value */ else b = 1; printf ("n. PBM file class %d size %d columns X %d rows Max=%dn", k, n, m, b); /* Allocate the image */ if (k==3 || k==6) /* Colour */ myabort (0, "Colour image. "); else { im = (IMAGE)newimage (n, m); im->info->oi = PBM_SE_ORIGIN_ROW; im->info->oj = PBM_SE_ORIGIN_COL; PBM_SE_ORIGIN_ROW = 0; PBM_SE_ORIGIN_COL = 0; for (i=0; i<n; i++) for (j=0; j<m; j++) if (k<3) { fscanf (f, "%d", &val); im->data[i][j] = (unsigned char)val; } else { fscanf (f, "%c", &ucval); im->data[i][j] = ucval; } } return im; }

IMAGE Output_PBM (IMAGE image, char *filename) { FILE *f; int i, j, k, perline;

IMAGE Output_PBM (IMAGE image, char *filename) { FILE *f; int i, j, k, perline; char buf 1[64]; } IMAGE Output_PBM strcpy (buf 1, filename); if (buf 1[0] == '$') Subs. Cmd. Line(buf 1); if (image->info->nc > 20) perline = 20; else perline = image->info->nc-1; f = fopen (buf 1, "w"); if (f == 0) myabort (0, "Can't open output file. "); fprintf (f, "P 2n#origin %d %dn", image->info->oj, image->info->oi); fprintf (f, "%d %d %dn", image->info->nc, image->info->nr, 255); k = 0; for (i=0; i<image->info->nr; i++) for (j=0; j<image->info->nc; j++) { fprintf (f, "%d ", image->data[i][j]); k++; if (k > perline) { fprintf (f, "n"); k = 0; } } fprintf (f, "n"); fclose (f); return image;

void get_num_pbm (FILE *f, char *b, int *bi, int *res) { void get_num_pbm int

void get_num_pbm (FILE *f, char *b, int *bi, int *res) { void get_num_pbm int i; char str[80]; while (b[*bi]==' ' || b[*bi]=='t' || b[*bi]=='n') { if (b[*bi] == 'n') { Reads line, explained in pbm_getln (f, b); next slide *bi = 0; } else *bi += 1; } } i = 0; while (b[*bi]>='0' && b[*bi]<='9') str[i++] = b[(*bi)++]; str[i] = ''; sscanf (str, "%d", res);

/* Get the next non-comment line from the PBM file f into the buffer

/* Get the next non-comment line from the PBM file f into the buffer b. Look for 'pragmas' - commands hidden in the comments */ void pbm_getln (FILE *f, char *b) { int i; char c; /* Read the next significant line (non-comment) from f into buffer b */ do { /* Read the next line */ i = 0; do { fscanf (f, "%c", &c); b[i++] = c; if (c == 'n') b[i] = ''; } while (c != 'n'); pbm_param looks for parameters, explained in next slide /* If a comment, look for a special parameter */ if (b[0] == '#') pbm_param (b); } } while (b[0]=='n' || b[0] == '#');

/* Look for a parameter hidden in a comment void pbm_param (char *s) {

/* Look for a parameter hidden in a comment void pbm_param (char *s) { */ int i, j; char key[24]; /* Extract the key word */ for (i=0; i<23; i++) { j = i; if (s[i+1] == ' ' || s[i+1] == 'n') break; key[i] = s[i+1]; } key[j] = ''; /* Convert to lower case */ for (i=0; i<j; i++) if ( (key[i]>='A') && (key[i]<='Z') ) key[i] = (char) ( (int)key[i] - (int)'A' + (int)'a' ); /* Which key word is it? */ if (strcmp(key, "origin") == 0) /* ORIGIN key word */ { sscanf (&(s[j+1]), "%d %d", &PBM_SE_ORIGIN_COL, &PBM_SE_ORIGIN_ROW); return; } } void pbm_param

struct image *newimage (int nr, int nc) { struct image *x; unsigned char *ptr;

struct image *newimage (int nr, int nc) { struct image *x; unsigned char *ptr; int i; /* New image */ /* new pixel array */ if (nr < 0 || nc < 0) { printf ("Error: Bad image size (%d, %d)n", nr, nc); return 0; } /* Allocate the image structure */ x = (struct image *) malloc( sizeof (struct image) ); if (!x) { printf ("Out of storage in NEWIMAGE. n"); return 0; } /* Allocate and initialize the header */ x->info = (struct header *)malloc( sizeof(struct header) ); if (!(x->info)) { printf ("Out of storage in NEWIMAGE. n"); return 0; } x->info->nr = nr; x->info->nc = nc; x->info->oi = x->info->oj = 0;

struct image *newimage (int nr, int nc) /* Allocate the pixel array */ x->data

struct image *newimage (int nr, int nc) /* Allocate the pixel array */ x->data = (unsigned char **)malloc(sizeof(unsigned char *)*nr); /* Pointers to rows */ if (!(x->data)) { printf ("Out of storage in NEWIMAGE. n"); return 0; } } for (i=0; i<nr; i++) { ptr = (unsigned char *)malloc(nc); /* Allocate one row */ if (!ptr) { printf ("Out of storage in NEWIMAGE. n"); return 0; } else x->data[i] = ptr; } return x;

void freeimage (struct image *z) { /* } Free the storage associated with the

void freeimage (struct image *z) { /* } Free the storage associated with the image Z */ int i; if (z != 0) { for (i=0; i<z->info->nr; i++) free (z->data[i]); free (z->info); free (z->data); free (z); }

IMAGE Complement (IMAGE x) Image t is a complement of image x IMAGE Complement

IMAGE Complement (IMAGE x) Image t is a complement of image x IMAGE Complement (IMAGE x) { IMAGE t; int i, j; } t = New. Image (x); for (i=0; i<x->info->nr; i++) for (j=0; j<x->info->nc; j++) t->data[i][j] = 255 - x->data[i][j]; ; return t;

void myabort (int val, char *mess) { val = val; fprintf (stderr, "**** ABORT

void myabort (int val, char *mess) { val = val; fprintf (stderr, "**** ABORT MAX: %s ****n", mess); exit (2); } void myabort (int val, char *mess) int Attribute (int which, IMAGE z) { PIXEL p; } p = (PIXEL)z; if (which == 1) return z->info->nc; else if (which == 2) return z->info->nr; else if (which == 3) return z->info->oi; else if (which == 4) return z->info->oj; else if (which == 5) return p->row; else if (which == 6) return p->col; myabort (0, "Illegal attribute. "); int Attribute (int which, IMAGE z) Returns all kinds of information about pixel p

void Copy. Var. Image (IMAGE *a, IMAGE *b) { int i, j; Copies image

void Copy. Var. Image (IMAGE *a, IMAGE *b) { int i, j; Copies image from b to a if (a == b) return; if (*a) freeimage (*a); *a = newimage ((*b)->info->nr, (*b)->info->nc); if (*a == 0) myabort (0, "No more storage. n"); } for (i=0; i<(*b)->info->nr; i++) for (j=0; j< (*b)->info->nc; j++) (*a)->data[i][j] = (*b)->data[i][j]; (*a)->info->oi = (*b)->info->oi; (*a)->info->oj = (*b)->info->oj;

void Copy. Var. Pix (PIXEL *a, PIXEL *b) { int i, j; Copies pixel

void Copy. Var. Pix (PIXEL *a, PIXEL *b) { int i, j; Copies pixel if (a == b) return; if (*a) free (*a); *a = (PIXEL)malloc (sizeof(struct pixrec)); if (*a == 0) myabort (0, "No more storage. n"); } (*a)->row = (*b)->row; (*a)->col = (*b)->col;

/* Min. Max computes the range for pixel values that will contain all pixels

/* Min. Max computes the range for pixel values that will contain all pixels in both images. */ void Min. Max (IMAGE a, IMAGE b, int *rmax, int *cmax, int *rmin, int *cmin) { int x, y; void Min. Max (IMAGE a, IMAGE b, int *rmax, int *cmax, int *rmin, int *cmin) /* (0, 0) index is which pixel? */ y = jtop (a, 0); x = jtop (b, 0); if (x < y) *cmin = x; else *cmin = y; y = itop (a, 0); x = itop (b, 0); if (x < y) *rmin = x; else *rmin = y; /* Now max indices */ x = itop (a, a->info->nr); y = itop (b, b->info->nr); if (x > y) *rmax = x; else *rmax = y; } x = jtop (a, a->info->nc); y = jtop (b, b->info->nc); if (x>y) *cmax = x; else *cmax = y;

void Min. Max (IMAGE a, IMAGE b, int *rmax, int *cmax, int *rmin, int

void Min. Max (IMAGE a, IMAGE b, int *rmax, int *cmax, int *rmin, int *cmin) /* An INDEX into an image is offset by the image's origin. These functions convert indices into pixels, or set elements */ int itop (IMAGE a, int i) { return i-a->info->oi; } int jtop (IMAGE a, int j) { return j - a->info->oj; }

/* These functions convert PIXELS (set elements) into image indices, either row or column,

/* These functions convert PIXELS (set elements) into image indices, either row or column, using the origin location. int ptoi (IMAGE a, int p) { return p + a->info->oi; } int ptoj (IMAGE a, int p) { return p + a->info->oj; } */ int ptoi (IMAGE a, int p) int ptoj (IMAGE a, int p) /* Get and set pixel values from pixel coordinates (set elements) */ int pget (IMAGE a, int i, int j) { int ii, jj; } ii = ptoi (a, i); jj = ptoj (a, j); if (range(a, ii, jj)) return a->data[ii][jj]; else return 0; int pget (IMAGE a, int i, int j)

void pset (IMAGE a, int i, int j, int val) { int ii, jj;

void pset (IMAGE a, int i, int j, int val) { int ii, jj; } void pset (IMAGE a, int i, int j, int val) ii = ptoi (a, i); jj = ptoj (a, j); if (range(a, ii, jj)) a->data[ii][jj] = val; else if (val != 0) myabort (0, "Attempt to set pixel not in image. "); IMAGE Union (IMAGE a, IMAGE b) { IMAGE t; int i, j, rmin, rmax, cmin, cmax; } Min. Max (a, b, &rmax, &cmax, &rmin, &cmin); t = newimage (rmax-rmin, cmax-cmin); t->info->oi = -rmin; t->info->oj = -cmin; for (i=rmin; i<rmax; i++) for (j=cmin; j< cmax; j++) if (pget(a, i, j)<= pget(b, i, j)) pset (t, i, j, pget(b, i, j)); else pset (t, i, j, pget(a, i, j)); return t; IMAGE Union (IMAGE a, IMAGE b)

IMAGE Intersection (IMAGE a, IMAGE b) { IMAGE t; int i, j, rmin, rmax,

IMAGE Intersection (IMAGE a, IMAGE b) { IMAGE t; int i, j, rmin, rmax, cmin, cmax; } Min. Max (a, b, &rmax, &cmax, &rmin, &cmin); t = newimage (rmax-rmin, cmax-cmin); t->info->oi = -rmin; t->info->oj = -cmin; for (i=rmin; i<rmax; i++) for (j=cmin; j< cmax; j++) if (pget(a, i, j) > pget(b, i, j)) pset(t, i, j, pget(a, i, j)); else pset (t, i, j, pget(b, i, j)); return t;

IMAGE Difference (IMAGE a, IMAGE b) { IMAGE t; int i, j, rmax, rmin,

IMAGE Difference (IMAGE a, IMAGE b) { IMAGE t; int i, j, rmax, rmin, cmax, cmin; } Min. Max (a, b, &rmax, &cmax, &rmin, &cmin); t = newimage (rmax-rmin, cmax-cmin); t->info->oi = -rmin; t->info->oj = -cmin; for (i=rmin; i<rmax; i++) for (j=cmin; j< cmax; j++) pset (t, i, j, pget(a, i, j)-pget(b, i, j)); return t;

int PSub. Set (IMAGE a, IMAGE b) { if (Im. Compare(a, b)) return 0;

int PSub. Set (IMAGE a, IMAGE b) { if (Im. Compare(a, b)) return 0; return Sub. Set (a, b); } int Sub. Set (IMAGE a, IMAGE b) { int i, j; int rmax, rmin, cmax, cmin; } int PSub. Set (IMAGE a, IMAGE b) int Sub. Set (IMAGE a, IMAGE b) Min. Max (a, b, &rmax, &cmax, &rmin, &cmin); for (i=rmin; i<rmax; i++) for (j=cmin; j< cmax; j++) if (pget(a, i, j)==1 && pget(b, i, j)==0) return 0; return 1;

int Im. Compare (IMAGE a, IMAGE b) { int i, j, rmin, rmax, cmin,

int Im. Compare (IMAGE a, IMAGE b) { int i, j, rmin, rmax, cmin, cmax, x, y; Min. Max (a, b, &rmax, &cmax, &rmin, &cmin); } for (i=rmin; i<rmax; i++) for (j=cmin; j< cmax; j++) { x = pget (a, i, j); y = pget (b, i, j); if (x != y) return 0; } return 1;

int Im. Value (IMAGE a, int val) { int i, j; } int Im.

int Im. Value (IMAGE a, int val) { int i, j; } int Im. Value (IMAGE a, int val) for (i=0; i<a->info->nr; i++) for (j=0; j< a->info->nc; j++) if (a->data[i][j]!=val) return 0; return 1; int Pix. Value (PIXEL a, PIXEL b) { if (a==0) myabort (0, "NULL pixel. "); if (b==0) myabort (0, "NULL pixel. "); if ((a->row == b->row) && (a->col== b->col)) return 1; return 0; }

IMAGE New. Image (IMAGE x) { IMAGE t; int i, j; } if (x

IMAGE New. Image (IMAGE x) { IMAGE t; int i, j; } if (x == 0) myabort (0, "IMAGE is 0 in New. Image. "); t = newimage (x->info->nr, x->info->nc); if (t == 0) myabort (0, "Out of storage. "); t->info->oi = x->info->oi; t->info->oj = x->info->oj; for (i=0; i<x->info->nr; i++) for (j=0; j<x->info->nc; j++) t->data[i][j] = 0; return t;

IMAGE Translate (IMAGE b, PIXEL p) { IMAGE t; int i, j; } IMAGE

IMAGE Translate (IMAGE b, PIXEL p) { IMAGE t; int i, j; } IMAGE Translate (IMAGE b, PIXEL p) t = newimage (b->info->nr+p->row+b->info->oi, b->info->nc+p->col+b->info->oj); t->info->oi = 0; t->info->oj = 0; for (i=0; i<t->info->nr; i++) for (j=0; j< t->info->nc; j++) t->data[i][j] = 0; for (i=0; i<b->info->nr; i++) for (j=0; j< b->info->nc; j++) if (range(t, i+p->row-b->info->oi, j+p->col-b->info->oj)) t->data[i+p->row-b->info->oi][j+p->col-b->info->oj] = b->data[i][j]; return t;

PIXEL Pixel (int i, int j) { PIXEL x; } x = (PIXEL) malloc

PIXEL Pixel (int i, int j) { PIXEL x; } x = (PIXEL) malloc (sizeof (struct pixrec)); if (x == 0) myabort (0, "Out of storage. "); x->row = i; x->col = j; return x; int Member (PIXEL p, IMAGE xx) { int i, j; } if (xx == 0) myabort (0, "Image is NULL. n"); i = p->row + xx->info->oi; j = p->col + xx->info->oj; if (range(xx, i, j)) { if (xx->data[i][j] >= 1) return 1; else return 0; } else return 0;

void Outint (int val, char *name) { int i; if (name[0] == '�') {

void Outint (int val, char *name) { int i; if (name[0] == '') { printf (" %d ", val); return; } /* Standard output */ for (i=0; i<nfiles; i++) if (strcmp(filenames[i], name)==0) { fprintf (files[i], "%dn", val); return; }

void Outint (int val, char *name) /* New file name */ strcpy (filenames[nfiles], name);

void Outint (int val, char *name) /* New file name */ strcpy (filenames[nfiles], name); files[nfiles] = fopen (name, "w"); if (files[nfiles] == NULL) { fprintf (stderr, "File name '%s': ", name); myabort (0, "Cannot open output file. "); } fprintf (files[nfiles], " %d ", val); nfiles++; }

void Outint (int val, char *name) int inint (int *val, char *name) { int

void Outint (int val, char *name) int inint (int *val, char *name) { int i; if (name[0] == '') { scanf ("%d", val); return *val; } /* Standard input */ for (i=0; i<nfiles; i++) if (strcmp(filenames[i], name)==0) { fscanf (files[i], "%dn", val); return *val; } /* New input file */ strcpy (filenames[nfiles], name); files[nfiles] = fopen (name, "r"); if (files[nfiles] == NULL) { fprintf (stderr, "File name '%s': ", name); myabort (0, "Cannot open input file. "); } fscanf (files[nfiles], " %d ", val); nfiles++; return *val; }

IMAGE Image. Gen (PIXEL p 1, PIXEL p 2, char *s) { int i,

IMAGE Image. Gen (PIXEL p 1, PIXEL p 2, char *s) { int i, j, k; IMAGE Image. Gen (PIXEL p 1, PIXEL p 2, char *s) IMAGE t; } t = newimage (p 1 ->row, p 1 ->col); t->info->oi = p 2 ->row; t->info->oj = p 2 ->col; k = 0; for (i=0; i<p 1 ->row; i++) for (j=0; j<p 1 ->col; j++) { if (s[k] == '1') t->data[i][j] = 1; else if (s[k] == '2') t->data[i][j] = 2; else if (s[k] == '3') t->data[i][j] = 3; else if (s[k] == '4') t->data[i][j] = 4; else if (s[k] == '5') t->data[i][j] = 5; else if (s[k] == '6') t->data[i][j] = 6; else if (s[k] == '7') t->data[i][j] = 7; else if (s[k] == '8') t->data[i][j] = 8; else if (s[k] == '9') t->data[i][j] = 9; else t->data[i][j] = 0; k++; } return t;

int Isolated (IMAGE a) { int i, j, n, m, k, res; } k

int Isolated (IMAGE a) { int i, j, n, m, k, res; } k = 0; for (i=0; i<a->info->nr; i++) for (j=0; j<a->info->nc; j++) if (a->data[i][j] > 0) { res = 1; for (n= -1; n<=1; n++) for (m= -1; m<=1; m++) { if (n==0 && m==0) continue; if (range(a, i+n, j+m)) if (a->data[i+n][j+m] > 0) res = 0; } k += res; } return k; int Isolated (IMAGE a)

IMAGE Set. APixel (IMAGE a, PIXEL p) { int i, j, row, col; int

IMAGE Set. APixel (IMAGE a, PIXEL p) { int i, j, row, col; int rmin, rmax, cmin, cmax; IMAGE t; } IMAGE Set. APixel (IMAGE a, PIXEL p) row = ptoi(a, p->row); col = ptoj(a, p->col); if (row > a->info->nr) rmax = row; else rmax = a->info->nr; if (col > a->info->nc) cmax = col; else cmax = a->info->nc; if (row < 0) rmin = row; else rmin = 0; if (col < 0) cmin = col; else cmin = 0; if (range(a, row, col)==0) { t = newimage (rmax-rmin, cmax-cmin); t->info->oi = -rmin; t->info->oj = -cmin; for (i=rmin; i<rmax; i++) for (j=cmin; j<cmax; j++) pset(t, i, j, pget(a, i, j)); pset (t, p->row, p->col, 1); return t; } a->data[row][col] = 1; return a;

PIXEL Pix. Dif (PIXEL p 1, PIXEL p 2) { PIXEL p 3; }

PIXEL Pix. Dif (PIXEL p 1, PIXEL p 2) { PIXEL p 3; } p 3 = (PIXEL)malloc (sizeof(struct pixrec)); p 3 ->row = p 1 ->row - p 2 ->row; p 3 ->col = p 1 ->col - p 2 ->col; return p 3; PIXEL Pix. Add (PIXEL p 1, PIXEL p 2) { PIXEL p 3; } p 3 = (PIXEL)malloc (sizeof(struct pixrec)); p 3 ->row = p 1 ->row + p 2 ->row; p 3 ->col = p 1 ->col + p 2 ->col; return p 3;

void Display (IMAGE x) { Output_PBM (x, ". dis"); system ("disp. dis"); system ("del.

void Display (IMAGE x) { Output_PBM (x, ". dis"); system ("disp. dis"); system ("del. dis"); }