#include <math.h>#include <stdlib.h>#include <ppm.h>Go to the source code of this file.
Functions | |
| int | comp_int (const void *int1, const void *int2) |
| enum ppm_error | median_filter_pgm (PPM *im_in, PPM **im_out) |
| enum ppm_error | mode_filter_pgm (PPM *im_in, PPM **im_out) |
| enum ppm_error | mean_filter_pgm (PPM *im_in, PPM **im_out) |
| int comp_int | ( | const void * | int1, | |
| const void * | int2 | |||
| ) |
Definition at line 140 of file filter.c.
References add_comment(), _PPM::bytes_per_pixel, _PPM::height, _PPM::max_col_comp, new_ppm(), ONE_PLANE_NEEDED, PGM_RAW, _PPM::pixel, PPM_OK, and _PPM::width.
Referenced by main().
00140 { 00141 00142 /* Applies a 3x3 mean filter to the image im_in, the result being 00143 returned in *im_out, the space for which is allocated in this routine. 00144 00145 /* Note: The images are must be 1 byte per pixel - these could be grey 00146 levels or indices into a colour map. */ 00147 00148 /* Until I get around to improving it, I will use the dumbest possible 00149 implementation! */ 00150 00151 int pixels[9]; 00152 int num_pixels; 00153 double mean; 00154 int x, y, i, j; 00155 00156 if (im_in->bytes_per_pixel != 1) { 00157 fprintf(stderr, "mode_filter_pgm: "); 00158 return(ONE_PLANE_NEEDED); 00159 } 00160 00161 /* create the output image */ 00162 /* create the PPM */ 00163 (*im_out) = new_ppm(); 00164 add_comment((*im_out), "# Image created by mean filtering.\n"); 00165 (*im_out)->type = PGM_RAW; 00166 (*im_out)->width = im_in->width; 00167 (*im_out)->height = im_in->height; 00168 (*im_out)->max_col_comp = im_in->max_col_comp; 00169 (*im_out)->bytes_per_pixel = 1; 00170 (*im_out)->pixel = (byte *)malloc((*im_out)->width*(*im_out)->height*sizeof(byte)); 00171 00172 /* do the mean filtering */ 00173 for (y = 0; y < im_in->height; y++) { 00174 for (x = 0; x < im_in->width; x++) { 00175 num_pixels = 0; 00176 mean = 0; 00177 for (j = -1; j <= 1; j++) { 00178 for (i = -1; i <= 1; i++) { 00179 if ( ((x + i) >= 0) && 00180 ((x + i) < im_in->width) && 00181 ((y + j) >= 0) && 00182 ((y + j) < im_in->height) 00183 ) { 00184 mean += (double)im_in->pixel[(y+j)*im_in->width + x + i]; 00185 num_pixels++; 00186 } 00187 } 00188 } 00189 (*im_out)->pixel[y*(*im_out)->width + x] = (byte)rint(mean/(double)num_pixels); 00190 } 00191 } 00192 00193 return(PPM_OK); 00194 }
Definition at line 10 of file filter.c.
References add_comment(), _PPM::bytes_per_pixel, comp_int(), _PPM::height, _PPM::max_col_comp, new_ppm(), ONE_PLANE_NEEDED, PGM_RAW, _PPM::pixel, PPM_OK, and _PPM::width.
Referenced by main().
00010 { 00011 00012 /* Applies a 3x3 median filter to the image im_in, the result being 00013 returned in *im_out, the space for which is allocated in this routine. 00014 Uses an efficient median filter implementation, described in: 00015 00016 Manfred Kopp and Werner Purgathofer. Efficient 3x3 median filter 00017 computations. Machine Vision and Graphics, 4(1/2):79-82, 1995. */ 00018 00019 /* Note: The images are must be 1 byte per pixel - these could be grey 00020 levels or indices into a colour map. */ 00021 00022 /* But until I get around to it, I will use the dumbest possible 00023 implementation! */ 00024 00025 int pixels[9]; 00026 int num_pixels, median_pos; 00027 int x, y, i, j; 00028 00029 if (im_in->bytes_per_pixel != 1) { 00030 fprintf(stderr, "median_filter_pgm: "); 00031 return(ONE_PLANE_NEEDED); 00032 } 00033 00034 /* create the output image */ 00035 /* create the PPM */ 00036 (*im_out) = new_ppm(); 00037 add_comment((*im_out), "# Image created by median filtering.\n"); 00038 (*im_out)->type = PGM_RAW; 00039 (*im_out)->width = im_in->width; 00040 (*im_out)->height = im_in->height; 00041 (*im_out)->max_col_comp = im_in->max_col_comp; 00042 (*im_out)->bytes_per_pixel = 1; 00043 (*im_out)->pixel = (byte *)malloc((*im_out)->width*(*im_out)->height*sizeof(byte)); 00044 00045 /* do the median filtering */ 00046 for (y = 0; y < im_in->height; y++) { 00047 for (x = 0; x < im_in->width; x++) { 00048 num_pixels = 0; 00049 for (j = -1; j <= 1; j++) { 00050 for (i = -1; i <= 1; i++) { 00051 if ( ((x + i) >= 0) && 00052 ((x + i) < im_in->width) && 00053 ((y + j) >= 0) && 00054 ((y + j) < im_in->height) 00055 ) 00056 pixels[num_pixels++] = im_in->pixel[(y+j)*im_in->width + x + i]; 00057 } 00058 } 00059 qsort(pixels, num_pixels, sizeof(int), comp_int); 00060 median_pos = num_pixels/2; 00061 if (num_pixels % 2) 00062 median_pos += 1; 00063 (*im_out)->pixel[y*(*im_out)->width + x] = pixels[median_pos]; 00064 } 00065 } 00066 00067 return(PPM_OK); 00068 }
Definition at line 70 of file filter.c.
References add_comment(), _PPM::bytes_per_pixel, _PPM::height, _PPM::max_col_comp, new_ppm(), ONE_PLANE_NEEDED, PGM_RAW, _PPM::pixel, PPM_OK, and _PPM::width.
Referenced by main().
00070 { 00071 00072 /* Applies a 3x3 mode filter to the image im_in, the result being 00073 returned in *im_out, the space for which is allocated in this routine. 00074 00075 /* Note: The images are must be 1 byte per pixel - these could be grey 00076 levels or indices into a colour map. */ 00077 00078 /* Until I get around to improving it, I will use the dumbest possible 00079 implementation! */ 00080 00081 int pixels[9]; 00082 int *pixel_count; 00083 int num_pixels; 00084 int max, max_pixel; 00085 int x, y, i, j; 00086 00087 if (im_in->bytes_per_pixel != 1) { 00088 fprintf(stderr, "mode_filter_pgm: "); 00089 return(ONE_PLANE_NEEDED); 00090 } 00091 00092 /* create the output image */ 00093 /* create the PPM */ 00094 (*im_out) = new_ppm(); 00095 add_comment((*im_out), "# Image created by mode filtering.\n"); 00096 (*im_out)->type = PGM_RAW; 00097 (*im_out)->width = im_in->width; 00098 (*im_out)->height = im_in->height; 00099 (*im_out)->max_col_comp = im_in->max_col_comp; 00100 (*im_out)->bytes_per_pixel = 1; 00101 (*im_out)->pixel = (byte *)malloc((*im_out)->width*(*im_out)->height*sizeof(byte)); 00102 00103 /* allocate space for the pixel value counts */ 00104 pixel_count = (int *)malloc((*im_out)->max_col_comp*sizeof(int)); 00105 for (i = 0; i < (*im_out)->max_col_comp; i++) 00106 pixel_count[i] = 0; 00107 00108 /* do the mode filtering */ 00109 for (y = 0; y < im_in->height; y++) { 00110 for (x = 0; x < im_in->width; x++) { 00111 num_pixels = 0; 00112 for (j = -1; j <= 1; j++) { 00113 for (i = -1; i <= 1; i++) { 00114 if ( ((x + i) >= 0) && 00115 ((x + i) < im_in->width) && 00116 ((y + j) >= 0) && 00117 ((y + j) < im_in->height) 00118 ) { 00119 pixels[num_pixels] = (int)im_in->pixel[(y+j)*im_in->width + x + i]; 00120 pixel_count[pixels[num_pixels]]++; 00121 num_pixels++; 00122 } 00123 } 00124 } 00125 max = 0; 00126 for (i = 0; i < num_pixels; i++) { 00127 if (pixel_count[pixels[i]] > max) { 00128 max = pixel_count[pixels[i]]; 00129 max_pixel = pixels[i]; 00130 pixel_count[pixels[i]] = 0; /* for next pass */ 00131 } 00132 } 00133 (*im_out)->pixel[y*(*im_out)->width + x] = (byte)max_pixel; 00134 } 00135 } 00136 00137 return(PPM_OK); 00138 }
1.5.6