"Fossies" - the Fresh Open Source Software Archive

Member "sip-0.12.1/macros/color_classify_single.sci" (8 Nov 2011, 7808 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 [label, certainty_level, confidence, secondary_label] = color_classify_single(RGB, method)
    2 //
    3 // Classifies a single RGB triplet into Red, Green, Blue, Yellow, Black, and others.
    4 // Should work well under different lighting conditions and is robust to wrong white
    5 // balance. Should be simple enough for real time applications.
    6 //
    7 // AUTHOR 
    8 //  Ricardo Fabbri <rfabbri@gmail.com>
    9 //
   10 // USAGE
   11 //
   12 // INPUT 
   13 //  RGB - [r g b] triplet
   14 //  method - 'hsv_sip', 'distance_to_reference'
   15 //
   16 // OUTPUT 
   17 //
   18 //  label - 'red', 'green', 'blue', 'yellow', 'black', 'white' or something else
   19 //
   20 //  certainty_level - 'certain', 'good guess', 'unreliable'.
   21 //
   22 //  confidence - a scalar from 0 to 1 indicating the confidence in the guess,
   23 //  for those algorithms that enable it; for other algorithms this is just -1.
   24 //   
   25 //  secondary_label - a secondary class label for the color when ambiguity
   26 //  occurs. This will only be set if certainty_level <> 'certain'. Multiple
   27 //  labels can be present here and are separated by '-'.
   28 
   29 select argn(2)
   30   case 0
   31     error('Invalid number of arguments.')
   32   case 1
   33     method = 'hsv_sip';
   34 end
   35 
   36 certainty_level = 'certain';
   37 confidence = -1.0;
   38 secondary_label = '';
   39 
   40 
   41 select method
   42 // -------------------------------------------------------------------------
   43 case 'hsv_sip'
   44   // median is better when the image has patches of other colors.
   45   // otherwise the performance is similar.
   46 
   47   r = RGB(1);
   48   g = RGB(2);
   49   b = RGB(3);
   50 
   51   hsv = rgb2hsv([r g b]);
   52 
   53   hue = hsv(1);
   54   sat = hsv(2);
   55   val = hsv(3);
   56 
   57   hue = hue * 360;
   58 
   59   label = '';
   60   if val < 0.3
   61     if sat > 0.3 & val > 0.2
   62       secondary_label = 'black';
   63 
   64       if sat > 0.6 & (hue >= 65 & hue <= 170)
   65         label = 'green';
   66         return;
   67       elseif sat > 0.8
   68         certainty_level = 'unreliable'; 
   69       else
   70         certainty_level = 'good guess'; 
   71       end
   72       // will proceed below to guessing colors
   73     else
   74       label = 'black';
   75       return;
   76     end
   77   elseif (val > 0.8 & sat < 0.2) | (val > 0.7 & sat < 0.1) | (val > 0.6 & sat < 0.05)
   78     label = 'white';
   79     if sat > 0.1
   80       // light baby colors; could be white with offset colorbalance.
   81       certainty_level = 'unreliable'; 
   82       // will proceed below to guessing colors
   83       label = '';
   84       secondary_label = 'white';
   85     else
   86       if val < 0.7
   87         secondary_label = 'gray';
   88         certainty_level = 'good guess';
   89       end
   90       return;
   91     end
   92   elseif val < 0.65 & sat < 0.15 |...
   93          val < 0.70 & sat < 0.1  |...
   94          val < 0.5  & sat < 0.3 & (hue < 40 & hue > 10) // heuristica ~ marrons
   95     if val < 0.50
   96       label = 'black'
   97       certainty_level = 'good guess';
   98       if val > 0.40
   99         secondary_label = 'gray';
  100       end
  101       return;
  102     else
  103       if sat < 0.08
  104         certainty_level = 'good guess';
  105         label = 'white';
  106         secondary_label = 'gray';
  107         return;
  108       else
  109         certainty_level = 'unreliable';
  110         secondary_label = 'gray';
  111       end
  112     end
  113   end
  114 
  115   // RED
  116   if hue < 30 | hue > 330
  117     // disp 'maybe red!' 
  118     if hue > 10 & hue <= 30 & sat < 0.4
  119       // disp 'some beige or organge-yellow or salmon!' 
  120       if hue > 20
  121         // disp 'dont know this color, but would guess yellow or yellow-gray, perhaps beige!' 
  122         label = label + 'yellow';
  123         certainty_level = 'good guess';
  124         if val < 0.7
  125           secondary_label = 'gray';
  126         end
  127       else
  128         // disp 'dont know this color, but would guess orange our brown-ish!' 
  129         label = label + 'red';
  130         certainty_level = 'unreliable';
  131         if val < 0.7
  132           secondary_label = 'gray';
  133         end
  134       end
  135     else
  136       label = label + 'red';
  137       if (sat < 0.3 & val < 0.6) | (sat < 0.4 & val < 0.35)
  138         if certainty_level == 'certain'
  139           certainty_level = 'good guess';
  140         end
  141       end
  142     end
  143   // GREEN
  144   elseif hue > 80 & hue < 170
  145     label = label + 'green';
  146     if sat < 0.2
  147       if certainty_level == 'certain'
  148         certainty_level = 'good guess';
  149       end
  150     end
  151 
  152     if hue > 160 & (sat < 0.6 | val > 0.6)
  153         certainty_level = 'unreliable';
  154         secondary_label = 'blue';
  155     end
  156   // BLUE
  157   elseif hue > 185 & hue < 270
  158     if sat < 0.3 
  159       if certainty_level == 'certain'
  160         certainty_level = 'good guess';
  161       end
  162       if val > 0.75
  163         label = 'white';
  164         secondary_label = 'blue';
  165         if certainty_level == 'certain'
  166           certainty_level = 'good guess';
  167         end
  168       else
  169         label = label + 'blue';
  170         if val < 0.6
  171           secondary_label = 'gray';
  172         else
  173           secondary_label = 'white';
  174         end
  175       end
  176     else
  177       label = label + 'blue';
  178     end
  179   // remaining YELLOW + OTHER cases
  180   else
  181     if sat < 0.5
  182       certainty_level = 'unreliable';
  183       if sip_get_verbose() == 'wordy'
  184         warning('unreliable!')
  185       end
  186       // in the real system you just discard this estimate and use the previous
  187       // estimate (e.g. previous frame) at this point
  188     end
  189     if hue >= 30 & hue <= 80
  190       if hue >= 70 & val <= 70
  191         label = label + 'green';
  192       else
  193         label = label + 'yellow';
  194       end
  195     else
  196       if certainty_level == 'certain'
  197         certainty_level = 'good guess';
  198       end
  199       if hue >= 150 & hue <= 185
  200         // hard test near cyan and put a secondary label.
  201         if hue >= 180
  202           label = label + 'blue'
  203         elseif hue < 170
  204           label = label + 'green'
  205           secondary_label = 'blue';
  206         else
  207           label = label + 'blue'
  208           secondary_label = 'green';
  209         end
  210       elseif hue >= 270 & hue <= 330
  211         // hard test near magenta and put a secondary label.
  212         if hue < 280
  213           label = label + 'blue'
  214           if isempty(secondary_label)
  215             secondary_label = 'purple-pink-lavender';
  216           end
  217         else
  218           label = label + 'red'
  219           if isempty(secondary_label)
  220             secondary_label = 'purple-pink-magenta';
  221           end
  222         end
  223       end
  224     end
  225   end
  226 
  227 // -------------------------------------------------------------------------
  228 case 'distance_to_reference'
  229 
  230   red_ref = [1 0 0];
  231   green_ref = [0 1 0];
  232   blue_ref = [0 0 1];
  233   black_ref = [0 0 0];
  234   yellow_ref = [1 1 0];
  235   white_ref = [1 1 1];
  236 
  237   refcolors = [red_ref; green_ref; blue_ref; black_ref; yellow_ref; white_ref];
  238   cnames = ['red', 'green', 'blue', 'black', 'yellow', 'white'];
  239 
  240   d_min = %inf;
  241   i_min = 0;
  242 
  243   nrefs = size(refcolors,1);
  244 
  245   for i=1:nrefs
  246     d = RGB-refcolors(i,:);
  247     d = d*d';
  248     if d < d_min
  249       second_d_min = d_min;
  250       second_i_min = i_min;
  251       d_min = d;
  252       i_min = i;
  253     end
  254   end
  255 
  256   label = cnames(i_min);
  257 
  258   if sqrt(d_min) > 0.8*sqrt(second_d_min)
  259     certainty_level = 'unreliable';    
  260   end
  261 else
  262   error('not yet implemented');
  263 end
  264 
  265 endfunction
  266 //
  267 // -------------------------------------------------------------------------
  268 // SIP - Scilab Image Processing toolbox
  269 // Copyright (C) 2002-2009  Ricardo Fabbri
  270 //
  271 // This program is free software; you can redistribute it and/or modify
  272 // it under the terms of the GNU General Public License as published by
  273 // the Free Software Foundation; either version 2 of the License, or
  274 // (at your option) any later version.
  275 //
  276 // This program is distributed in the hope that it will be useful,
  277 // but WITHOUT ANY WARRANTY; without even the implied warranty of
  278 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  279 // GNU General Public License for more details.
  280 //
  281 // You should have received a copy of the GNU General Public License
  282 // along with this program; if not, write to the Free Software
  283 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  284 // -------------------------------------------------------------------------
  285 //