Changeset 230
- Timestamp:
- 10/15/08 23:11:59 (3 years ago)
- Location:
- trunk/software/rb/vision
- Files:
-
- 4 modified
Legend:
- Unmodified
- Added
- Removed
-
trunk/software/rb/vision/main.c
r229 r230 86 86 /* Mark slopes */ 87 87 print_time("Marking slopes"); 88 mark_slopes(out); // This currently segfaults88 mark_slopes(out); 89 89 90 90 /* Mark straint path */ -
trunk/software/rb/vision/makefile
r214 r230 1 CFLAGS=-p -pg -g -Wall -pedantic --std=gnu99 -fgnu89-inline -O3 -I/usr/local/include/opencv $(shell pkg-config --cflags libgoffice-0.6)2 LDFLAGS=-lpython2.5 -lcv -lhighgui -lml $(shell pkg-config --libs libgoffice-0.6)1 CFLAGS=-p -pg -g -Wall -pedantic --std=gnu99 -fgnu89-inline -O3 $(shell pkg-config --cflags opencv gsl) 2 LDFLAGS=-lpython2.5 $(shell pkg-config --libs opencv gsl) 3 3 DLFLAGS=-shared -nostartfiles -fPIC 4 4 … … 22 22 23 23 main: main.o vision.o pathplan.o 24 gcc $(CFLAGS)-o $@ $+ $(LDFLAGS)24 gcc -o $@ $+ $(LDFLAGS) 25 25 26 26 %.o: %.c %.h 27 gcc -o $@ $*.c -c $(CFLAGS)27 gcc $(CFLAGS) -c -o $@ $*.c 28 28 29 29 _%: _%.c -
trunk/software/rb/vision/vision.c
r229 r230 23 23 #include <opencv/cv.h> 24 24 #include <X11/keysym.h> 25 #include <gsl/gsl_fit.h> 25 26 26 27 #include "vision.h" 27 28 28 29 /* Determine if the path between two points is all grass */ 29 int is_clear(IplImage *out, CvPoint start, CvPoint end) { 30 int is_clear(IplImage *out, CvPoint start, CvPoint end) 31 { 30 32 int x, y; 31 33 int i; … … 52 54 /* Find the first non-grass point starting at `start' 53 55 * Each time, increment y by `1' and x by `x_inc', */ 54 CvPoint find_obsticle(IplImage *out, CvPoint2D32f start, float x_inc) { 56 CvPoint find_obsticle(IplImage *out, CvPoint2D32f start, float x_inc) 57 { 55 58 float x = start.x, y = start.y; 56 59 //printf("start = (%f,%f), incr = %f\n", x, y, x_inc); … … 68 71 69 72 /* Rightmost pixel from leftmost blob :) */ 70 CvPoint find_px_at_y(IplImage *out, int y_line, px_t target) { 73 CvPoint find_px_at_y(IplImage *out, int y_line, px_t target) 74 { 71 75 int x; 72 76 for (x = 0; x < out->width; x++) { … … 80 84 } 81 85 82 /* Fits a line to a slope at the Y */ 83 double find_slope_in_box(IplImage *out, CvRect box, go_regression_stat_t *reg) { 84 int xdim = 1; 85 int n = box.height * box.width; 86 double **xss = malloc(sizeof(double*)*xdim); 87 xss[0] = malloc(sizeof(double)*n); 88 double *ys = malloc(sizeof(double)*n); 89 double *res = malloc(sizeof(double)*(n+1)); 90 91 int x, y; 86 int find_slope_in_box(IplImage *out, CvRect box, CvLine *linebox, double *yint, double *slope) 87 { 88 int n = box.height * box.width; 89 double *xs = malloc(sizeof(double)*n); 90 double *ys = malloc(sizeof(double)*n); 91 92 int x, y, minx = -1, miny = -1, maxx = -1, maxy = -1; 92 93 n = 0; // find out how many we _actually_ have.. 93 94 for (x = box.x; x < box.x+box.width; x++) { 94 95 for (y = box.y; y < box.y+box.height; y++) { 95 96 if (IS_PX(GET_PX(out, x, y), PX_LINE)) { 96 xs s[0][n] = x;97 xs[n] = x; 97 98 ys[n] = y; 98 99 n++; 100 if (x < minx || minx == -1) minx = x; 101 if (x > maxx || maxx == -1) maxx = x; 102 if (y < miny || miny == -1) miny = y; 103 if (y > maxy || maxy == -1) maxy = y; 99 104 } 100 105 } 101 106 } 102 if ((double)n / (box.width * box.height) < 0.05) return 0; 103 go_linear_regression(xss, xdim, ys, n, 1, res, reg); 104 double rval = res[1]; 105 free(xss[0]); 106 free(xss); 107 //printf("found %d/%d white points\n", n, box.width * box.height); 108 if ((double)n / (box.width * box.height) < 0.05) return -1; 109 110 double cov01, cov10, cov11, sumsq; // ignored 111 int rval = gsl_fit_linear(xs, 1, ys, 1, n, yint, slope, 112 &cov01, &cov10, &cov11, &sumsq); 113 linebox->start.x = minx; 114 linebox->start.y = miny; 115 linebox->end.x = maxx; 116 linebox->end.y = maxy; 117 free(xs); 107 118 free(ys); 108 free(res);109 119 return rval; 110 120 } 111 121 112 122 /* in in HSV, out in BGR :) */ 113 px_t mark_type(const IplImage *in, IplImage *out, px_params_t px_params, get_type_cb_t get_type_cb) { 123 px_t mark_type(const IplImage *in, IplImage *out, px_params_t px_params, 124 get_type_cb_t get_type_cb) 125 { 114 126 int x, y; 115 127 //uint32_t type_avg_h = 0, type_avg_s = 0, type_avg_v = 0, type_count = 1; … … 121 133 122 134 //const px_t rpx = GET_PX(in, x, y); 123 //fprintf(stderr, "%d %d %d\n", rpx.hsv.h, rpx.hsv.s, rpx.hsv.v); 135 //fprintf(stderr, "%d %d %d\n", rpx.hsv.h, rpx.hsv.s, 136 // rpx.hsv.v); 124 137 //if (IS_PX(px, px_params.target)) { 125 138 // type_avg_h += rpx.hsv.h; … … 130 143 } 131 144 } 132 //printf("h = %lld, s = %lld, v = %lld, count = %lld\n", type_avg_h, type_avg_s, type_avg_v, type_count); 145 //printf("h = %lld, s = %lld, v = %lld, count = %lld\n", type_avg_h, 146 // type_avg_s, type_avg_v, type_count); 133 147 //type_avg_h /= type_count; 134 148 //type_avg_s /= type_count; … … 149 163 * Save values inbetween in static/global variables 150 164 */ 151 inline px_t get_grass(const IplImage *in, IplImage *out, int x, int y, px_params_t px_params) { 165 inline px_t get_grass(const IplImage *in, IplImage *out, int x, int y, px_params_t px_params) 166 { 152 167 const px_t px = GET_PX(in, x, y); 153 168 if (y < 200) … … 160 175 return PX_OBSTICLE; 161 176 } 162 void mark_grass(const IplImage *in, IplImage *out) { 177 void mark_grass(const IplImage *in, IplImage *out) 178 { 163 179 /* Default grass colors */ 164 180 px_params_t px_params = { … … 184 200 185 201 /* can only one at a time (white or yellow) :( */ 186 inline px_t get_lines(const IplImage *in, IplImage *out, int x, int y, px_params_t px_params) { 202 inline px_t get_lines(const IplImage *in, IplImage *out, int x, int y, px_params_t px_params) 203 { 187 204 const px_t px = GET_PX(in, x, y); 188 205 if (y < 30) … … 203 220 return PX_IGNORE; 204 221 } 205 void mark_lines(const IplImage *in, IplImage *out) { 222 void mark_lines(const IplImage *in, IplImage *out) 223 { 206 224 /* Default grass colors */ 207 225 px_params_t px_params = { … … 218 236 white_avg.hsv.h, white_avg.hsv.s, white_avg.hsv.v); 219 237 } 220 void expand_lines(const IplImage *out1, IplImage *out2) { 238 void expand_lines(const IplImage *out1, IplImage *out2) 239 { 221 240 CvPoint pt; 222 241 int rad = 10; … … 253 272 } 254 273 } 255 /* Used for dashed line detection */ 256 void mark_slopes(IplImage *out) { 257 double slope; 258 go_regression_stat_t reg; 259 CvScalar color = {{255, 255, 0, 0}}; 274 void mark_slopes(IplImage *out) 275 { 276 double slope, yint; // y = slope*x + yint 260 277 CvPoint start, end; 261 CvRect box = cvRect(1, 1, 500, 500); 262 for (box.x = 0; box.x + box.width < out->width; box.x += box.width/2) { 263 for (box.y = 0; box.y + box.height < out->height; box.y += box.height/2) { 264 //printf("checking box (%d,%d)â(%d-%d)\n", box.x, box.y, box.width, box.height); 265 slope = find_slope_in_box(out, box, ®); 266 if (slope == 0) continue; 267 //printf("found slope `%f', at (%d,%d)\n", slope, (int)reg.xbar[1], (int)reg.ybar); 268 start.x = reg.xbar[1] - ((box.width/2)); 269 start.y = reg.ybar - ((box.width/2)*slope); 270 end.x = reg.xbar[1] + ((box.width/2)); 271 end.y = reg.ybar + ((box.width/2)*slope); 278 CvScalar color = {{255, 255, 0, 0}}; 279 CvScalar color2 = {{255, 64, 0, 0}}; 280 CvRect box = cvRect(1, 1, 500, 500); 281 for (box.y = 0; box.y + box.height < out->height; box.y += box.height) { 282 for (box.x = 0; box.x + box.width < out->width; box.x += box.width) { 283 // The extreems of the actual line 284 CvLine linebox = {{0, 0}, {0, 0}}; 285 int rval = find_slope_in_box(out, box, &linebox, &yint, &slope); 286 if (rval == -1) continue; 287 //printf("checking box (%4d,%4d)â(%4d-%4d): y = %8.3f*x + %8.3f\n", 288 // box.x, box.y, box.width, box.height, slope, yint); 289 start.x = linebox.start.x; 290 start.y = yint + slope * linebox.start.x; 291 end.x = linebox.end.x; 292 end.y = yint + slope * (linebox.end.x); 293 //CvPoint outer_start, outer_end; 294 //outer_start.x = box.x + 5; 295 //outer_start.y = box.y + 5; 296 //outer_end.x = box.x + box.width - 5; 297 //outer_end.y = box.y + box.height - 5; 298 //linebox.start.x += 2; 299 //linebox.start.y += 2; 300 //linebox.end.x -= 2; 301 //linebox.end.y -= 2; 302 //cvRectangle(out, outer_start, outer_end, color2, 10, 0, 0); 303 //cvRectangle(out, linebox.start, linebox.end, color, 4, 0, 0); 272 304 cvLine(out, start, end, color, 10, 0, 0); 273 305 } … … 275 307 } 276 308 277 void print_image(IplImage *img) { 309 void print_image(IplImage *img) 310 { 278 311 int x, y; 279 312 for (y = 0; y < img->height; y++) { … … 287 320 } 288 321 289 void print_time(char *label) { 322 void print_time(char *label) 323 { 290 324 struct timeb tp; 291 325 ftime(&tp); … … 293 327 } 294 328 295 inline int IS_PX(px_t px1, px_t px2) { 329 inline int IS_PX(px_t px1, px_t px2) 330 { 296 331 //return !memcmp(&px1, &px2, sizeof(px_t)); 297 332 return px1.bgr.b == px2.bgr.b && … … 301 336 } 302 337 /* Ahh, is there supposed to be (width+1) here or not?? */ 303 inline px_t GET_PX(const IplImage *img, int x, int y) { 338 inline px_t GET_PX(const IplImage *img, int x, int y) 339 { 304 340 return *( (px_t*)img->imageData + (img->width*y + x) ); 305 341 } 306 inline void SET_PX(IplImage *img, int x, int y, px_t val) { 342 inline void SET_PX(IplImage *img, int x, int y, px_t val) 343 { 307 344 *(((px_t*)img->imageData) + ((img->width)*(y) + (x))) = val; 308 345 } -
trunk/software/rb/vision/vision.h
r217 r230 21 21 22 22 #include <stdint.h> 23 #include <goffice/math/go-regression.h>24 23 25 24 /* CV stuff that should exist */ … … 131 130 * order to fill in gaps that occure in the line. 132 131 */ 133 double find_slope_in_box (IplImage *out, CvRectbox,134 go_regression_stat_t *reg);132 int find_slope_in_box (IplImage *out, CvRect box, CvLine *linebox, 133 double *yint, double *slope); 135 134 136 135 /*

