Rose-Hulman Robotics Team

Changeset 476

Show
Ignore:
Timestamp:
03/29/09 16:10:54 (3 years ago)
Author:
spenceal
Message:

splitting classify into multiple files

Location:
trunk/software/rb/vision
Files:
5 added
3 modified
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/software/rb/vision/classify.c

    r475 r476  
    11#include <stdio.h> 
    2 #include <errno.h> 
    3  
    4 #include <X11/keysym.h> 
    52#include <opencv/cv.h> 
    63#include <opencv/highgui.h> 
    7 #include <opencv/ml.h> 
    84 
    95#include "classify.h" 
     6#include "classify_dtree.h" 
     7#include "classify_hard.h" 
    108 
    119int get_type(CvScalar _in) 
     
    2220} 
    2321 
    24 CvMat *classify_old(IplImage *image) 
    25 { 
    26         CvSize size = {image->width, image->height}; 
    27         CvMat *grass_mask = cvCreateMat(size.height, size.width, CV_8U); 
    28         CvMat *line_mask  = cvCreateMat(size.height, size.width, CV_8U); 
    29         cvInRangeS(image, cvScalar(30, 0, 80, 0), cvScalar(120,90,180,0), grass_mask); 
    30         cvInRangeS(image, cvScalar(100,60,140,0), cvScalar(120,90,254,0), line_mask); 
    31         // TODO, mark obstacles separately 
    32  
    33         /* Morphological operations */ 
    34         int rad = (int)size.width*0.005; 
    35         IplConvKernel *se = cvCreateStructuringElementEx(rad, rad, rad/2, rad/2, CV_SHAPE_RECT, NULL); 
    36         cvDilate(line_mask, line_mask, se, 2); 
    37         cvReleaseStructuringElement(&se); 
    38  
    39         /* Mark outputs */ 
    40         CvMat *output = cvCreateMat(size.height, size.width, CV_8U); 
    41         //cvSet(output, cvRealScalar(RB_IGNORE), NULL); 
    42         cvSet(output, cvRealScalar(RB_GRASS),  grass_mask); 
    43         cvSet(output, cvRealScalar(RB_LINE ),  line_mask); 
    44  
    45         /* Release temporary data */ 
    46         cvReleaseMat(&grass_mask); 
    47         cvReleaseMat(&line_mask); 
    48         return output; 
    49 } 
    50  
    51 CvDTree *dtree_create() 
    52 { 
    53         CvDTree *dtree = new CvDTree; 
    54         return dtree; 
    55 } 
    56  
    57 void dtree_parse_files(CvDTree *dtree, 
    58                        const char *input_file, const char *desired_file, 
    59                        CvMat **input, CvMat **desired, CvMat **missing) 
     22void parse_files(const char *input_file, const char *desired_file, 
     23                       CvMat **input, CvMat **desired) 
    6024{ 
    6125        /* Load training data */ 
     
    8448        *input     = cvCreateMat(out_rows, RB_NUM_CLASSES, CV_32F); 
    8549        *desired   = cvCreateMat(out_rows, 1,              CV_32F); 
    86         *missing   = cvCreateMat(out_rows, RB_NUM_CLASSES, CV_8U); 
    8750        int row, col; 
    8851        for (row = 0; row < size.height; row++) { 
     
    9255                        CvScalar desired_px = cvGet2D(desired_img, row, col); 
    9356                        int desired_type = get_type(desired_px); 
    94                         cvSet2D(*input,   out_row, 0, cvScalar(input_px.val[0])); 
    95                         cvSet2D(*input,   out_row, 1, cvScalar(input_px.val[1])); 
    96                         cvSet2D(*input,   out_row, 2, cvScalar(input_px.val[2])); 
    97                         cvSet2D(*missing, out_row, 0, cvScalar(0)); // No missing data 
    98                         cvSet2D(*missing, out_row, 1, cvScalar(0)); // No missing data 
    99                         cvSet2D(*missing, out_row, 2, cvScalar(0)); // No missing data 
    100                         cvSet2D(*desired, out_row, 0, cvScalar(desired_type)); 
     57                        cvSet2D(*input,   out_row, 0, cvRealScalar(input_px.val[0])); 
     58                        cvSet2D(*input,   out_row, 1, cvRealScalar(input_px.val[1])); 
     59                        cvSet2D(*input,   out_row, 2, cvRealScalar(input_px.val[2])); 
     60                        cvSet2D(*desired, out_row, 0, cvRealScalar(desired_type)); 
    10161                } 
    10262        } 
    103 } 
    104  
    105 /* Train DTree */ 
    106 void dtree_train(CvDTree *dtree, CvMat *input, CvMat *desired, CvMat *missing) 
    107 { 
    108         fprintf(stderr, "Training DTree\n"); 
    109  
    110         float priors[] = {20, 5, 2, 1}; // grass, obsticle, line, ignore 
    111  
    112         CvMat* var_type = cvCreateMat(input->cols+1, 1, CV_8U); 
    113         cvSet(var_type, cvScalarAll(CV_VAR_NUMERICAL)); 
    114         cvSet2D(var_type, input->cols, 0, cvScalarAll(CV_VAR_CATEGORICAL)); 
    115  
    116         dtree->train(input, CV_ROW_SAMPLE, desired, 0, 0, var_type, missing, 
    117                 CvDTreeParams( 
    118                         17,    // Max depth 
    119                         10,    // min sample count 
    120                         0,     // regression accuracy: N/A here 
    121                         false, // compute surrogate split , as we have missing data 
    122                         5,     // max number of categories (use sub-optimal algorithm for larger numbers) 
    123                         10,    // the number of cross-validation folds 
    124                         false, // use 1SE rule => smaller tree 
    125                         true,  // throw away the pruned tree branches 
    126                         priors // the array of priors , the bigger p_weight, the more attention 
    127                 ) 
    128         ); 
    129 } 
    130 CvMat *dtree_predict(CvDTree *dtree, IplImage *input) 
    131 { 
    132         fprintf(stderr, "Predicting using DTree\n"); 
    133  
    134         CvSize size = {input->width, input->height}; 
    135  
    136         CvMat *output = cvCreateMat(size.height, size.width, CV_8U); 
    137         int row, col; 
    138         for (row = 0; row < size.height; row++) { 
    139                 for (col = 0; col < size.width; col++) { 
    140                         CvMat *sample = cvCreateMat(1, RB_NUM_CLASSES, CV_32F); 
    141                         CvScalar input_px = cvGet2D(input, row, col); 
    142  
    143                         cvSet2D(sample, 0, 0, cvScalar(input_px.val[0])); 
    144                         cvSet2D(sample, 0, 1, cvScalar(input_px.val[1])); 
    145                         cvSet2D(sample, 0, 2, cvScalar(input_px.val[2])); 
    146  
    147                         double predicted_px = dtree->predict(sample, NULL)->value; 
    148  
    149                         cvSet2D(output, row, col, cvScalar(predicted_px)); 
    150                 } 
    151         } 
    152  
    153         return output; 
    15463} 
    15564 
     
    15766{ 
    15867        fprintf(stderr, "Starting classificaiton\n"); 
    159         CvDTree *dtree = dtree_create(); 
    160         CvMat *input, *desired, *missing; 
    161         dtree_parse_files(dtree, 
    162                         "data/classes/input.jpeg", "data/classes/classes.png", 
    163                         &input, &desired, &missing); 
    164         dtree_train(dtree, input, desired, missing); 
     68        CvMat *input, *desired; 
     69        parse_files("data/classes/input.jpeg", "data/classes/classes.png", 
     70                    &input, &desired); 
     71        CvCDTree *dtree = dtree_create(); 
     72        dtree_train(dtree, input, desired); 
    16573        CvMat *predictions = dtree_predict(dtree, image); 
    16674        fprintf(stderr, "Finished classificaiton\n"); 
  • trunk/software/rb/vision/classify.h

    r475 r476  
    1 #ifdef __cplusplus 
    2 extern "C" { 
    3 #endif 
    4  
    51/** 
    62 * Standard pixel values 
     
    2622 */ 
    2723CvMat *classify(IplImage *hsv);  
    28  
    29 #ifdef __cplusplus 
    30 } 
    31 #endif 
  • trunk/software/rb/vision/makefile

    r474 r476  
    2323#       #python $PROG.py test_image2.jpeg 
    2424 
    25 main: main.o vision.o classify.o 
     25main: main.o vision.o classify.o classify_hard.o classify_dtree.o 
    2626        gcc -o $@ $+ $(LDFLAGS) 
    2727 
  • trunk/software/rb/vision/vision.c

    r475 r476  
    4949         
    5050        /* transform image */ 
    51         //IplImage *transformed = cvCreateImage(size, img->depth, img->nChannels); 
    52         //CvMat* mat= cvCreateMat( 3, 3, CV_32F ); 
    53         //CvPoint2D32f from[] = { 
    54         //      cvPoint2D32f(0           , size.height*0.12), 
    55         //      cvPoint2D32f(0           , size.height-1), 
    56         //      cvPoint2D32f(size.width-1, size.height-1), 
    57         //      cvPoint2D32f(size.width-1, size.height*0.12) 
    58         //}; 
    59         //CvPoint2D32f to[] = { 
    60         //      cvPoint2D32f(0              , -62           ), 
    61         //      cvPoint2D32f(size.width*0.33, size.height-1), 
    62         //      cvPoint2D32f(size.width*0.67, size.height-1), 
    63         //      cvPoint2D32f(size.width-1   , -62            ) 
    64         //}; 
    65         //cvGetPerspectiveTransform(from, to, mat); 
    66         //cvWarpPerspective(img, transformed, mat, CV_WARP_FILL_OUTLIERS, cvScalarAll(0)); 
    67         //cvReleaseImage(&img); 
    68         //return transformed; 
    69         return img; 
     51        IplImage *transformed = cvCreateImage(size, img->depth, img->nChannels); 
     52        CvMat* mat= cvCreateMat( 3, 3, CV_32F ); 
     53        CvPoint2D32f from[] = { 
     54                cvPoint2D32f(0           , size.height*0.12), 
     55                cvPoint2D32f(0           , size.height-1), 
     56                cvPoint2D32f(size.width-1, size.height-1), 
     57                cvPoint2D32f(size.width-1, size.height*0.12) 
     58        }; 
     59        CvPoint2D32f to[] = { 
     60                cvPoint2D32f(0              , -62          ), 
     61                cvPoint2D32f(size.width*0.33, size.height-1), 
     62                cvPoint2D32f(size.width*0.67, size.height-1), 
     63                cvPoint2D32f(size.width-1   , -62          ) 
     64        }; 
     65        cvGetPerspectiveTransform(from, to, mat); 
     66        cvWarpPerspective(img, transformed, mat, CV_WARP_FILL_OUTLIERS, cvScalarAll(0)); 
     67        cvReleaseImage(&img); 
     68        return transformed; 
    7069} 
    7170