Rose-Hulman Robotics Team

Changeset 479

Show
Ignore:
Timestamp:
04/02/09 01:07:42 (3 years ago)
Author:
spenceal
Message:

factoring out classification code

Location:
trunk/software
Files:
13 modified

Legend:

Unmodified
Added
Removed
  • trunk/software/Makefile

    r469 r479  
    1616 
    1717clean: 
    18         rm -f *.so 
    19         rm -fR rb/*.so 
    20         rm -fR rb/*/*.so 
    21         rm -f *.pyc 
    22         rm -fR rb/*.pyc 
    23         rm -fR rb/*/*.pyc 
     18        rm -rf build/* 
     19        rm -f *.{o,so,pyc} 
     20        rm -f rb/*.{o,so,pyc} 
     21        rm -f rb/*/*.{o,so,pyc} 
    2422 
    2523rungui: 
  • trunk/software/rb/vision/_vision.c

    r469 r479  
    2323 
    2424#include "vision.h" 
     25#include "classify.h" 
    2526 
    2627/* TODO: define some constants for the types of points */ 
     
    3233        PyObject *points = PyList_New(0); 
    3334 
     35 
     36        classifier_t *classifier = classifier_new(RB_DTREE); 
     37        classifier_add_data_from_files(classifier, 
     38                        "rb/vision/data/classes/input.jpeg", 
     39                        "rb/vision/data/classes/classes.png"); 
     40        classifier_train(classifier); 
     41 
    3442        IplImage *input       = get_image(); 
    35         IplImage *transformed = transform_image(input, 0.25); 
    36         CvMat    *classes     = classify_image(transformed); 
     43        IplImage *transformed = transform_image(input, 0.50); 
     44        CvMat    *classes     = classifier_predict(classifier, transformed); 
    3745        CvSeq    *seq         = get_points(classes); 
    3846 
     
    4250                int data[3]; 
    4351                CV_READ_SEQ_ELEM(data, reader); 
    44                 PyObject *point = Py_BuildValue("iii", data[0], data[1], data[2]); 
     52                PyObject *point = Py_BuildValue("iii", 
     53                                data[0], data[1], data[2]); 
    4554                PyList_Append(points, point); 
    4655        } 
  • trunk/software/rb/vision/classify.c

    r476 r479  
    33#include <opencv/highgui.h> 
    44 
     5#include "vision.h" 
    56#include "classify.h" 
    67#include "classify_dtree.h" 
     
    1213        for (int type = 0; type < RB_NUM_CLASSES; type++) { 
    1314                const double *px = class_colors[type].val; 
    14                 if (in[0]==px[0] && in[1]==px[1] && in[2]==px[2]) { 
     15                if (in[0]==px[0] && in[1]==px[1] && in[2]==px[2]) 
    1516                        return type; 
    16                 } 
    1717        } 
    1818        printf("Warning, unknown type: (%f,%f,%f)\n", in[0], in[1], in[2]); 
     
    2020} 
    2121 
    22 void parse_files(const char *input_file, const char *desired_file, 
    23                        CvMat **input, CvMat **desired) 
     22#define COPY_PX(row, col, px_num) { \ 
     23        CvScalar input_px = cvGet2D(hsv, row, col); \ 
     24        cvSet2D(input, out_row, (px_num*3)+0, cvRealScalar(input_px.val[0])); \ 
     25        cvSet2D(input, out_row, (px_num*3)+1, cvRealScalar(input_px.val[1])); \ 
     26        cvSet2D(input, out_row, (px_num*3)+2, cvRealScalar(input_px.val[2])); \ 
     27} 
     28/* Convert a raw RGB input image to a CvMat suitable for learning algorithms */ 
     29CvMat *image_to_input(IplImage *image) 
    2430{ 
    25         /* Load training data */ 
    26         fprintf(stderr, "Loading training images\n"); 
    27         IplImage *_input_img   = cvLoadImage(input_file,   1); 
    28         IplImage *_desired_img = cvLoadImage(desired_file, 1); 
     31        CvSize size = {image->width, image->height}; 
    2932 
    30         /* Scaling input */ 
    31         CvSize size = {_input_img->width*0.25, _input_img->height*0.25}; 
    32         IplImage *input_img   = cvCreateImage(size, _input_img->depth,   _input_img->nChannels); 
    33         IplImage *desired_img = cvCreateImage(size, _desired_img->depth, _desired_img->nChannels); 
    34         cvResize(_input_img,   input_img,   CV_INTER_LINEAR); 
    35         cvResize(_desired_img, desired_img, CV_INTER_NN); 
     33        /* Smooth image */ 
     34        print_time("Smoothing"); 
     35        IplImage *smoothed = cvCreateImage(size, image->depth, image->nChannels); 
     36        cvSmooth(image, smoothed, CV_BLUR, 3, 3, 0, 0); 
    3637 
    37         /* Blur */ 
    38         cvSmooth(input_img, input_img, CV_BLUR, 3, 3, 0, 0); 
     38        /* Convert image to HSV */ 
     39        print_time("To HSV"); 
     40        IplImage *hsv = cvCreateImage(size, image->depth, image->nChannels); 
     41        cvCvtColor(smoothed, hsv, CV_BGR2HSV); 
    3942 
    40         /* Convert to HSV */ 
    41         fprintf(stderr, "Preprocessing training images\n"); 
    42         cvCvtColor(input_img,   input_img,   CV_BGR2HSV); 
    43  
    44         /* Convert IplImage to DTree input format */ 
    45         fprintf(stderr, "Parsing training images to DTree input\n"); 
     43        /* Create matrix from image */ 
    4644        int out_rows = size.height * size.width; 
    4745        fprintf(stderr, "Creating %dx%d = %d input\n", size.width, size.height, out_rows); 
    48         *input     = cvCreateMat(out_rows, RB_NUM_CLASSES, CV_32F); 
    49         *desired   = cvCreateMat(out_rows, 1,              CV_32F); 
     46        CvMat *input = cvCreateMat(out_rows, 3*1, CV_32F); 
    5047        int row, col; 
    5148        for (row = 0; row < size.height; row++) { 
    5249                for (col = 0; col < size.width; col++) { 
    5350                        int out_row = row*size.width + col; 
    54                         CvScalar input_px   = cvGet2D(input_img,   row, col); 
    55                         CvScalar desired_px = cvGet2D(desired_img, row, col); 
    56                         int desired_type = get_type(desired_px); 
    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)); 
     51                        COPY_PX(row,   col,   0); 
     52                        //if (col-5 >= 0         ) COPY_PX(row,   col-5, 1); 
     53                        //if (col+5 < size.width ) COPY_PX(row,   col+5, 2); 
     54                        //if (row-5 >= 0         ) COPY_PX(row-5, col,   3); 
     55                        //if (row+5 < size.height) COPY_PX(row+5, col,   4); 
    6156                } 
     57        } 
     58 
     59        /* Done */ 
     60        cvReleaseImage(&smoothed); 
     61        cvReleaseImage(&hsv); 
     62        return input; 
     63} 
     64/* Convert a hand classified RGB input image to a CvMat suitable for learning algorithms */ 
     65CvMat *image_to_desired(IplImage *image) 
     66{ 
     67        /* Create matrix from image */ 
     68        CvMat *desired = cvCreateMat(image->height, image->width, CV_32F); 
     69        for (int row = 0; row < image->height; row++) 
     70                for (int col = 0; col < image->width; col++) 
     71                        cvSet2D(desired, row, col, 
     72                                cvRealScalar(get_type(cvGet2D(image, row, col)))); 
     73 
     74        cvReshape(desired, desired, 1, image->height * image->width); 
     75        return desired; 
     76} 
     77 
     78 
     79classifier_t *classifier_new(int type) 
     80{ 
     81        classifier_t *classifier = malloc(sizeof(classifier_t)); 
     82        classifier->type = type; 
     83        classifier->train_input   = cvCreateMat(1, 3, CV_32F); // FIXME: number of pixles may change 
     84        classifier->train_desired = cvCreateMat(1, 1, CV_32F); 
     85        switch (type) { 
     86        case RB_DTREE: classifier->classifier = dtree_create(); break; 
     87        case RB_HARD:  break; 
     88        } 
     89        return classifier; 
     90} 
     91 
     92void classifier_add_data(classifier_t *classifier, CvMat *input, CvMat *desired) 
     93{ 
     94        // FIXME: This currently wipes out any existing data 
     95        cvReleaseMat(&classifier->train_input); 
     96        cvReleaseMat(&classifier->train_desired); 
     97        classifier->train_input   = cvCloneMat(input); 
     98        classifier->train_desired = cvCloneMat(desired); 
     99} 
     100 
     101// "data/classes/input.jpeg" 
     102// "data/classes/classes.png" 
     103void classifier_add_data_from_files(classifier_t *classifier, const char *input_file, const char *desired_file) 
     104{ 
     105        IplImage *input_img   = cvLoadImage(input_file,   1); 
     106        IplImage *desired_img = cvLoadImage(desired_file, 1); 
     107 
     108        CvSize size = cvSize(input_img->width*0.25, input_img->height*0.25); 
     109        IplImage *input_img_s   = cvCreateImage(size, input_img->depth, input_img->nChannels); 
     110        IplImage *desired_img_s = cvCreateImage(size, desired_img->depth, desired_img->nChannels); 
     111        cvResize(input_img,   input_img_s,   CV_INTER_LINEAR); 
     112        cvResize(desired_img, desired_img_s, CV_INTER_NN); 
     113        cvReleaseImage(&input_img); 
     114        cvReleaseImage(&desired_img); 
     115 
     116        CvMat *input_mat   = image_to_input(input_img_s); 
     117        CvMat *desired_mat = image_to_desired(desired_img_s); 
     118        cvReleaseImage(&input_img_s); 
     119        cvReleaseImage(&desired_img_s); 
     120 
     121        classifier_add_data(classifier, input_mat, desired_mat); 
     122        cvReleaseMat(&input_mat); 
     123        cvReleaseMat(&desired_mat); 
     124} 
     125 
     126void classifier_add_data_class(classifier_t *classifier, CvMat *input, int desired) 
     127{ 
     128        // TODO 
     129} 
     130 
     131void classifier_train(classifier_t *classifier) 
     132{ 
     133        switch (classifier->type) { 
     134        case RB_DTREE: dtree_train(classifier->classifier, classifier->train_input, classifier->train_desired); break; 
     135        case RB_HARD:  /* no training */ break; 
    62136        } 
    63137} 
    64138 
    65 CvMat *classify(IplImage *image) 
     139CvMat *classifier_predict(classifier_t *classifier, IplImage *image) 
    66140{ 
    67         fprintf(stderr, "Starting classificaiton\n"); 
    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); 
    73         CvMat *predictions = dtree_predict(dtree, image); 
    74         fprintf(stderr, "Finished classificaiton\n"); 
     141        CvMat *predictions = NULL; 
     142        CvMat *input = image_to_input(image); 
     143 
     144        switch (classifier->type) { 
     145        case RB_DTREE: predictions = dtree_predict(classifier->classifier, input); break; 
     146        case RB_HARD:  predictions = hard_predict(classifier->classifier,  image); break; 
     147        } 
     148 
     149        cvReleaseMat(&input); 
     150        cvReshape(predictions, predictions, 1, image->height); 
    75151        return predictions; 
    76         //return classify_old(image); 
    77152} 
  • trunk/software/rb/vision/classify.h

    r476 r479  
    44 * The classified image will be represented by these colors 
    55 */ 
    6 typedef enum rb_classes_t { 
     6enum { 
    77        RB_GRASS, 
    88        RB_OBSTICLE, 
     
    1010        RB_IGNORE  , 
    1111        RB_NUM_CLASSES 
    12 } rb_classes_t; 
     12}; 
    1313const static CvScalar class_colors[] = { 
    1414        {{0*255, 1*255, 0*255, 0}}, // Green   
     
    1818}; 
    1919 
     20enum { 
     21        RB_DTREE, // Decision tree 
     22        RB_HARD,  // Hard coded classifier 
     23        RB_NUM_CLASSIFIERS 
     24}; 
     25 
    2026/** 
    21  * Do pixel classifications using a decision tree 
     27 * Classifiers, these store the actual OpenCV classifier along with some 
     28 * additional stuff for adding/removing data from them. 
    2229 */ 
    23 CvMat *classify(IplImage *hsv);  
     30typedef struct { 
     31        /* Type of classifier */ 
     32        int type; 
     33 
     34        /* The a classifier itself */ 
     35        void *classifier; 
     36 
     37        /* Training data */ 
     38        CvMat *train_input;   // Nx3 (HSV values) 
     39        CvMat *train_desired; // Nx1 (classes) 
     40} classifier_t; 
     41 
     42 
     43/** 
     44 * Create a new classifier of the given type 
     45 */ 
     46classifier_t *classifier_new(int type); 
     47 
     48 
     49/** 
     50 * Append data to the train_input and train_desired fields 
     51 */ 
     52void classifier_add_data(classifier_t *classifier, CvMat *input, CvMat *desired); 
     53/** 
     54 * Loads data from input files before appending it to the training data 
     55 */ 
     56void classifier_add_data_from_files(classifier_t *classifier, const char *input, const char *desired); 
     57/** 
     58 * Adds training data that is all of the same class 
     59 */ 
     60void classifier_add_data_class(classifier_t *classifier, CvMat *input, int desired); 
     61 
     62 
     63/** 
     64 * Train the classifier based on the data store within it 
     65 */ 
     66void classifier_train(classifier_t *classifier); 
     67 
     68 
     69/** 
     70 * Train the classifier based on the data store within it 
     71 */ 
     72CvMat *classifier_predict(classifier_t *classifier, IplImage *image); 
  • trunk/software/rb/vision/classify_dtree.cpp

    r476 r479  
    2626        dtree->train(input, CV_ROW_SAMPLE, desired, 0, 0, var_type, NULL, 
    2727                CvDTreeParams( 
    28                         17,    // Max depth 
    29                         10,    // min sample count 
    30                         0,     // regression accuracy: N/A here 
    31                         false, // no not compute surrogate split 
    32                         5,    // max number of categories (use sub-optimal algorithm for larger numbers) 
    33                         10,    // the number of cross-validation folds 
    34                         false, // use 1SE rule => smaller tree 
    35                         true,  // throw away the pruned tree branches 
    36                         priors // the array of priors , the bigger p_weight, the more attention 
     28                        15,             // Max depth 
     29                        50,             // min sample count 
     30                        0,              // regression accuracy: N/A here 
     31                        false,          // no not compute surrogate split 
     32                        RB_NUM_CLASSES, // max number of categories (use sub-optimal algorithm for larger numbers) 
     33                        20,             // the number of cross-validation folds 
     34                        false,          // use 1SE rule => smaller tree 
     35                        true,           // throw away the pruned tree branches 
     36                        priors          // the array of priors , the bigger p_weight, the more attention 
    3737                ) 
    3838        ); 
    3939} 
    40 CvMat *dtree_predict(CvCDTree *_dtree, IplImage *input) 
     40/* todo, return a 1xN matrix and let the caller reshape it */ 
     41CvMat *dtree_predict(CvCDTree *_dtree, CvMat *input) 
    4142{ 
    4243        CvDTree *dtree = (CvDTree *)_dtree; 
    4344        fprintf(stderr, "Predicting using DTree\n"); 
    4445 
    45         CvSize size = {input->width, input->height}; 
    46  
    47         CvMat *output = cvCreateMat(size.height, size.width, CV_8U); 
    48         int row, col; 
    49         for (row = 0; row < size.height; row++) { 
    50                 for (col = 0; col < size.width; col++) { 
    51                         CvMat *sample = cvCreateMat(1, RB_NUM_CLASSES, CV_32F); 
    52                         CvScalar input_px = cvGet2D(input, row, col); 
    53  
    54                         cvSet2D(sample, 0, 0, cvScalar(input_px.val[0])); 
    55                         cvSet2D(sample, 0, 1, cvScalar(input_px.val[1])); 
    56                         cvSet2D(sample, 0, 2, cvScalar(input_px.val[2])); 
    57  
    58                         double predicted_px = dtree->predict(sample, NULL)->value; 
    59  
    60                         cvSet2D(output, row, col, cvScalar(predicted_px)); 
    61                 } 
     46        CvMat *output = cvCreateMat(input->height, 1, CV_8U); 
     47        CvMat *sample = cvCreateMat(1, RB_NUM_CLASSES, CV_32F); 
     48        for (int row = 0; row < input->height; row++) { 
     49                sample = cvGetRow(input, sample, row); 
     50                double predicted_px = dtree->predict(sample, NULL)->value; 
     51                cvSet2D(output, row, 0, cvScalar(predicted_px)); 
    6252        } 
    6353 
  • trunk/software/rb/vision/classify_dtree.h

    r476 r479  
    99CvCDTree *dtree_create(); 
    1010void dtree_train(CvCDTree *dtree, CvMat *input, CvMat *desired); 
    11 CvMat *dtree_predict(CvCDTree *dtree, IplImage *input); 
     11CvMat *dtree_predict(CvCDTree *dtree, CvMat *input); 
    1212 
    1313#ifdef __cplusplus 
  • trunk/software/rb/vision/classify_hard.c

    r476 r479  
    44#include "classify_hard.h" 
    55 
    6 CvMat *classify_old(IplImage *image) 
     6// /* Morphological operations */ 
     7// int rad = (int)size.width*0.005; 
     8// IplConvKernel *se = cvCreateStructuringElementEx(rad, rad, rad/2, rad/2, CV_SHAPE_RECT, NULL); 
     9// cvDilate(line_mask, line_mask, se, 2); 
     10// cvReleaseStructuringElement(&se); 
     11 
     12CvMat *hard_predict(classifier_t *classifier, IplImage *image) 
    713{ 
     14        /* TODO: convert to using CvMat*  */ 
     15        /* Smooth image */ 
    816        CvSize size = {image->width, image->height}; 
     17        IplImage *smoothed = cvCreateImage(size, image->depth, image->nChannels); 
     18        cvSmooth(image, smoothed, CV_BLUR, 3, 3, 0, 0); 
     19 
     20        /* Convert image to HSV */ 
     21        IplImage *hsv = cvCreateImage(size, image->depth, image->nChannels); 
     22        cvCvtColor(smoothed, hsv, CV_BGR2HSV); 
     23 
     24        /* Masks */ 
    925        CvMat *grass_mask = cvCreateMat(size.height, size.width, CV_8U); 
    1026        CvMat *line_mask  = cvCreateMat(size.height, size.width, CV_8U); 
    11         cvInRangeS(image, cvScalar(30, 0, 80, 0), cvScalar(120,90,180,0), grass_mask); 
    12         cvInRangeS(image, cvScalar(100,60,140,0), cvScalar(120,90,254,0), line_mask); 
    13         // TODO, mark obstacles separately 
    14  
    15         /* Morphological operations */ 
    16         int rad = (int)size.width*0.005; 
    17         IplConvKernel *se = cvCreateStructuringElementEx(rad, rad, rad/2, rad/2, CV_SHAPE_RECT, NULL); 
    18         cvDilate(line_mask, line_mask, se, 2); 
    19         cvReleaseStructuringElement(&se); 
     27        cvInRangeS(hsv, cvScalar(30, 0, 80, 0), cvScalar(120,90,180,0), grass_mask); 
     28        cvInRangeS(hsv, cvScalar(100,60,140,0), cvScalar(120,90,254,0), line_mask); 
    2029 
    2130        /* Mark outputs */ 
    2231        CvMat *output = cvCreateMat(size.height, size.width, CV_8U); 
    23         //cvSet(output, cvRealScalar(RB_IGNORE), NULL); 
    24         cvSet(output, cvRealScalar(RB_GRASS),  grass_mask); 
    25         cvSet(output, cvRealScalar(RB_LINE ),  line_mask); 
     32        cvSet(output, cvRealScalar(RB_OBSTICLE), NULL); 
     33        cvSet(output, cvRealScalar(RB_GRASS),    grass_mask); 
     34        cvSet(output, cvRealScalar(RB_LINE ),    line_mask); 
    2635 
    2736        /* Release temporary data */ 
  • trunk/software/rb/vision/classify_hard.h

    r476 r479  
    11/* Hard coded classifier */ 
    2 CvMat *classify_old(IplImage *image); 
     2CvMat *hard_predict(classifier_t *classifier, IplImage *image); 
  • trunk/software/rb/vision/main.c

    r474 r479  
    4141 
    4242        /* Process */ 
     43        print_time("Training classifier"); 
     44        classifier_t *classifier = classifier_new(RB_DTREE); 
     45        classifier_add_data_from_files(classifier, 
     46                        "data/classes/input.jpeg", 
     47                        "data/classes/classes.png"); 
     48        classifier_train(classifier); 
     49 
    4350        print_time("Processing images"); 
    44         IplImage *transformed = transform_image(input, 0.25); 
    45         CvMat    *classes     = classify_image(transformed); 
     51        IplImage *transformed = transform_image(input, 0.50); 
     52        CvMat    *classes     = classifier_predict(classifier, transformed); 
    4653        IplImage *colors      = color_mat(classes); 
    4754 
  • trunk/software/rb/vision/makefile

    r476 r479  
    1 CFLAGS  =-p -pg -g -Wall -pedantic --std=gnu99 -fgnu89-inline -O3 $(shell pkg-config --cflags opencv gsl) 
    2 CPPFLAGS=-p -pg -g -Wall -pedantic -O3 $(shell pkg-config --cflags opencv gsl) 
    3 LDFLAGS =-lpython2.5 $(shell pkg-config --libs opencv gsl) 
    4 DLFLAGS =-shared -nostartfiles -fPIC 
     1CFLAGS   = -p -pg -g -Wall -pedantic --std=gnu99 -fgnu89-inline -O3 $(shell pkg-config --cflags opencv gsl) 
     2CPPFLAGS = -p -pg -g -Wall -pedantic -O3 $(shell pkg-config --cflags opencv gsl) 
     3LDFLAGS  = $(shell pkg-config --libs opencv gsl) 
     4DLFLAGS  = -shared -nostartfiles -fPIC 
     5PYFLAGS  = -I/usr/include/python2.5/ 
     6PYLIBS   = -lpython2.5 
    57 
     8SRCS=vision.c classify.c classify_hard.c classify_dtree.c 
     9OBJS=$(subst .c,.o,$(SRCS)) 
    610PROG=main 
    7 CPROG=$(PROG) 
    8 PPROG=$(PROG).py 
     11LIB=_vision.so 
    912 
    10 #test:VQ: _ml 
    11 #       ./_ml data/both.log 
    12 #       #./_hough ../../training_course/cam-000077.jpeg 
     13default: $(PROG) $(LIB) test 
    1314 
    14 test: $(CPROG) 
    15         #./$CPROG ./line.jpeg 
    16 #       #./$CPROG ./grass.jpeg 
    17 #       #./$CPROG ./notgrass.jpeg 
    18         ./$(CPROG) ./data/classes/input.jpeg ./data/classes/classes.png 
    19 #       ./$(CPROG) ./tile.png 
    20 #       #./$CPROG ./lines.jpeg 
    21 #       #./$CPROG ./lines.jpeg 
    22 #       #for (i in 00*.jpeg) ./$CPROG $i 
    23 #       #python $PROG.py test_image2.jpeg 
     15test: $(PROG) 
     16        ./$(PROG) ./data/images/cam-000043.jpeg 
     17        #./$(PROG) ./data/classes/input.jpeg 
    2418 
    25 main: main.o vision.o classify.o classify_hard.o classify_dtree.o 
    26         gcc -o $@ $+ $(LDFLAGS) 
     19# Python rules 
     20%.so: %.o $(OBJS) 
     21        gcc $(CFLAGS) $(DLFLAGS) -o $@ $+ $(LDFLAGS) $(PYLIBS) 
     22_%.o: _%.c 
     23        gcc $(CFLAGS) $(PYFLAGS) -c -o $@ $+ 
    2724 
    28 %.o: %.cpp 
    29         g++ $(CPPFLAGS) -c -o $@ $< 
    30  
    31 %.o: %.c 
    32         gcc $(CFLAGS) -c -o $@ $< 
    33  
    34 _%: _%.c 
     25# C rules 
     26main: main.o $(OBJS) 
    3527        gcc $(CFLAGS) -o $@ $+ $(LDFLAGS) 
    3628 
    37 _%.so: _%.c 
    38         gcc $(CFLAGS) $(DLFLAGS) -o $@ $+ $(LDFLAGS) 
     29# Object rules 
     30%.o: %.cpp 
     31        g++ $(CPPFLAGS) -c -o $@ $+ 
     32%.o: %.c 
     33        gcc $(CFLAGS) -c -o $@ $+ 
    3934 
     35# Misc 
    4036tags: 
    4137        ctags -R 
    42  
    4338clean: 
    44         rm -f $(CPROG) *.o *.so *.pyc core out*.jpeg *.out tags 
     39        rm -f $(PROG) *.o *.so *.pyc core out*.jpeg *.out tags 
  • trunk/software/rb/vision/vision.c

    r476 r479  
    3131IplImage *get_image() 
    3232{ 
    33         char *file = "rb/vision/data/classes/input.jpeg"; 
     33        const char *file = "rb/vision/data/classes/input.jpeg"; 
    3434        IplImage *image = cvLoadImage(file, 1); 
    3535        if (!image) { 
     
    6767        cvReleaseImage(&img); 
    6868        return transformed; 
    69 } 
    70  
    71 CvMat *classify_image(IplImage *input) 
    72 { 
    73         CvSize size = {input->width, input->height}; 
    74         IplImage *img = cvCreateImage(size, input->depth, input->nChannels); 
    75         cvCopy(input, img, NULL); 
    76  
    77         /* Smooth image */ 
    78         print_time("Smoothing"); 
    79         cvSmooth(img, img, CV_BLUR, 3, 3, 0, 0); 
    80  
    81         /* Convert image to HSV */ 
    82         print_time("To HSV"); 
    83         IplImage *hsv = cvCreateImage(size, img->depth, img->nChannels); 
    84         cvCvtColor(img, hsv, CV_BGR2HSV); 
    85  
    86         /* Threshold image */ 
    87         print_time("Marking types"); 
    88         CvMat *classes = classify(hsv); 
    89         //CvMat *grass_mask = cvCreateMat(size.height, size.width, CV_8U); 
    90         //CvMat *line_mask  = cvCreateMat(size.height, size.width, CV_8U); 
    91         //cvInRangeS(hsv, cvScalar(30, 0, 80, 0), cvScalar(120,90,180,0), grass_mask); 
    92         //cvInRangeS(hsv, cvScalar(100,60,140,0), cvScalar(120,90,254,0), line_mask); 
    93         // TODO, mark obstacles separately 
    94  
    95         /* Morphological operations */ 
    96         //print_time("Expanding lines"); 
    97         //int rad = (int)size.width*0.005; 
    98         //IplConvKernel *se = cvCreateStructuringElementEx(rad, rad, rad/2, rad/2, CV_SHAPE_RECT, NULL); 
    99         //cvDilate(line_mask, line_mask, se, 2); 
    100         //cvReleaseStructuringElement(&se); 
    101  
    102         /* Mark outputs */ 
    103         //CvMat *output = cvCreateMat(input->height, input->width, CV_8U); 
    104         //cvSet(output, cvRealScalar(RB_IGNORE), NULL); 
    105         //cvSet(output, cvRealScalar(RB_GRASS),  grass_mask); 
    106         //cvSet(output, cvRealScalar(RB_LINE ),  line_mask); 
    107  
    108         /* Release temporary data */ 
    109         //cvReleaseMat(&grass_mask); 
    110         //cvReleaseMat(&line_mask); 
    111         cvReleaseImage(&hsv); 
    112         cvReleaseImage(&img); 
    113  
    114         return classes; 
    11569} 
    11670 
     
    248202} 
    249203 
    250 void print_time(char *label) 
     204void print_time(const char *label) 
    251205{ 
    252206        struct timeb tp; 
  • trunk/software/rb/vision/vision.h

    r474 r479  
    9696 * This is used for profiling the software 
    9797 */ 
    98 void print_time(char *label); 
     98void print_time(const char *label); 
    9999 
    100100#endif 
  • trunk/software/setup.py

    r469 r479  
    2727vision_module  = Extension( 
    2828        'rb.vision._vision', 
    29         ['rb/vision/_vision.c', 'rb/vision/vision.c', 'rb/vision/main.c'], 
     29        ['rb/vision/_vision.c', 
     30         'rb/vision/vision.c', 
     31         'rb/vision/classify.c', 
     32         'rb/vision/classify_hard.c', 
     33         'rb/vision/classify_dtree.cpp'], 
    3034        libraries=['cv', 'highgui', 'gsl'], 
    31         extra_compile_args=['--std=c99'] 
     35        extra_compile_args=['-I/usr/include/opencv -lcv -lhighgui -lgsl'] 
    3236); 
    3337