Changeset 469
- Timestamp:
- 03/22/09 18:15:40 (3 years ago)
- Location:
- trunk/software
- Files:
-
- 9 modified
-
Makefile (modified) (1 diff)
-
rb/vision (modified) (1 prop)
-
rb/vision/_vision.c (modified) (1 diff)
-
rb/vision/main.c (modified) (4 diffs)
-
rb/vision/makefile (modified) (1 diff)
-
rb/vision/vision.c (modified) (8 diffs)
-
rb/vision/vision.h (modified) (4 diffs)
-
setup.py (modified) (2 diffs)
-
test.py (modified) (1 prop)
Legend:
- Unmodified
- Added
- Removed
-
trunk/software/Makefile
r403 r469 7 7 gtk-builder-convert $< $@ 8 8 9 test: main9 test: all 10 10 python test.py 11 11 12 docs: main12 docs: all 13 13 epydoc --html --docformat=restructuredtext \ 14 14 --show-imports --no-frames --graph=all \ -
trunk/software/rb/vision
- Property svn:ignore
-
old new 1 1 *.jpeg 2 *.png 2 3 main 3 4 gmon.out
-
- Property svn:ignore
-
trunk/software/rb/vision/_vision.c
r468 r469 20 20 21 21 #include <Python.h> 22 #include <opencv/cv.h> 23 24 #include "vision.h" 25 26 /* TODO: define some constants for the types of points */ 22 27 23 28 static PyObject* 24 29 _get_points(PyObject *self, PyObject *args) 25 30 { 26 PyObject *points = PyList_New(0); // TODO: start with a larger list 31 // TODO: start with a larger list 32 PyObject *points = PyList_New(0); 27 33 28 for (int i = 0; i < 10; i++) { 29 /* Each point is (type_of_points, x, y) */ 30 PyObject *point = Py_BuildValue("iii", 0, i, i*2); 34 IplImage *input = get_image(); 35 IplImage *transformed = transform_image(input, 0.25); 36 CvMat *classes = classify_image(transformed); 37 CvSeq *seq = get_points(classes); 38 39 CvSeqReader reader; 40 cvStartReadSeq(seq, &reader, 0); 41 for(int i = 0; i < seq->total; i++) { 42 int data[3]; 43 CV_READ_SEQ_ELEM(data, reader); 44 PyObject *point = Py_BuildValue("iii", data[0], data[1], data[2]); 31 45 PyList_Append(points, point); 32 46 } 47 cvReleaseMemStorage(&seq->storage); 33 48 34 49 return points; -
trunk/software/rb/vision/main.c
r468 r469 18 18 19 19 #include <stdio.h> 20 #include <stdint.h>21 20 #include <errno.h> 22 #include <opencv/cv.h>23 21 #include <opencv/highgui.h> 24 22 #include <X11/keysym.h> … … 43 41 /* Process */ 44 42 print_time("Processing images"); 45 IplImage *output = process_image(input, 0.25); 43 IplImage *transformed = transform_image(input, 0.25); 44 CvMat *classes = classify_image(transformed); 45 IplImage *colors = color_mat(classes); 46 46 47 /* Save image */ 48 print_time("Saving images"); 49 cvSaveImage("out.png", output); 47 /* Extracting points */ 48 print_time("Extracting and marking points"); 49 CvSeq *seq = get_points(classes); 50 CvSeqReader reader; 51 cvStartReadSeq(seq, &reader, 0); 52 for(int i = 0; i < seq->total; i++) { 53 int data[3]; 54 CV_READ_SEQ_ELEM(data, reader); 55 printf("read: (%d, %d, %d)\n", data[0], data[1], data[2]); 56 cvCircle(transformed, 57 cvPoint(data[1], data[2]), 58 2, class_colors[data[0]], CV_FILLED, 8, 0); 59 } 60 cvReleaseMemStorage(&seq->storage); 50 61 51 62 /* Display images */ … … 54 65 cvMoveWindow ("input", 100, 100); 55 66 cvShowImage ("input", input); 56 cvNamedWindow("out", 0); 57 cvMoveWindow ("out", 100, 100); 58 cvShowImage ("out", output); 67 cvNamedWindow("transformed", 0); 68 cvMoveWindow ("transformed", 100, 100); 69 cvShowImage ("transformed", transformed); 70 cvNamedWindow("colors", 0); 71 cvMoveWindow ("colors", 100, 100); 72 cvShowImage ("colors", colors); 59 73 60 74 while (cvWaitKey(0) != XK_q); … … 62 76 /* Release temp data */ 63 77 cvReleaseImage(&input); 64 cvReleaseImage(&output); 78 cvReleaseImage(&transformed); 79 cvReleaseMat(&classes); 80 cvReleaseImage(&colors); 65 81 66 82 return 0; -
trunk/software/rb/vision/makefile
r468 r469 25 25 gcc -o $@ $+ $(LDFLAGS) 26 26 27 %.o: %.c %.h27 %.o: %.c 28 28 gcc $(CFLAGS) -c -o $@ $*.c 29 29 -
trunk/software/rb/vision/vision.c
r468 r469 22 22 #include <sys/timeb.h> 23 23 #include <opencv/cv.h> 24 #include <opencv/highgui.h> 24 25 #include <X11/keysym.h> 25 26 #include <gsl/gsl_fit.h> … … 27 28 #include "vision.h" 28 29 29 IplImage *process_image(IplImage *input, double scale) 30 IplImage *get_image() 31 { 32 char *file = "rb/vision/data/images/cimg4753.jpg"; 33 IplImage *image = cvLoadImage(file, 1); 34 if (!image) { 35 printf("Could not load image file: %s\n", file); 36 return NULL; 37 } 38 return image; 39 } 40 41 IplImage *transform_image(IplImage *input, double scale) 30 42 { 31 43 /* Scale image */ … … 53 65 cvWarpPerspective(img, transformed, mat, CV_WARP_FILL_OUTLIERS, cvScalarAll(0)); 54 66 cvReleaseImage(&img); 55 img = transformed; 67 return transformed; 68 } 69 70 CvMat *classify_image(IplImage *input) 71 { 72 CvSize size = {input->width, input->height}; 73 IplImage *img = cvCreateImage(size, input->depth, input->nChannels); 74 cvCopy(input, img, NULL); 56 75 57 76 /* Smooth image */ … … 66 85 /* Threshold image */ 67 86 print_time("Marking grass"); 68 CvMat *grass_mask = cvCreateMat(size.height, size.width, CV_8U C1);69 CvMat *line_mask = cvCreateMat(size.height, size.width, CV_8U C1);87 CvMat *grass_mask = cvCreateMat(size.height, size.width, CV_8U); 88 CvMat *line_mask = cvCreateMat(size.height, size.width, CV_8U); 70 89 cvInRangeS(hsv, cvScalar(30, 0, 80, 0), cvScalar(120,90,180,0), grass_mask); 71 90 cvInRangeS(hsv, cvScalar(100,60,140,0), cvScalar(120,90,254,0), line_mask); 91 // TODO, mark obstacles separately 72 92 73 93 /* Morphological operations */ 74 94 print_time("Expanding lines"); 75 95 int rad = (int)size.width*0.005; 76 printf("se.rad = %d\n", rad);77 96 IplConvKernel *se = cvCreateStructuringElementEx(rad, rad, rad/2, rad/2, CV_SHAPE_RECT, NULL); 78 97 cvDilate(line_mask, line_mask, se, 2); … … 80 99 81 100 /* Mark outputs */ 82 IplImage *output = cvCreateImage(size, img->depth, img->nChannels); 83 cvSet(output, SC_GRASS, grass_mask); 84 cvSet(output, SC_LINE, line_mask); 101 CvMat *output = cvCreateMat(input->height, input->width, CV_8U); 102 cvSet(output, cvRealScalar(RB_IGNORE), NULL); 103 cvSet(output, cvRealScalar(RB_GRASS), grass_mask); 104 cvSet(output, cvRealScalar(RB_LINE ), line_mask); 85 105 86 106 /* Release temporary data */ … … 91 111 92 112 return output; 113 } 114 115 CvSeq *get_points(CvMat *classes) 116 { 117 int test_data[][3] = { 118 {RB_SKY,100,100}, {RB_GRASS,200,100}, {RB_OBSTICLE,300,100}, 119 {RB_SKY,100,200}, {RB_GRASS,200,200}, {RB_OBSTICLE,300,200}, 120 {RB_SKY,100,300}, {RB_GRASS,200,300}, {RB_OBSTICLE,300,300}, 121 }; 122 123 CvMemStorage *storage = cvCreateMemStorage(0); 124 CvSeq *seq = cvCreateSeq(CV_32SC3, // sequence of 3 tuples? (scalars?) 125 sizeof(CvSeq), // header size - no extra fields 126 sizeof(int)*3, // element size 127 storage); // the container storage 128 129 CvSeqWriter writer; 130 cvStartAppendToSeq(seq, &writer); 131 for(int x = 0; x < classes->cols; x+=10) { 132 for(int y = 0; y < classes->rows; y+=10) { 133 CvScalar class = cvGet2D(classes, y, x); 134 int data[3] = {class.val[0], x, y}; 135 CV_WRITE_SEQ_ELEM(data, writer); 136 } 137 } 138 cvEndWriteSeq(&writer); 139 140 return seq; 141 } 142 143 IplImage *color_mat(CvMat *classes) 144 { 145 IplImage *img = cvCreateImage(cvSize(classes->cols, classes->rows), 8, 3); 146 CvMat *mask = cvCreateMat(classes->rows, classes->cols, CV_8UC1); 147 for (int i = 0; i < RB_NUM_CLASSES; i++) { 148 cvCmpS(classes, i, mask, CV_CMP_EQ); 149 cvSet(img, class_colors[i], mask); 150 } 151 return img; 93 152 } 94 153 … … 105 164 CvScalar px = cvGet2D(out, x, y); 106 165 // TODO: test/check the memcmp line 107 if (!memcmp(&px, & SC_LINE, sizeof(CvScalar))) {166 if (!memcmp(&px, &class_colors[RB_LINE], sizeof(CvScalar))) { 108 167 xs[n] = x; 109 168 ys[n] = y; … … 160 219 box.width = out->width - box.x; 161 220 162 // The extre ems of the actual line221 // The extremes of the actual line 163 222 CvLine linebox = {{0, 0}, {0, 0}}; 164 223 int rval = find_slope_in_box(out, box, &linebox, &yint, &slope); -
trunk/software/rb/vision/vision.h
r468 r469 28 28 } CvLine; 29 29 30 /* 30 /** 31 31 * Standard pixel values 32 32 * 33 33 * The classified image will be represented by these colors 34 34 */ 35 const static CvScalar SC_SKY = {{0*255, 0*255, 1*255, 0}}; // Blue 36 const static CvScalar SC_GRASS = {{0*255, 1*255, 0*255, 0}}; // Green 37 const static CvScalar SC_LINE = {{1*255, 1*255, 1*255, 0}}; // White 38 const static CvScalar SC_OBSTICLE = {{1*255, 0*255, 0*255, 0}}; // Red 39 const static CvScalar SC_PATH = {{1*255, 0*255, 1*255, 0}}; // Purple 40 const static CvScalar SC_IGNORE = {{0*255, 0*255, 0*255, 0}}; // Black 35 typedef enum rb_classes_t { 36 RB_SKY , 37 RB_GRASS , 38 RB_LINE , 39 RB_OBSTICLE, 40 RB_PATH , 41 RB_IGNORE , 42 RB_NUM_CLASSES 43 } rb_classes_t; 44 const static CvScalar class_colors[256] = { 45 [RB_SKY ] = {{0*255, 0*255, 1*255, 0}}, // Blue 46 [RB_GRASS ] = {{0*255, 1*255, 0*255, 0}}, // Green 47 [RB_LINE ] = {{1*255, 1*255, 1*255, 0}}, // White 48 [RB_OBSTICLE] = {{1*255, 0*255, 0*255, 0}}, // Red 49 [RB_PATH ] = {{1*255, 0*255, 1*255, 0}}, // Purple 50 [RB_IGNORE ] = {{0*255, 0*255, 0*255, 0}} // Black 51 }; 41 52 42 53 /** 43 * Preforms the following operations on the image, returning a new image as 44 * output. The original image is left untouched. 45 * - scaling 46 * - perspective transform 54 * Attempt to obtain an image from the camera 55 */ 56 IplImage *get_image(); 57 58 /** 59 * Scales and transforms the image. 60 * 61 * The goal is to make distacnes in the image (pixel distances) be a constant 62 * scale of distances in the actual world. 63 */ 64 IplImage *transform_image(IplImage *input, double scale); 65 66 /** 67 * Classifies the input image. 68 * 69 * This preforms the following operations on the image. as output. The 70 * original image is left untouched. 47 71 * - smoothing 48 72 * - converting to HSV … … 50 74 * - morphologies 51 75 */ 52 IplImage *process_image(IplImage *input, double scale);76 CvMat *classify_image(IplImage *input); 53 77 54 /* 78 /** 79 * Retrieves a set of important points from a processed image. This converts an 80 * entire image into something small enough to be used by Python 81 * 82 * @param classes Output of the clasify_image function 83 * @return A sequence of points on the image of type (type,x,y) where 84 * type is a integer that identifies the classification at that 85 * point in the image. 86 */ 87 CvSeq *get_points(CvMat *classes); 88 89 /** 90 * Color a matrix of classifications, useful for debugging. 91 * 92 * @param classes Output of the clasify_image function 93 * @return An image colored according to the class_colors map 94 */ 95 IplImage *color_mat(CvMat *classes); 96 97 /************************* 98 * Dashed line detection * 99 *************************/ 100 /** 55 101 * Attempt to find fit a slope to a line in a given box. 56 102 * … … 62 108 double *yint, double *slope); 63 109 64 /* 110 /** 65 111 * This partitions the image into a grid of boxes and attempts to draw the 66 112 * slope of the white line in each box. This is mostly used for debugging … … 69 115 void mark_slopes (IplImage *out); 70 116 71 /* 117 /** 72 118 * Prints out the given label as well as a timestamps for the current time. 73 119 * This is used for profiling the software -
trunk/software/setup.py
r398 r469 22 22 23 23 example_module = Extension('rb.example.c_module', ['rb/example/c_module.c']) 24 laser_module = Extension('rb.camera._laser',['rb/camera/_laser.c'],24 laser_module = Extension('rb.camera._laser', ['rb/camera/_laser.c'], 25 25 libraries=['cv', 'highgui'], 26 26 extra_compile_args=['-DOPENCV']) 27 vision_module = Extension( 28 'rb.vision._vision', 29 ['rb/vision/_vision.c', 'rb/vision/vision.c', 'rb/vision/main.c'], 30 libraries=['cv', 'highgui', 'gsl'], 31 extra_compile_args=['--std=c99'] 32 ); 27 33 28 34 … … 30 36 version='0.1', 31 37 #py_modules=['rb'], 32 ext_modules=[example_module, laser_module ],38 ext_modules=[example_module, laser_module, vision_module], 33 39 ) -
trunk/software/test.py
- Property svn:executable deleted

