Rose-Hulman Robotics Team

Changeset 230

Show
Ignore:
Timestamp:
10/15/08 23:11:59 (3 years ago)
Author:
spenceal
Message:

removing goffice dependency, converted to use gsl

Location:
trunk/software/rb/vision
Files:
4 modified

Legend:

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

    r229 r230  
    8686        /* Mark slopes */ 
    8787        print_time("Marking slopes"); 
    88         mark_slopes(out); // This currently segfaults 
     88        mark_slopes(out); 
    8989 
    9090        /* 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) 
     1CFLAGS=-p -pg -g -Wall -pedantic --std=gnu99 -fgnu89-inline -O3 $(shell pkg-config --cflags opencv gsl) 
     2LDFLAGS=-lpython2.5 $(shell pkg-config --libs opencv gsl) 
    33DLFLAGS=-shared -nostartfiles -fPIC 
    44 
     
    2222 
    2323main: main.o vision.o pathplan.o 
    24         gcc $(CFLAGS) -o $@ $+ $(LDFLAGS) 
     24        gcc -o $@ $+ $(LDFLAGS) 
    2525 
    2626%.o: %.c %.h 
    27         gcc -o $@ $*.c -c $(CFLAGS) 
     27        gcc $(CFLAGS) -c -o $@ $*.c 
    2828 
    2929_%: _%.c 
  • trunk/software/rb/vision/vision.c

    r229 r230  
    2323#include <opencv/cv.h> 
    2424#include <X11/keysym.h> 
     25#include <gsl/gsl_fit.h> 
    2526 
    2627#include "vision.h" 
    2728 
    2829/* Determine if the path between two points is all grass */ 
    29 int is_clear(IplImage *out, CvPoint start, CvPoint end) { 
     30int is_clear(IplImage *out, CvPoint start, CvPoint end) 
     31{ 
    3032        int x, y; 
    3133        int i; 
     
    5254/* Find the first non-grass point starting at `start' 
    5355 * Each time, increment y by `1' and x by `x_inc', */ 
    54 CvPoint find_obsticle(IplImage *out, CvPoint2D32f start, float x_inc) { 
     56CvPoint find_obsticle(IplImage *out, CvPoint2D32f start, float x_inc) 
     57{ 
    5558        float x = start.x, y = start.y; 
    5659        //printf("start = (%f,%f), incr = %f\n", x, y, x_inc); 
     
    6871 
    6972/* Rightmost pixel from leftmost blob :) */ 
    70 CvPoint find_px_at_y(IplImage *out, int y_line, px_t target) { 
     73CvPoint find_px_at_y(IplImage *out, int y_line, px_t target) 
     74{ 
    7175        int x; 
    7276        for (x = 0; x < out->width; x++) { 
     
    8084} 
    8185 
    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; 
     86int 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; 
    9293        n = 0; // find out how many we _actually_ have.. 
    9394        for (x = box.x; x < box.x+box.width; x++) { 
    9495                for (y = box.y; y < box.y+box.height; y++) { 
    9596                        if (IS_PX(GET_PX(out, x, y), PX_LINE)) { 
    96                                 xss[0][n] = x; 
     97                                xs[n] = x; 
    9798                                ys[n] = y; 
    9899                                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; 
    99104                        } 
    100105                } 
    101106        } 
    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); 
    107118        free(ys); 
    108         free(res); 
    109119        return rval; 
    110120} 
    111121 
    112122/* 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) { 
     123px_t mark_type(const IplImage *in, IplImage *out, px_params_t px_params, 
     124               get_type_cb_t get_type_cb) 
     125{ 
    114126        int x, y; 
    115127        //uint32_t type_avg_h = 0, type_avg_s = 0, type_avg_v = 0, type_count = 1; 
     
    121133                         
    122134                        //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); 
    124137                        //if (IS_PX(px, px_params.target)) { 
    125138                        //      type_avg_h += rpx.hsv.h; 
     
    130143                } 
    131144        } 
    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); 
    133147        //type_avg_h /= type_count; 
    134148        //type_avg_s /= type_count; 
     
    149163 *  Save values inbetween in static/global variables 
    150164 */ 
    151 inline px_t get_grass(const IplImage *in, IplImage *out, int x, int y, px_params_t px_params) { 
     165inline px_t get_grass(const IplImage *in, IplImage *out, int x, int y, px_params_t px_params) 
     166{ 
    152167        const px_t px = GET_PX(in, x, y); 
    153168        if (y < 200) 
     
    160175                return PX_OBSTICLE; 
    161176} 
    162 void mark_grass(const IplImage *in, IplImage *out) { 
     177void mark_grass(const IplImage *in, IplImage *out) 
     178{ 
    163179        /* Default grass colors */ 
    164180        px_params_t px_params = { 
     
    184200 
    185201/* 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) { 
     202inline px_t get_lines(const IplImage *in, IplImage *out, int x, int y, px_params_t px_params) 
     203{ 
    187204        const px_t px = GET_PX(in, x, y); 
    188205        if (y < 30) 
     
    203220        return PX_IGNORE; 
    204221} 
    205 void mark_lines(const IplImage *in, IplImage *out) { 
     222void mark_lines(const IplImage *in, IplImage *out) 
     223{ 
    206224        /* Default grass colors */ 
    207225        px_params_t px_params = { 
     
    218236                white_avg.hsv.h, white_avg.hsv.s, white_avg.hsv.v); 
    219237} 
    220 void expand_lines(const IplImage *out1, IplImage *out2) { 
     238void expand_lines(const IplImage *out1, IplImage *out2) 
     239{ 
    221240        CvPoint pt; 
    222241        int rad = 10; 
     
    253272        } 
    254273} 
    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}}; 
     274void mark_slopes(IplImage *out) 
     275{ 
     276        double slope, yint; // y = slope*x + yint 
    260277        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, &reg); 
    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); 
    272304                        cvLine(out, start, end, color, 10, 0, 0); 
    273305                } 
     
    275307} 
    276308 
    277 void print_image(IplImage *img) { 
     309void print_image(IplImage *img) 
     310{ 
    278311        int x, y; 
    279312        for (y = 0; y < img->height; y++) { 
     
    287320} 
    288321 
    289 void print_time(char *label) { 
     322void print_time(char *label) 
     323{ 
    290324        struct timeb tp; 
    291325        ftime(&tp); 
     
    293327} 
    294328 
    295 inline int IS_PX(px_t px1, px_t px2) { 
     329inline int IS_PX(px_t px1, px_t px2) 
     330{ 
    296331        //return !memcmp(&px1, &px2, sizeof(px_t)); 
    297332        return px1.bgr.b == px2.bgr.b && 
     
    301336} 
    302337/* Ahh, is there supposed to be (width+1) here or not?? */ 
    303 inline px_t GET_PX(const IplImage *img, int x, int y) { 
     338inline px_t GET_PX(const IplImage *img, int x, int y) 
     339{ 
    304340        return *( (px_t*)img->imageData + (img->width*y + x) ); 
    305341} 
    306 inline void SET_PX(IplImage *img, int x, int y, px_t val) { 
     342inline void SET_PX(IplImage *img, int x, int y, px_t val) 
     343{ 
    307344        *(((px_t*)img->imageData) + ((img->width)*(y) + (x))) = val; 
    308345} 
  • trunk/software/rb/vision/vision.h

    r217 r230  
    2121 
    2222#include <stdint.h> 
    23 #include <goffice/math/go-regression.h> 
    2423 
    2524/* CV stuff that should exist */ 
     
    131130 * order to fill in gaps that occure in the line. 
    132131 */ 
    133 double   find_slope_in_box (IplImage *out, CvRect box, 
    134                             go_regression_stat_t *reg); 
     132int      find_slope_in_box (IplImage *out, CvRect box, CvLine *linebox, 
     133                            double *yint, double *slope); 
    135134 
    136135/*