"Fossies" - the Fresh Open Source Software Archive

Member "sip-0.12.1/macros/color_segmentation.sci" (22 Oct 2011, 2507 Bytes) of package /linux/privat/sip-0.12.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Scilab source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 function [labeled_img, cmap, uncertainty_level] = color_segmentation(RGB, method)
    2 // Performs color detection / segmentation in an image
    3 //
    4 // AUTHOR 
    5 //  Ricardo Fabbri <rfabbri@gmail.com>
    6 //
    7 // USAGE
    8 // 
    9 // INPUT
   10 //  RGB - truecolor image (3D array)
   11 //  method - 'hsv_sip' (default), 'distance_to_reference'
   12 //
   13 // OUTPUT
   14 //  labeled_rgb, cmap - labeled_rgb is a 2D array the same size as RGB, where at (i,j) we have a
   15 //  reference color id (working as a segmentation label) to the real color in
   16 //  the colormap cmap. The color code is as follows:
   17 //
   18 //  labeled_img(i,j)     cmap(labeled_img(i,j))
   19 //  1                    [1 0 0]  (red)
   20 //  2                    [0 1 0]  (green)
   21 //  3                    [0 0 1]  (blue)
   22 //  4                    [0 0 0]  (black)
   23 //  5                    [1 1 0]  (yellow)
   24 //  6                    [1 1 1]  (white)
   25 //
   26 //  you can show the segmentation using imshow(labeled_img, cmap);
   27 //   
   28 //  uncertainty_level - a 2D array the same size as RGB, where at (i,j) it is 0
   29 //  for fully certain, 1 is a good guess, 2 is uncertain
   30 //
   31 select argn(2)
   32   case 0
   33     error('Invalid number of arguments.')
   34   case 1
   35     method = 'hsv_sip';
   36 end
   37 
   38 id_red = 1;
   39 id_green = 2;
   40 id_blue = 3;
   41 id_black = 4;
   42 id_yellow = 5;
   43 id_white = 6;
   44 
   45 id_certain = 0;
   46 id_good_guess = 1;
   47 id_unreliable = 2;
   48 
   49 r = size(RGB,1)
   50 c = size(RGB,2)
   51 
   52 labeled_img = zeros(r,c);
   53 uncertainty_level = zeros(r,c);
   54 
   55 for i=1:r
   56   for j=1:c
   57     [label, certainty_level] = color_classify_single(RGB(i,j,:)(:)', method);
   58 
   59     select label
   60     case 'red'
   61       labeled_img(i,j) =  id_red;
   62     case 'green'
   63       labeled_img(i,j) =  id_green;
   64     case 'blue'
   65       labeled_img(i,j) =  id_blue;
   66     case 'black'
   67       labeled_img(i,j) =  id_black;
   68     case 'yellow'
   69       labeled_img(i,j) =  id_yellow;
   70     case 'white'
   71       labeled_img(i,j) =  id_white;
   72     else
   73       error('Invalid color class ' + label + ' returned for ' + string(i) + ', ' + string(j));
   74     end
   75 
   76     select certainty_level
   77     case 'certain'
   78       uncertainty_level(i,j) = id_certain;
   79     case 'good guess'
   80       uncertainty_level(i,j) = id_good_guess;
   81     case 'unreliable'
   82       uncertainty_level(i,j) = id_unreliable;
   83     else
   84       error('Unknown certainty label ' + certainty_level + ' returned for ' + string(i) + ', ' + string(j));
   85     end
   86   end
   87 end
   88 
   89 cmap = zeros(6,3);
   90 cmap(id_red,:) = [1 0 0];
   91 cmap(id_green,:) = [0 1 0];
   92 cmap(id_blue,:) = [0 0 1];
   93 cmap(id_black,:) = [0 0 0];
   94 cmap(id_yellow,:) = [1 1 0];
   95 cmap(id_white,:) = [1 1 1];
   96 
   97 endfunction