Rose-Hulman Robotics Team

Changeset 687

Show
Ignore:
Timestamp:
02/21/10 16:41:59 (2 years ago)
Author:
mosttw
Message:

Changes to hist_test.m that speed it up, but broke the Hough transform

Location:
branches/2010-image-rec
Files:
1 removed
3 modified

Legend:

Unmodified
Added
Removed
  • branches/2010-image-rec/hist_test.m

    r683 r687  
    1 function hist_test(fn, show_images) 
    2  
    3 % Test how well histogramming works with our test images 
    4 %fn = 'images/course-walk/cimg4943.jpg'; 
    5 %fn = 'images/course-walk/cimg4921.jpg'; 
    6 %fn = 'images/elphel/cam-000078.jpeg'; 
    7 %$fn = 'images/elphel/cam-000086.jpeg'; % Hard image 
    8 %fn = 'images/course-walk/cimg4989.jpg'; 
    9  
    10 %fn = 'images/elphel/cam-000094.jpeg'; 
    11 fn = 'images/elphel/cam-000081.jpeg'; 
    12  
    13 fprintf('Running on %s... ', fn); 
     1function hist_test(fn) 
     2 
     3show_images = 1; 
     4 
     5if show_images 
     6    close all; 
     7    %fn = 'images/course-walk/cimg4943.jpg'; 
     8    %fn = 'images/course-walk/cimg4921.jpg'; 
     9    %fn = 'images/elphel/cam-000078.jpeg'; 
     10    %$fn = 'images/elphel/cam-000086.jpeg'; % Hard image 
     11    %fn = 'images/course-walk/cimg4989.jpg'; 
     12 
     13    %fn = 'images/elphel/cam-000094.jpeg'; 
     14    fn = 'images/elphel/cam-000081.jpeg'; 
     15end 
     16 
     17fprintf('Running on %s...\n', fn); 
    1418 
    1519% Number of bins in the histogram. 
     
    2428s_mean_factor = 0.2; % Seems to most effect obstacle detection 
    2529v_mean_factor = 0.1; % Obstacles and lines 
    26 t_mean_factor = 0.01; 
    27 texture_band = 2; 
     30%t_mean_factor = 0.01; 
     31%texture_band = 2; 
    2832 
    2933im_rgb = imread(fn); 
     
    3337 
    3438im = zeros(rows, cols, 4); 
    35 imbig = rgb2hsv(im_rgb); 
    3639im_rgb = imresize(im_rgb, 0.25); 
    3740 
    38  
    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); 
     41%fprintf('Sobelizing\n'); 
    4342edges = double(imread([fn '-sobel-v-5.png']))/255; 
    44 texture = filter2(fspecial('average', 10), edges); 
    45 texture = filter2(fspecial('average', 10), texture); 
    46 texture = filter2(fspecial('average', 10), texture); 
    47 texture = filter2(fspecial('average', 10), texture); 
    48 texture = filter2(fspecial('average', 10), texture); 
    49 texture = filter2(fspecial('average', 10), texture); 
    50 texture = filter2(fspecial('average', 10), texture); 
    51 texture = filter2(fspecial('average', 10), texture); 
    52 texture = filter2(fspecial('average', 10), texture); 
    53 texture = filter2(fspecial('average', 10), texture); 
    54 texture = filter2(fspecial('average', 10), texture); 
     43 
     44fprintf('Blurring sobel\n'); 
     45texture = imresize(edges, .25); 
     46%texture = filter2(fspecial('average', 3), texture); 
    5547texture(texture > 1) = 1; 
    5648 
    57 figure(1); 
    58 imwrite(texture, [fn '-texture-full.png']); 
    59  
    60 texture = imresize(texture, .25); 
    61  
     49if show_images 
     50    figure('Name','Texture'), imshow(texture); 
     51else 
     52    imwrite(texture, [fn '-texture.png']); 
     53end 
     54 
     55 
     56fprintf('Histogramming\n'); 
    6257im(:,:,1:3) = rgb2hsv(im_rgb); 
    63 im(:,:,4) = texture; 
     58%im(:,:,4) = texture; 
    6459 
    6560 
     
    7368grass_s_hist = hist(reshape(clear_area(:,:,2), 1, []), hist_bins); 
    7469grass_v_hist = hist(reshape(clear_area(:,:,3), 1, []), hist_bins); 
    75 grass_t_hist = hist(reshape(clear_area(:,:,4), 1, []), hist_bins); 
     70%grass_t_hist = hist(reshape(clear_area(:,:,4), 1, []), hist_bins); 
    7671 
    7772%hist(reshape(clear_area(:,:,1), 1, []), hist_bins);  %displays histogram 
     
    8277s = ceil(im(:,:,2) * (hist_bins - 1)) + 1; 
    8378v = ceil(im(:,:,3) * (hist_bins - 1)) + 1; 
    84 t = ceil(im(:,:,4) * (hist_bins - 1)) + 1; 
     79%t = ceil(im(:,:,4) * (hist_bins - 1)) + 1; 
    8580 
    8681% Make masks from the histograms. 
     
    9489%grass_t_mask(grass_t_hist < mean(grass_t_hist) * t_mean_factor) = 0; 
    9590 
     91fprintf('Generating mask\n'); 
    9692% Ungodly slow loop through the pixels 
    9793mask = zeros(rows, cols); 
     
    9995    for c = 1:cols 
    10096        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)) 
    102             mask(r,c) = 1; 
     97                grass_v_mask(v(r,c)) % && grass_t_mask(t(r,c)) 
     98            mask(r, c) = 1; 
    10399        end 
    104100    end 
    105101end 
    106102 
    107 imwrite(mask, [fn '-mask-raw.png']); 
     103if show_images 
     104    figure('Name','Raw mask'), imshow(mask) 
     105else 
     106    imwrite(mask, [fn '-mask-raw.png']); 
     107end 
    108108 
    109109% Clean things up a bit 
     
    111111mask = imclose(mask, se); 
    112112 
    113 imwrite(mask, [fn, '-mask-closed.png']); 
    114  
    115 tempMask=mask*-1+1; 
    116 bottomlist=bottomFind(tempMask); 
    117 imshow(tempMask); 
     113if show_images 
     114    figure('Name','Cleaned up mask'), imshow(mask) 
     115else 
     116    imwrite(mask, [fn '-mask-cleaned.png']); 
     117end 
     118 
     119fprintf('Locating obstacles\n'); 
     120tempMask = mask * -1 + 1; 
     121bottomlist = bottomFind(tempMask); 
     122figure('Name','Obstacle points'), imshow(im_rgb); 
    118123hold on; 
    119 scatter(bottomlist(:,1),-bottomlist(:,2)); 
    120 print('-depsc', [fn '-obstacle-points.png']); 
    121  
    122 line_mask = texture; 
     124scatter(bottomlist(:,1), -bottomlist(:,2)); 
     125if ~show_images 
     126    print('-depsc', [fn '-obstacle-points.eps']); 
     127end 
     128 
     129line_mask = zeros(size(texture)); 
    123130threshold = .42; 
    124131line_mask(texture > threshold & mask == 0) = 1; 
    125 line_mask(texture <= threshold | mask ~= 0) = 0; 
    126 imwrite(line_mask, [fn '-line-mask.png']); 
     132if show_images 
     133    figure('Name','Line mask'), imshow(line_mask) 
     134else 
     135    imwrite(line_mask, [fn '-line-mask.png']); 
     136end 
    127137 
    128138% Display 
    129 figure(2); 
    130 imshow([double(im_rgb) / 255])%; bw2rgb(mask)]); 
     139figure('Name','Clear area'), imshow([double(im_rgb) / 255])%; bw2rgb(mask)]); 
    131140 
    132141% Draw a box over the "clear" area 
     
    137146line([left_x right_x right_x left_x left_x], ... 
    138147     [top_y top_y bottom_y bottom_y top_y],'LineWidth',4,'Color','red'); 
    139   
    140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%5 
    141  
    142 figure(3); 
     148 
     149if ~show_images 
     150    print('-depsc', [fn '-clear-area.png']); 
     151end 
     152 
     153 
     154fprintf('Hough transform\n'); 
    143155 
    144156[H,T,R] = hough(line_mask); 
     157figure('Name','Hough peaks'); 
    145158imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit'); 
    146159xlabel('\theta'), ylabel('\rho'); 
     
    150163y = R(P(:,1)); 
    151164plot(x,y,'s','color','white'); 
    152 print('-depsc', [fn '-hough-peaks.eps']); 
     165if ~show_images 
     166    print('-depsc', [fn '-hough-peaks.eps']); 
     167end 
    153168 
    154169lines = houghlines(line_mask,T,R,P,'FillGap',5,'MinLength',7); 
    155 figure, imshow(im_rgb), hold on 
    156 max_len = 0; 
     170figure('Name','Hough lines'), imshow(im_rgb), hold on 
    157171for k = 1:length(lines) 
    158172    xy = [lines(k).point1; lines(k).point2]; 
     
    162176    plot(xy(1,1),xy(1,2),'x','LineWidth',8,'Color','yellow'); 
    163177    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 
    171 end 
    172  
    173 % highlight the longest line segment 
    174 plot(xy_long(:,1),xy_long(:,2),'LineWidth',8,'Color','cyan'); 
    175 print('-depsc', [fn '-hough-lines.eps']); 
    176  
    177  
    178 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
     178end 
     179 
     180if ~show_points 
     181    print('-depsc', [fn '-hough-lines.eps']); 
     182end 
     183 
    179184 
    180185fprintf('Done\n'); 
  • branches/2010-image-rec/run.sh

    r683 r687  
    1212        fi 
    1313        matlab $MFLAGS -r "hist_test('"$i"'); exit" 
     14        exit 
    1415done 
    1516