| 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) |
| | 22 | void parse_files(const char *input_file, const char *desired_file, |
| | 23 | CvMat **input, CvMat **desired) |
| 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)); |
| 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; |
| 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); |