Rose-Hulman Robotics Team

Changeset 716 for trunk

Show
Ignore:
Timestamp:
03/03/10 04:04:50 (6 months ago)
Author:
mosttw
Message:

Basic histogramming works

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

Legend:

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

    r714 r716  
    2525#include "_camera.h" 
    2626 
     27// The following macros return pointers to pixels in 1- and 3-channel images, respectively. 
     28#define IMAGE_PIXEL_C1(image, x, y) ((guint8*) (image->imageData + image->widthStep * y + x)) 
     29#define IMAGE_PIXEL_C3(image, x, y) ((guint8*) (image->imageData + image->widthStep * y + x * 3)) 
     30 
     31 
    2732static PyTypeObject *PyGObjectType = NULL; 
    2833static PyTypeObject *IplImageObjectType = NULL; 
     
    8590                int sobel_aperture = 5; 
    8691                int blur_aperture = 3; 
     92                int hist_bins = 100; 
     93                // Histogram bins below these thresholds are zeroed. 
     94                int hist_s_low =  50; 
     95                int hist_v_low = 100; 
     96 
     97                inline int hist_bin(guint8 pixel_value) { 
     98                        return pixel_value * hist_bins / 255.0;  
     99                } 
    87100 
    88101                // Calculate the "working" image size 
     
    99112                CvMat *sobel; 
    100113                IplImage *texture; 
     114                int *hist_h; 
     115                int *hist_s; 
     116                int *hist_v; 
    101117 
    102118                hsv_full   = cvCreateImage(cvSize(input->width, input->height), IPL_DEPTH_8U, 3); 
     
    107123                texture    = cvCreateImage(cvSize(working_width, working_height), IPL_DEPTH_8U, 1); 
    108124                output     = cvCreateImage(cvSize(working_width, working_height), IPL_DEPTH_8U, 3); 
     125                hist_h     = malloc(sizeof(int) * hist_bins); // TODO: Check this return value 
     126                hist_s     = malloc(sizeof(int) * hist_bins); // TODO: Check this return value 
     127                hist_v     = malloc(sizeof(int) * hist_bins); // TODO: Check this return value 
    109128 
    110129                // Colorspace conversion and scaling 
     
    112131                cvSplit(hsv_full, NULL, NULL, v_full, NULL); 
    113132                cvResize(hsv_full, hsv, CV_INTER_AREA); 
     133 
     134                // Create the output image 
     135                cvResize(input, output, CV_INTER_AREA); 
    114136 
    115137                // Calculate "texture" channel 
     
    119141                cvSmooth(texture, texture, CV_BLUR, blur_aperture, 0, 0, 0); 
    120142 
     143                // Calculate the location of the "safe region", which will be histogrammed in 
     144                // order to determine what the grass looks like. 
     145                int tl_x = working_width * 0.2; 
     146                int tl_y = working_height * 0.8; 
     147                int br_x = working_width - tl_x - 1; 
     148                int br_y = working_height - 1; 
     149 
     150                // Generate the ground histogram 
     151                for (int x = tl_x; x <= br_x; x++) { 
     152                        for (int y = tl_y; y <= br_y; y++) { 
     153                                guint8 *pixel = IMAGE_PIXEL_C3(hsv, x, y); 
     154                                hist_h[hist_bin(pixel[0])]++; 
     155                                hist_s[hist_bin(pixel[1])]++; 
     156                                hist_v[hist_bin(pixel[2])]++; 
     157                        } 
     158                } 
     159 
     160                // Low-pass filter the histograms 
     161                for (int i = 0; i < hist_bins; i++) { 
     162                        if (hist_s[i] < hist_s_low) hist_s[i] = 0; 
     163                        if (hist_v[i] < hist_v_low) hist_v[i] = 0; 
     164                } 
     165 
     166                for (int x = 0; x < working_width; x++) { 
     167                        for (int y = 0; y < working_height; y++) { 
     168                                guint8 *hsv_pixel = IMAGE_PIXEL_C3(hsv, x, y); 
     169                                guint8 *out_pixel = IMAGE_PIXEL_C3(output, x, y); 
     170                                if (hist_h[hist_bin(hsv_pixel[0])] && 
     171                                                hist_s[hist_bin(hsv_pixel[1])] && 
     172                                                hist_v[hist_bin(hsv_pixel[2])]) { 
     173                                        out_pixel[0] = 0; 
     174                                        out_pixel[1] = 255; 
     175                                        out_pixel[2] = 0; 
     176                                } 
     177                        } 
     178                } 
     179 
     180 
    121181                // Create output image 
    122                 cvCvtColor(texture, output, CV_GRAY2BGR); 
     182                //cvCvtColor(texture, output, CV_GRAY2BGR); 
    123183         
    124184                cvReleaseImage(&hsv_full); 
     
    128188                cvReleaseMat(&sobel); 
    129189                cvReleaseImage(&texture); 
     190                free(hist_h); 
     191                free(hist_s); 
     192                free(hist_v); 
    130193        Py_END_ALLOW_THREADS 
    131194 
  • trunk/software/rb/vision/makefile

    r713 r716  
    1 CFLAGS   := -p -pg -g -Wall -Werror -Wno-unused -pedantic -fPIC --std=gnu99 -fgnu89-inline -O3 $(shell pkg-config --cflags opencv gsl glib-2.0 gobject-2.0) 
     1CFLAGS   := -p -pg -g -Wall -Werror -Wno-unused -fPIC --std=gnu99 -fgnu89-inline -O3 $(shell pkg-config --cflags opencv gsl glib-2.0 gobject-2.0) 
    22#CFLAGS   := -p -pg -g -Wall -Werror -pedantic -fPIC -O3 $(shell pkg-config --cflags opencv gsl) 
    33CPPFLAGS := -p -pg -g -Wall -Werror -Wno-unused -pedantic -fPIC -O3 $(shell pkg-config --cflags opencv gsl)