extract_features.c

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <malloc.h>
00004 #include <math.h>
00005 #include <string.h>
00006 #include <unistd.h>
00007 
00008 #ifdef HAVE_CONFIG_H
00009 #include "gift-config.h"
00010 #endif
00011 
00012 #include <ppm.h>
00013 #include "gift_features.h"
00014 //#include "extract_features.proto"
00015 
00016 /* for c99 uint32_t */
00017 
00018 #include <stdint.h>
00019 
00020 #define numH 18
00021 #define numS 3
00022 #define numV 3
00023 #define numGrey 4
00024 
00025 #define OLDFIXED
00026 #ifdef OLDFIXED
00027 /* the number of scales to recognise features at */
00028 #define num_gabor_scales 3
00029 /* the number of color blocks we're going to break the image into (overlapping) */
00030 #define num_total_colour_blocks (256+64+16+4)
00031 #endif
00032 
00033 void init_feature_variables(uint32_t, uint32_t **, uint32_t ***, uint32_t ***);
00034 void extract_gabor_features(PPM *, uint32_t ***, uint32_t ***);
00035 void extract_mode_features(PPM *, uint32_t *, uint32_t, uint32_t **, byte *, uint32_t *);
00036 enum ppm_error write_mode_features(char *, uint32_t, uint32_t *, uint32_t ***, uint32_t ***, byte *);
00037 
00038 int main(int argc, char *argv[]) {
00039 
00040   char *in_fname, *out_fname;
00041   char *point_pos;
00042   FILE *ppm_file;
00043   PPM *im_rgb, *im_hsv, *im_quant;
00044   uint32_t *colmap, colmap_size;
00045   enum file_types ppm_type;
00046   enum ppm_error the_error;
00047   uint32_t ** block_gabor_class[num_gabor_scales];
00048   uint32_t ** gabor_histogram[num_gabor_scales];
00049   /* the "mode" of each color block. */
00050   byte block_mode[num_total_colour_blocks];
00051   /* the colour count of each block. for quantizing the image */
00052   uint32_t * col_counts[num_total_colour_blocks];
00053   uint32_t * col_histogram;
00054  
00055   switch(argc) {
00056   case 2:
00057     in_fname = argv[1];
00058     break;
00059   default:
00060     fprintf(stderr, "Usage: %s ppm_file \n\n", argv[0]);
00061     exit(1);
00062     break;
00063   }
00064 
00065   if ((ppm_file = fopen (in_fname, "r")) == NULL) {
00066     fprintf(stderr, "Can't open file: %s", in_fname);
00067     exit(1);
00068   }
00069 
00070   /* now get the filename prefix */
00071   out_fname = (char *)malloc((strlen(in_fname) + 10)*sizeof(char));
00072   if ((point_pos = strchr(in_fname, '.')) == NULL) {
00073     fprintf(stderr, "File %s has no ""."" - can't generate features filename\n\n", in_fname);
00074     exit(1);
00075   }
00076   else {
00077     /* must find the *last* "." in the filename */
00078     point_pos = &in_fname[strlen(in_fname)];
00079     while (*point_pos != '.') {
00080       point_pos--;
00081     }
00082     strncpy(out_fname, in_fname, (int)(point_pos - in_fname));
00083     strcat(out_fname, ".fts");
00084   }
00085 #ifdef DEBUG
00086   fprintf(stderr, "Features will be written to file %s\n", out_fname);
00087 #endif
00088   
00089 
00090   /* read the rgb image from we are going to extract features */
00091   switch(ppm_type = read_magic_no(ppm_file)) {
00092   case PGM_ASC: case PPM_ASC: case PGM_RAW: case PPM_RAW:
00093     if ((the_error = read_ppm(ppm_file, &im_rgb, ppm_type)) != PPM_OK) {
00094       ppm_handle_error(the_error);
00095       exit(1);
00096     }
00097     break;
00098   default:
00099     fprintf(stderr, "Unrecognized file type.\n");
00100     exit(1);
00101     break;
00102   }
00103 
00104   /* convert it to hsv */
00105   if ((the_error = rgb2hsv_ppm(im_rgb, &im_hsv)) != PPM_OK) {
00106     ppm_handle_error(the_error);
00107     exit(1);
00108   }
00109 
00110   /* quantize the image */
00111   colmap_size = numH*numS*numV + numGrey;
00112   if ((the_error = hsv_quantize_ppm(im_hsv, &im_quant, &colmap, numH, numS, numV, numGrey)) != PPM_OK) {
00113     ppm_handle_error(the_error);
00114     exit(1);
00115   }
00116 
00117   /* FIXME: move this back into init_feature_variables. */
00118   col_histogram = (uint32_t *)malloc(colmap_size*sizeof(uint32_t));
00119 
00120   /* initialise the variables required for the feature extraction */
00121   init_feature_variables(colmap_size, col_counts, block_gabor_class, gabor_histogram);
00122 
00123   /* extract the features */
00124 
00125   extract_mode_features(im_quant, colmap, colmap_size, col_counts, block_mode, col_histogram);
00126 
00127   extract_gabor_features(im_hsv, block_gabor_class, gabor_histogram);
00128 
00129   /* write them to the file */
00130   if ((the_error = write_mode_features(out_fname, colmap_size, col_histogram, block_gabor_class, gabor_histogram, block_mode)) != PPM_OK) {
00131     ppm_handle_error(the_error);
00132     exit(1);
00133   }
00134   
00135   /* everything is OK */
00136   destroy_ppm(&im_hsv);
00137   destroy_ppm(&im_quant);
00138   free(colmap);
00139   exit(0);
00140 }

Generated on Tue Jan 6 00:30:37 2009 for Gift by  doxygen 1.5.6