D4 2 2. (pixel) 2 ( ) M N (x, y) (x, y ) ( )f(x, y) j N- i j i f(i, j) M-. ij f(i, j) 8 2 8 = 256, 2 2 f(x, y) 3,, R(x, y), G(x, y), B(x, y) 256 2 2.2 ( ) JPEG (Joint Photographic Experts Group), GIF (Graphics Interchange Format), TIFF (Tag Image File Format), ppm (Portable Pixmap File Format), pbm (Portable Bitmap File Format), pgm (Portable Graymap File Format) < > + < > < > < > < > pgm raw ( pgm-raw ) 256
<pgm-raw >::=< >< > < >::=< >< >< > < >< >< > < >< > < >::=P5 < >::=< > < >::=< > < >::=< > < >::=< > < >< > < >::= 2 3 4 5 6 7 8 9 < >::=< > < >< > < >::= ( ) ( \t ) ( \n ) ( \r ) < >::=< > < >::=< > < >< > 2. pgm-raw pgm-raw 2 2 < > # # ( \n ) < > < >, < >, < > ASCII < > ( ) < > < > ( ) < > 3 4 (a) 3 4 4 pgm-raw < > 4 (b) < > 4 (c) 2 2 N N- N- N + N+2 2N- M- N(M-) NM- 3. 2
2 3 2 (a) P5 # This is a comment. 3 4 3 (b) 4. pgm-raw,, 2, 3, 2,,, 3, 2,,, (c) 2.3 f (i, j) f(i, j) g(i, j) g g(i, j) = m 2 m= m n 2 n= n C m,n f(i + m, j + n) () C m,n 3 3 (m =, m 2 =, n =, n 2 = ) C m,n C m,n 5 C -, - C, - C,- C -, C, C, C -, C, C, 5. 2.4 ( ) 2.4. gradient g(i, j) = f(i, j) = i u i + i u j ) 2 ( ) 2 ( = + (2) i j u i, u j 3
i, j / i, / j - = f(i +, j) f(i, j) - i - = f(i, j + ) f(i, j) j (a) (i +.5, j +.5) i j = f(i +, j) f(i, j) = f(i, j + ) f(i, j ) - - - (b) 6. Prewitt 3 3 6 Prewitt 7 Sobel (2) g(i, j) = i + j (3) - -2 2 - (a) - -2 2 - (b) 7. Sobel 2.4.2 2 2 2 g(i, j) = 2 f i 2 + 2 f j 2 (4) 2 f i 2 = f(i +, j) f(i, j) f(i, j) f(i, j) = f(i +, j) 2f(i, j) + f(i, j) (5) 2 f j 2 = f(i, j + ) 2f(i, j) + f(i, j ) (6) 4
(5), (6) (4) 8(a) 45 8(b) 4 8 2.4.3 Roberts Robers -4 (a) 4-8 (b) 8 8. g(i, j) = ( ) 2 ( 2 f(i, j) f(i +, j + ) + f(i, j + ) f(i +, j)) (7) Robers Forsen Forsen g(i, j) = f(i, j) f(i +, j + ) + (f(i, j + ) f(i +, j) (8) g(i, j) = maxf(i + m, j + n) m, n minf(i + m, j + n) m, n (9) 2.5 2 2 g(i, j) =, (f(i, j) T ), (f(i, j) > T ) () T 2 T T 2 T ( ) 2 σ B 2 T σ B 2 (k) = ω (µ µ T ) 2 + ω (µ µ T ) 2 () σ B 2 (T ) = max k L σ B 2 (k) (2) 5
n i = i N = p i = n i /N k L ω = p i, ω = p i i= i=k+ k L L µ = ip i /ω, µ = ip i /ω, µ T = i= i=k+ i=,, 2,, L- k [, k] [k +, L-] 2 2 2 ip i 3. pgm-raw pgm-raw (a) Prewitt + (2) (b) Prewitt + (3) (c) Sobel + (2) (d) Sobel + (3) (e) 4 (f) 8 (g) Robers (h) Forsen (i) 2. pgm-raw 2 pgm-raw 3. 2 2 2 4. 2. A pgm-raw 2. 6
3 3 (, 5) () g(, 5) = C, f(, 4) + C, f(, 4) + C, f(, 4) + C, f(, 5) + C, f(, 5) + C, f(, 5) + C, f(, 6) + C, f(, 6) + C, f(, 6) (3) f(, 4), f(, 5), f(, 6) 3. 2.4 [, 255] 4. 256 unsigned char unsigned char 255 256 unsigned char 5. main. 2. 3. [], :,, 992. [2] :, C MAGAZINE, vol. 5, No. 7, pp. 38 69, Jul. 993. 7
/********************************************************************** sample.c 994/9/: Created 24/2/24: #include was modified ********************************************************************* #include <stdio.h> #include <stdlib.h> /* * #define min(a, B) ((A)<(B)? (A) : (B)) #define max(a, B) ((A)>(B)? (A) : (B)) /* * typedef struct int width; /* int height; /* int maxvalue; /* ( ) unsigned char *data; /* /* image_t; / * FILE * parsearg (int argc, char **argv) FILE *fp; /* if (argc!= 2) goto usage; fp = fopen (argv[], "rb"); /* /* if (fp == NULL) /* fputs ("Opening the input file was failend\n", stderr); goto usage; return fp; /* usage: fprintf (stderr, "usage : %s <pgm file>\n", argv[]); fputs ("\t<pgm file> : PGM-RAW format image file\n", stderr); exit (); / * * image_t *ptimage (width height) * (maxvalue) * initimage (image_t * ptimage, int width, int height, int maxvalue) ptimage->width = width; ptimage->height = height; ptimage->maxvalue = maxvalue; /* ptimage->data = (unsigned char *) malloc ((size_t) (width * height)); if (ptimage->data == NULL) /* fputs ("out of memory\n", stderr); exit (); * FILE *fp \n char * char *buf n * n- * # * buf EOF (End * Of File) NULL char * readoneline (char *buf, int n, FILE * fp) char *fgetsresult; do fgetsresult = fgets (buf, n, fp); while (fgetsresult!= NULL && buf[] == # ); /* EOF # /* return fgetsresult; / * PGM-RAW * PGM-RAW FILE *fp * * image_t *ptimage * * ptimage->data * *!!!! * PGM-RAW * PGM-RAW * * readpgmrawheader (FILE * fp, image_t * ptimage) int width, height, maxvalue; char buf[28]; /* (P5) if (readoneline (buf, 28, fp) == NULL) if (buf[]!= P buf[]!= 5 ) /* if (readoneline (buf, 28, fp) == NULL) if (sscanf (buf, "%d %d", &width, &height)!= 2) if (width <= height <= ) /* if (readoneline (buf, 28, fp) == NULL) if (sscanf (buf, "%d", &maxvalue)!= ) if (maxvalue <= maxvalue >= 256) /* initimage (ptimage, width, height, maxvalue); return; /* / error: * fputs ("Reading PGM-RAW header was failed\n", stderr); exit ();
/ * PGM-RAW * FILE *fp * image_t *ptimage data readpgmrawbitmapdata (FILE * fp, image_t * ptimage) if (fread (ptimage->data, sizeof (unsigned char), ptimage->width * ptimage->height, fp)!= ptimage->width * ptimage->height) /* fputs ("Reading PGM-RAW bitmap data was failed\n", stderr); exit (); / * ( ) * image_t *originalimage ( * ) image_t *resultimage filteringimage (image_t * resultimage, image_t * originalimage) int x, y; int width, height; /* originalimage resultimage /* width = min (originalimage->width, resultimage->width); height = min (originalimage->height, resultimage->height); for (y = ; y < height; y++) for (x = ; x < width; x++) resultimage->data[x + resultimage->width * y] = (originalimage->maxvalue - originalimage->data[x + originalimage->width * y]) * resultimage->maxvalue / originalimage->maxvalue; / * PGM-RAW * image_t *ptimage FILE *fp * PGM-RAW writepgmrawheader (FILE * fp, image_t * ptimage) /* (P5) if (fputs ("P5\n", fp) == EOF) /* if (fprintf (fp, "%d %d\n", ptimage->width, ptimage->height) == EOF) /* if (fprintf (fp, "%d\n", ptimage->maxvalue) == EOF) return; error: fputs ("Writing PGM-RAW header was failed\n", stderr); exit (); writepgmrawbitmapdata (FILE * fp, image_t * ptimage) if (fwrite (ptimage->data, sizeof (unsigned char), ptimage->width * ptimage->height, fp)!= ptimage->width * ptimage->height) /* fputs ("Writing PGM-RAW bitmap data was failed\n", stderr); exit (); /* * int main (int argc, char **argv) image_t originalimage, resultimage; FILE *fp; /* fp = parsearg (argc, argv); /* /* readpgmrawheader (fp, &originalimage); /* readpgmrawbitmapdata (fp, &originalimage); /* initimage (&resultimage, originalimage.width, originalimage.height, originalimage.maxvalue); /* filteringimage (&resultimage, &originalimage); /* writepgmrawheader (stdout, &resultimage); /* writepgmrawbitmapdata (stdout, &resultimage); return ; / * PGM-RAW * image_t *ptimage FILE *fp * PGM-RAW