Rose-Hulman Robotics Team

Changeset 683

Show
Ignore:
Timestamp:
02/20/10 14:47:47 (2 years ago)
Author:
mosttw
Message:

Checkpoint

Location:
branches/2010-image-rec
Files:
3 added
4 modified

Legend:

Unmodified
Added
Removed
  • branches/2010-image-rec/docs/report.tex

    r681 r683  
    1414 
    1515\begin{document} 
    16 \title{TODO: Pick a title} 
     16\title{Vision-based obstacle recognition for an autonomous mobile robot in a grassy, outdoor environment} 
    1717\author{Thomas W. Most \and Trenton S. Tabor} 
    1818\date{\today} 
  • branches/2010-image-rec/hist_test.m

    r682 r683  
    1 function hist_test(fn) 
     1function hist_test(fn, show_images) 
    22 
    33% Test how well histogramming works with our test images 
     
    3737 
    3838 
    39 fsobel = fspecial('sobel'); 
    40 hedges = filter2(fsobel , imbig(:,:,texture_band)); 
    41 vedges = filter2(fsobel', imbig(:,:,texture_band)); 
    42 edges = sqrt(hedges .^ 2 + vedges .^ 2); 
    43 edges=double(imread([fn '-sobel-v-5.png']))/255; 
     39%fsobel = fspecial('sobel'); 
     40%hedges = filter2(fsobel , imbig(:,:,texture_band)); 
     41%vedges = filter2(fsobel', imbig(:,:,texture_band)); 
     42%edges = sqrt(hedges .^ 2 + vedges .^ 2); 
     43edges = double(imread([fn '-sobel-v-5.png']))/255; 
    4444texture = filter2(fspecial('average', 10), edges); 
    4545texture = filter2(fspecial('average', 10), texture); 
     
    5656 
    5757figure(1); 
    58 imshow([edges; texture]); 
    59  
    60 texture=imresize(texture,.25); 
     58imwrite(texture, [fn '-texture-full.png']); 
     59 
     60texture = imresize(texture, .25); 
    6161 
    6262im(:,:,1:3) = rgb2hsv(im_rgb); 
     
    6565 
    6666% Area at the bottom of the image assumed to be grass, clear of obstacles 
    67 clear_area = im(round(clear_area_top * rows):end-20, ... 
     67clear_area = im(round(clear_area_top * rows):end, ... 
    6868                round(clear_area_left * cols): ... 
    6969                    round((1 - clear_area_left) * cols), :); 
     
    9191grass_v_mask = grass_v_hist; 
    9292grass_v_mask(grass_v_hist < mean(grass_v_hist) * v_mean_factor) = 0; 
    93 grass_t_mask = grass_t_hist; 
    94 grass_t_mask(grass_t_hist < mean(grass_t_hist) * t_mean_factor) = 0; 
     93%grass_t_mask = grass_t_hist; 
     94%grass_t_mask(grass_t_hist < mean(grass_t_hist) * t_mean_factor) = 0; 
    9595 
    9696% Ungodly slow loop through the pixels 
     
    9999    for c = 1:cols 
    100100        if grass_h_mask(h(r,c)) && grass_s_mask(s(r,c)) && ... 
    101                 grass_v_mask(v(r,c)) && grass_t_mask(t(r,c)) 
     101                grass_v_mask(v(r,c)) ... && grass_t_mask(t(r,c)) 
    102102            mask(r,c) = 1; 
    103103        end 
     
    105105end 
    106106 
     107imwrite(mask, [fn '-mask-raw.png']); 
     108 
    107109% Clean things up a bit 
    108110se = strel('square', 6); 
    109111mask = imclose(mask, se); 
     112 
     113imwrite(mask, [fn, '-mask-closed.png']); 
    110114 
    111115tempMask=mask*-1+1; 
     
    114118hold on; 
    115119scatter(bottomlist(:,1),-bottomlist(:,2)); 
    116  
     120print('-depsc', [fn '-obstacle-points.png']); 
    117121 
    118122line_mask = texture; 
    119 for threshold = .42 
    120     line_mask(texture > threshold & mask == 0) = 1; 
    121     line_mask(texture <= threshold | mask ~= 0) = 0; 
    122     figure; 
    123     imshow(line_mask); 
    124 end 
    125 %keyboard; 
     123threshold = .42; 
     124line_mask(texture > threshold & mask == 0) = 1; 
     125line_mask(texture <= threshold | mask ~= 0) = 0; 
     126imwrite(line_mask, [fn '-line-mask.png']); 
     127 
    126128% Display 
    127129figure(2); 
     
    130132% Draw a box over the "clear" area 
    131133top_y = round(clear_area_top * rows); 
    132 bottom_y = rows-20; 
     134bottom_y = rows; 
    133135left_x = round(clear_area_left * cols); 
    134136right_x = round((1 - clear_area_left) * cols); 
     
    136138     [top_y top_y bottom_y bottom_y top_y],'LineWidth',4,'Color','red'); 
    137139  
    138  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%5 
    139   
    140  figure(3); 
    141   
    142  [H,T,R] = hough(line_mask); 
    143        imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit'); 
    144        xlabel('\theta'), ylabel('\rho'); 
    145        axis on, axis normal, hold on; 
    146        P  = houghpeaks(H,5,'NHoodSize',[161,161])%ceil(0.3*max(H(:)))); 
    147        x = T(P(:,2));  
    148        y = R(P(:,1)); 
    149        plot(x,y,'s','color','white'); 
    150   
    151   lines = houghlines(line_mask,T,R,P,'FillGap',5,'MinLength',7); 
    152        figure, imshow(im_rgb), hold on 
    153        max_len = 0; 
    154        for k = 1:length(lines) 
    155          xy = [lines(k).point1; lines(k).point2]; 
    156          plot(xy(:,1),xy(:,2),'LineWidth',8,'Color','blue'); 
    157   
    158          % plot beginnings and ends of lines 
    159          plot(xy(1,1),xy(1,2),'x','LineWidth',8,'Color','yellow'); 
    160          plot(xy(2,1),xy(2,2),'x','LineWidth',8,'Color','red'); 
    161   
    162          % determine the endpoints of the longest line segment  
    163          len = norm(lines(k).point1 - lines(k).point2); 
    164          if ( len > max_len) 
    165            max_len = len; 
    166            xy_long = xy; 
    167          end 
    168        end 
    169   
    170        % highlight the longest line segment 
    171        plot(xy_long(:,1),xy_long(:,2),'LineWidth',8,'Color','cyan'); 
    172   
    173   
    174  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
     140%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5 
     141 
     142figure(3); 
     143 
     144[H,T,R] = hough(line_mask); 
     145imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit'); 
     146xlabel('\theta'), ylabel('\rho'); 
     147axis on, axis normal, hold on; 
     148P  = houghpeaks(H,5,'NHoodSize',[161,161]); %ceil(0.3*max(H(:)))); 
     149x = T(P(:,2));  
     150y = R(P(:,1)); 
     151plot(x,y,'s','color','white'); 
     152print('-depsc', [fn '-hough-peaks.eps']); 
     153 
     154lines = houghlines(line_mask,T,R,P,'FillGap',5,'MinLength',7); 
     155figure, imshow(im_rgb), hold on 
     156max_len = 0; 
     157for k = 1:length(lines) 
     158    xy = [lines(k).point1; lines(k).point2]; 
     159    plot(xy(:,1),xy(:,2),'LineWidth',8,'Color','blue'); 
     160 
     161    % plot beginnings and ends of lines 
     162    plot(xy(1,1),xy(1,2),'x','LineWidth',8,'Color','yellow'); 
     163    plot(xy(2,1),xy(2,2),'x','LineWidth',8,'Color','red'); 
     164 
     165    % determine the endpoints of the longest line segment  
     166    len = norm(lines(k).point1 - lines(k).point2); 
     167    if ( len > max_len) 
     168        max_len = len; 
     169        xy_long = xy; 
     170    end 
     171end 
     172 
     173% highlight the longest line segment 
     174plot(xy_long(:,1),xy_long(:,2),'LineWidth',8,'Color','cyan'); 
     175print('-depsc', [fn '-hough-lines.eps']); 
     176 
     177 
     178%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
    175179 
    176180fprintf('Done\n'); 
     
    179183 
    180184function rgb = bw2rgb(bw) 
    181  
    182 rgb = zeros(size(bw, 1), size(bw, 2), 3); 
    183 rgb(:,:,1) = bw; 
    184 rgb(:,:,2) = bw; 
    185 rgb(:,:,3) = bw; 
    186  
    187 end 
     185        rgb = zeros(size(bw, 1), size(bw, 2), 3); 
     186        rgb(:,:,1) = bw; 
     187        rgb(:,:,2) = bw; 
     188        rgb(:,:,3) = bw; 
     189end 
     190 
    188191function bottomlist = bottomFind(bw) 
    189 list=zeros(size(bw,2),2); 
    190 temp=bw; 
    191 for i = 1:size(bw,2) 
    192 temp(1,:)=1; 
    193 list(i,2)=find(temp(:,i)==1,1,'last'); 
    194 list(i,1)=i; 
    195 end 
    196 bottomlist=list; 
    197 end 
     192        list = zeros(size(bw,2),2); 
     193        temp = bw; 
     194        for i = 1:size(bw,2) 
     195                temp(1,:) = 1; 
     196                list(i,2) = find(temp(:,i)==1,1,'last'); 
     197                list(i,1) = i; 
     198        end 
     199        bottomlist = list; 
     200end 
  • branches/2010-image-rec/sobel.py

    r669 r683  
    55import cv 
    66 
    7 fn = sys.argv[1] 
     7try: 
     8        fn = sys.argv[1] 
     9        operations = [(5, c) for c in sys.argv[2:]] or [(5, 'v')] 
     10except IndexError: 
     11        sys.stderr.write('usage: {0} image-fn [channel...]') 
     12        sys.exit(2) 
    813 
     14# Load the image and split its channels out 
    915im = cv.LoadImage(fn) 
    1016hsv = cv.CreateImage((im.width, im.height), cv.IPL_DEPTH_8U, 3) 
    1117cv.CvtColor(im, hsv, cv.CV_BGR2HSV) 
    12 im = hsv 
     18channels = { 
     19        'h': cv.CreateImage((im.width, im.height), cv.IPL_DEPTH_8U, 1), 
     20        's': cv.CreateImage((im.width, im.height), cv.IPL_DEPTH_8U, 1), 
     21        'v': cv.CreateImage((im.width, im.height), cv.IPL_DEPTH_8U, 1), 
     22} 
     23cv.Split(hsv, channels['h'], channels['s'], channels['v'], None) 
    1324 
    14 for sobel_aperture in (3, 5, 7): 
     25# Preallocate buffers 
     26 
     27# The output buffer for the sobel operation 
     28sobel = cv.CreateMat(im.height, im.width, cv.CV_32FC1) 
     29# 8-bit scaled version of sobel that can be saved to a file 
     30sobel_img = cv.CreateImage((im.width, im.height), cv.IPL_DEPTH_8U, 1) 
     31 
     32# Loop through the operations 
     33for (sobel_aperture, channel) in operations: 
    1534        print "sobelizing", 
    1635        start = time.time() 
    17         sobel = cv.CreateMat(im.height, im.width, cv.CV_32FC3) 
    18         #sobel_x = cv.CreateMat(im.height, im.width, cv.CV_32FC3) 
    19         #sobel_y = cv.CreateMat(im.height, im.width, cv.CV_32FC3) 
    20         #cv.Sobel(im, sobel_x, 1, 0, 3) 
    21         #cv.Sobel(im, sobel_y, 0, 1, 3) 
    22         cv.Sobel(im, sobel, 1, 1, sobel_aperture) 
     36        cv.Sobel(channels[channel], sobel, 1, 1, sobel_aperture) 
    2337        print "took %.2fs" % (time.time() - start) 
    2438 
    25         #cv.ShowImage('window', sobel) 
    26         #cv.WaitKey() 
    27         sobel_img = cv.CreateImage((im.width, im.height), cv.IPL_DEPTH_8U, 3) 
    2839        cv.ConvertScale(sobel, sobel_img, 8.0) 
    29         sobel_h = cv.CreateImage((im.width, im.height), cv.IPL_DEPTH_8U, 1) 
    30         sobel_s = cv.CreateImage((im.width, im.height), cv.IPL_DEPTH_8U, 1) 
    31         sobel_v = cv.CreateImage((im.width, im.height), cv.IPL_DEPTH_8U, 1) 
    32         cv.Split(sobel_img, sobel_h, sobel_s, sobel_v, None) 
    33         cv.SaveImage('%s-sobel-h-%s.png' % (fn, sobel_aperture), sobel_h) 
    34         cv.SaveImage('%s-sobel-s-%s.png' % (fn, sobel_aperture), sobel_s) 
    35         cv.SaveImage('%s-sobel-v-%s.png' % (fn, sobel_aperture), sobel_v) 
    36         print "wrote %s-sobel-[hsv]-%s.png" % (fn, sobel_aperture) 
    37 sys.exit(0) 
     40 
     41        out_fn = '{fn}-sobel-{channel}-{aperture}.png'.format(fn=fn, channel=channel, aperture=sobel_aperture) 
     42        cv.SaveImage(out_fn, sobel_img) 
     43        print "wrote {0}".format(out_fn) 
    3844 
    3945#print "median"