"Fossies" - the Fresh Open Source Software Archive

Member "sip-0.12.1/macros/edge.sci" (9 Apr 2012, 4284 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. See also the latest Fossies "Diffs" side-by-side code changes report for "edge.sci": 0.5.6_vs_0.12.1.

    1 function [border, thresh]=edge(Img, method, thresh, dir_or_sig, sig)
    2 // 
    3 // AUTHOR
    4 //    Ricardo Fabbri  <rfabbri@(not this part) gmail d0t com>
    5 //    Cybernetic Vision Research Group
    6 //    Luciano da Fontoura Costa, supervisor.
    7 //    http://siptoolbox.sf.net
    8 //
    9 // REFERENCE
   10 //    "Algorithms for Image Processing and Computer Vision", 
   11 //    J.R. Parker, Wiley, chapter 1.
   12 //
   13 // TODO
   14 //    - "edge" routine should have parameters "same" and "valid", just
   15 //    like imconv.
   16 // 
   17 //
   18 // $Revision: 1.2 $ $Date: 2009-03-29 21:34:48 $
   19 
   20 if argn(2)==0 then
   21   error('Invalid number of arguments.')
   22 end
   23 
   24 //check image channel
   25 if size( size(Img),2) <> 2  then
   26   error('The input image should be a single channel image.');
   27 end
   28   
   29 
   30 if ~exists('method','local') then
   31   method='sobel'
   32 end
   33 if ~exists('thresh','local') then
   34   thresh=0.2;
   35 end
   36 
   37 
   38 direction='both'; //default value
   39 sigma = []; //default value
   40 
   41 if exists('dir_or_sig', 'local') then
   42   if( typeof(dir_or_sig) == 'string') then
   43     direction = dir_or_sig;
   44   elseif (typeof(dir_or_sig) == 'constant') then
   45     sigma = dir_or_sig;
   46   end
   47 end
   48 
   49 if exists('sig', 'local') then
   50   sigma = sig;
   51 end
   52 
   53 
   54 select method
   55 case 'sobel' then
   56   mask = mkfilter('sobel')
   57 case 'prewitt' then
   58   mask = mkfilter('prewitt')
   59 case 'canny' then
   60   //set default thresh value
   61   //negative value is invalid here
   62   if thresh < 0 then
   63     thresh = 0.5;
   64   end
   65   
   66   if(length(thresh)==1) then
   67     low_th=thresh*0.4;
   68     high_th=thresh;
   69   else
   70     low_th=thresh(1);
   71     high_th=thresh(2);
   72   end
   73   
   74   if isempty(sigma) then
   75     sigma=3;
   76   end
   77   if and(sigma <> [3,5,7]) then
   78     error('sigma for canny means kernel size (width), and must be 3, 5 or 7.');
   79   end
   80 
   81   if (typeof(Img(1)) == 'constant')
   82     Img = Img*255;
   83   end
   84   
   85     
   86   border = canny_c(Img, low_th*1024, high_th*1024, sigma);
   87   border = im2bw(double(border), 0.5);
   88   
   89   //END of CANNY
   90   return;
   91 case 'fftderiv' // fourier gradient
   92   if ~exists('sigma','local') | isempty(sigma) then
   93      sigma=1
   94   end
   95 
   96   [r,c] = size(Img)
   97   fu = [0:c-1]*(1/c)
   98   fv = [0:r-1]*(1/r)
   99   fu(int(c/2):c) = fu(int(c/2):c) - 1;
  100   fv(int(r/2):r) = fv(int(r/2):r) - 1;
  101   
  102   if sigma == 0
  103      gf = ones(r,c)
  104   else
  105      gf = ones(r,1) * exp(-(sigma*%pi*fu)^2)
  106      gv = exp(-(sigma*%pi*fv')^2) * ones(1,c)
  107      gf = gf .* gv
  108   end
  109 
  110   select direction
  111   case 'horizontal'
  112      fvp = %i*2*%pi*fv' * ones(1,c)
  113      Dyf = fft(Img,-1) .* gf .* fvp
  114      border = abs(fft(Dyf,1))
  115   case 'vertical'
  116      fup = ones(r,1) * %i*2*%pi*fu
  117      Dxf = fft(Img,-1) .* gf .* fup
  118      border = abs(fft(Dxf,1))
  119   case 'both'
  120      fup = ones(r,1) * %i*2*%pi*fu
  121      fvp = %i*2*%pi*fv' * ones(1,c)
  122      Dxf = fft(Img,-1) .* gf .* fup
  123      Dyf = fft(Img,-1) .* gf .* fvp
  124   
  125      Dx = abs(fft(Dxf,1))
  126      Dy = abs(fft(Dyf,1))
  127   
  128      border = sqrt(Dx.^2 + Dy.^2)
  129   else
  130      error('Invalid direction.');
  131   end
  132   if thresh >=0 then
  133      border=im2bw(border,thresh,max(border))
  134   end
  135   // END
  136   return
  137 else
  138   error('Invalid edge detection method.')
  139 end
  140 
  141 select direction
  142 case 'horizontal'
  143   mx=imconv(Img,mask);
  144   border=abs(mx)
  145 case 'vertical'
  146   my=imconv(Img,-mask');
  147   border=abs(my)
  148 case 'both'
  149   mx=imconv(Img,mask);
  150   my=imconv(Img,-mask');
  151   border=sqrt(mx.*mx + my.*my);
  152 else
  153   error('Invalid direction.');
  154 end
  155 
  156 if thresh >=0 then
  157   border=im2bw(border,thresh,max(border))
  158 end
  159 
  160 endfunction
  161 
  162 //
  163 // -------------------------------------------------------------------------
  164 // SIP - Scilab Image Processing toolbox
  165 // Copyright (C) 2002-2009  Ricardo Fabbri
  166 //
  167 // This program is free software; you can redistribute it and/or modify
  168 // it under the terms of the GNU General Public License as published by
  169 // the Free Software Foundation; either version 2 of the License, or
  170 // (at your option) any later version.
  171 //
  172 // This program is distributed in the hope that it will be useful,
  173 // but WITHOUT ANY WARRANTY; without even the implied warranty of
  174 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  175 // GNU General Public License for more details.
  176 //
  177 // You should have received a copy of the GNU General Public License
  178 // along with this program; if not, write to the Free Software
  179 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  180 // -------------------------------------------------------------------------
  181 //