imageprocessing.cpp

Go to the documentation of this file.
00001  #include <stdlib.h>
00002 
00003 #include "imageprocessing.h"
00004 
00005 
00006         int threshold_red=0;//=250;
00007     int threshold_green=0;//=250;
00008         int threshold_blue=0;//=100;
00009 
00010 
00011 int init_imageprocessing() {
00012 
00013         char *green = getenv( "GREEN" );
00014         if ( green ) threshold_green= atoi(green);
00015         char *blue = getenv( "BLUE" );
00016         if ( blue ) threshold_blue= atoi(blue);
00017         char *red = getenv( "RED" );
00018         if ( red ) threshold_red= atoi(red);
00019     printf("red : %d , green %d, blue %d",threshold_red,threshold_green,threshold_blue);
00020         //exit(0);
00021     return 0;
00022 }
00023 
00024 Image *get_component (Image *inputImage , int color)
00025 {
00026     Image *resultImage;
00027         Matrix *mat;
00028         Image *temp;
00029 
00031         if (color == RGB_RED)   // RED COMPONENT
00032         {
00033                 Image *band2, *band3 ;
00034                 band2 = duplicate_Image(inputImage);
00035                 band3 = duplicate_Image(inputImage);
00036                 band2 = extract_band(band2, 2);
00037                 band3 = extract_band(band3, 3);
00038                 inputImage = extract_band(inputImage, 1);
00039                 inputImage = gray_multiply2(inputImage,2.0);    
00040                 inputImage = subtract_Image(inputImage, band2);
00041                 inputImage = subtract_Image(inputImage, band3);
00042         }
00044 
00045 
00046         if (color == RGB_GREEN)   // GREEN COMPONENT
00047         {
00048                 Image *band1, *band3 ;
00049                 band1 = duplicate_Image(inputImage);
00050                 band3 = duplicate_Image(inputImage);
00051                 band1 = extract_band(band1, 1);
00052                 band3 = extract_band(band3, 3);
00053                 inputImage = extract_band(inputImage, 2);
00054                 inputImage = gray_multiply2(inputImage,2.5);    
00055                 inputImage = subtract_Image(inputImage, band1);
00056                 inputImage = subtract_Image(inputImage, band3);
00057         }
00058                 
00060 
00061         if (color == RGB_BLUE)   // BLUE COMPONENT
00062         {
00063                 Image *band1, *band2 ;
00064                 band1 = duplicate_Image(inputImage);
00065                 band2 = duplicate_Image(inputImage);
00066                 band1 = extract_band(band1, 1);
00067                 band2 = extract_band(band2, 2);
00068                 inputImage = extract_band(inputImage, 3);
00069                 inputImage = gray_multiply2(inputImage,2.0);    
00070                 inputImage = subtract_Image(inputImage , band1);
00071                 inputImage = subtract_Image(inputImage , band2);
00072         }
00073                         
00074    //log_Image(inputImage);
00075 // log_Image(inputImage,"_subtractedband");
00076 
00077 
00078 // DEBUG //     Image *band1 = duplicate_Image(inputImage);
00079 // DEBUG //     band1=remap_Image(band1 ,CVIP_SHORT,0,255);
00080 // DEBUG //     view_Image(band1,"remapaftersubtract.ppm");
00081 
00082 // DEBUG //view_Image(inputImage,"aftersubtract.ppm");
00083 
00084     
00085 //  temp = remap_Image(inputImage ,CVIP_SHORT,-255,255);
00086 //temp = remap_Image(inputImage ,CVIP_SHORT,0,255);
00087 
00088         // pak alleen de hoge waardens
00089 //      temp = gray_linear(inputImage ,200.0,510.0,0.0 ,1.0,0, -1);
00090         
00091         // doe threshold at 250 (red)
00092         // doe threshold at 150 (blue)
00093 //      temp = gray_linear(inputImage ,250.0,510.0,100.0 ,0.0,0, -1);
00094         // 0 -> 310
00095         if (color == RGB_BLUE) {
00096                 temp = gray_linear(inputImage ,threshold_blue,510.0,100.0 ,0.0,0, -1);
00097     }
00098         if (color == RGB_RED) {
00099                 temp = gray_linear(inputImage ,threshold_red,510.0,100.0 ,0.0,0, -1);
00100     }
00101         if (color == RGB_GREEN) {
00102                 temp = gray_linear(inputImage ,threshold_green,510.0,100.0 ,0.0,0, -1);
00103     }
00104 
00105 
00106 
00107     //view_Image(inputImage,"band1short");
00108 //DEBUG //view_Image(temp,"afterthreshold.ppm");
00109 
00110         // remap data in 0-100 range
00111         //temp = remap_Image(temp ,CVIP_SHORT,0,255);
00112 
00113 //      delete_Image(inputImage);
00114 
00115 
00116 
00117  // remap to scale for good convolve ( type must be short for convolve!)
00118         temp = remap_Image(temp ,CVIP_SHORT,0,100);
00119         
00120         // blur image to make nice full continuous objects
00121         mat=(Matrix *)get_default_filter(BLUR_SPATIAL,7,0);
00122         temp = (Image *)convolve_filter(temp,mat);
00123         //temp = (Image *)convolve_filter(temp,mat);
00124         //temp = (Image *)convolve_filter(temp,mat);
00125 
00126 // DEBUG // view_Image(temp,"afterblur.ppm");
00127 
00128         delete_Matrix(mat);
00129 
00130         // cut off object edge at 70%  of value after blur
00131 //      temp = gray_linear(temp ,130.0,255.0,255.0,0.0,0, -1);
00132         temp = gray_linear(temp ,50,255.0,255.0,0.0,0, -1);
00133         
00134 
00135     resultImage = remap_Image(temp ,CVIP_BYTE,0,255);   
00136         delete_Image(temp);
00137    
00138 //    log_Image(resultImage,"_component");
00139     //log_Image(resultImage);
00140     return resultImage;
00141 }
00142 
00143 Image *find_color_entities(Image *inputimage, int color , 
00144                           ENTITYLIST *entitylist )
00145 {
00146      int row,col,angle,label,center_row,center_col,ret;
00147      long entityarea;
00148      int *ctr;
00149      Image *component_img;
00150      Image *labeled_img;
00151      Entity entity;
00152 
00153 
00154         if (color == RGB_BLUE  && ! threshold_blue) {
00155                  return inputimage;
00156     }
00157         if (color == RGB_RED  && ! threshold_red) {
00158                  return inputimage;
00159 
00160     }
00161         if (color == RGB_GREEN  && ! threshold_green) {
00162                  return inputimage;
00163     }
00164 
00165  
00166      // get color entitys in labeled image
00167      component_img=get_component(inputimage, color);
00168    
00169     // label entitys ( connented values not null)
00170      if ( !get_first_non_null_point(component_img,&row,&col) ) {
00171                 printf("first label : EMPTY IMG ");
00172         //return false;
00173                 return component_img;
00174         }
00175     Image *temp = duplicate_Image(component_img); 
00176     labeled_img = ::label(temp);
00177 
00178 
00179     entity.setColor((Entity::Color)color);
00180 
00181     // per entity
00182     // 1. find first coordinates row col of a entity with label  'label'
00183     //    start with first label=1
00184     label=1;
00185     row=col=0;
00186     //get_first_non_null_point(labeled_image, &row,&col);
00187             // find labeled entity with label higher than 'label'
00188     while ( find_labeled_entity(labeled_img,row,col,label) ) {
00189        //myprintf("label,pos: %d %d %d  ", label,row ,col);
00190         
00191        // 2. determine area a --> obj_type  
00192        entityarea=area(labeled_img,row,col);
00193        printf("area : %d",entityarea);
00194         
00195        // 3. find center coordinates 
00196        if  ( (SUBJECT_MINAREA < entityarea) && (entityarea <= GOAL_MAXAREA ) ) {
00197            // find center
00198            ctr=centroid(labeled_img,row,col);
00199            center_row=ctr[0];
00200            center_col=ctr[1];
00201 
00202            entity.setX(center_col);
00203                    //swap orientation y-as!!
00204            entity.setY(240-center_row);
00205            delete(ctr);
00206        }
00207 
00208 
00209 /*       
00210        // 4. set entity type, and if type=robot find angle 
00211        if  ( (ROBOT_MINAREA < entityarea) && (entityarea <= ROBOT_MAXAREA ) ) {
00212            // set type to robot
00213            entity.setType(Entity::ROBOT);
00214            // find angle robot
00215            ret=robot_angle(component_img,center_row,center_col,angle); 
00216            if (ret ) {
00217               entity.setAngle(angle);
00218               // 5. add entity to entitylist
00219               entitylist->push_back(entity); 
00220               //myprintf("rover:\n  angle=%d,row=%d,col=%d,",angle,row,col);
00221            }
00222        }
00223 */
00224        if  ( (SUBJECT_MINAREA < entityarea) && (entityarea <= SUBJECT_MAXAREA ) ) {
00225            // set type 
00226            entity.setType(Entity::SUBJECT);
00227            // 5. add entity to entitylist
00228            entitylist->push_back(entity); 
00229            //myprintf("subject:\n  row=%d,col=%d,",row,col);
00230        }
00231 /*
00232        if  ( (GOAL_MINAREA < entityarea) && (entityarea <= GOAL_MAXAREA ) ) {
00233            // set type 
00234            entity.setType(Entity::GOAL);
00235            // 5. add entity to entitylist
00236            entitylist->push_back(entity); 
00237            //myprintf("goal:\n  row=%d,col=%d,",row,col);
00238        }
00239      */ 
00240        label++;
00241     }
00242 
00243 
00244 
00245 
00246  
00247 
00248         delete_Image(labeled_img);
00249 
00250     return component_img;
00251 
00252 }
00253 
00254 
00255 
00256 
00257 
00258 
00259 
00260 // returns true if non-null pixel found
00261 // row,column : coordinates of this pixel
00262 // inputImage : not changed (not deleted from memory)
00263 bool get_first_non_null_point(Image *inputImage, int *row,int *column)
00264 {
00265     int r, c, rows ,cols;
00266     bool found=false;
00267     byte **image;
00268 
00269 
00270     rows = getNoOfRows_Image(inputImage);
00271     cols = getNoOfCols_Image(inputImage);
00272         image = (unsigned char ** )getData_Image(inputImage, 0);
00273     for(r=0; r < rows; r++) 
00274         {
00275         for(c=0; c < cols; c++) 
00276                 {
00277                 if (!found)
00278                         {
00279                                 if ( image[r][c] != 0 ) 
00280                                 {
00281                                         *row=r;
00282                         *column=c;
00283                                         found = true;
00284                                 }
00285                         }
00286         }
00287     }
00288         
00289         return found;
00290 }
00291 
00292 bool find_labeled_entity(Image *inputImage, int &row,int &col, int &label) {
00293     int r, c, rows ,cols;
00294 
00295     //byte **image;
00296     int **image;
00297 
00298     // get dimensions of image
00299     rows = getNoOfRows_Image(inputImage);
00300     cols = getNoOfCols_Image(inputImage);
00301     // access data in image
00302 //      image = (unsigned char ** )getData_Image(inputImage, 0);
00303     image = (int ** )getData_Image(inputImage, 0);
00304     // search for object with label value 'label'
00305     // starting at 'row','col' until end of image
00306     for(r=row; r < rows; r++) 
00307         {
00308         for(c=0; c < cols; c++) 
00309                 {
00310             // check for new label value which 
00311             //   - must not be equal old label
00312             //   - and label must not be 0 (means no object)
00313             if ( image[r][c] == label ) 
00314                         {
00315                     label=image[r][c];
00316                     row=r;
00317                     col=c;                    
00318                     return true;
00319                         }
00320         }
00321     }
00322     return false;
00323 }
00324 
00325   

Generated on Wed Nov 29 01:27:41 2006 by  doxygen 1.4.6