how to do diagonal edge detection in image processing?

  • Follow


Dear all,

I want to ask how to do diagonal edge detection? Can it be done simply in
Matlab? I saw several edge detection techniques in Matlab, such as "canny"
edge detection method, but I just want to detect the presence of "diagonal
edge", are there any simple method?

Thanks a lot,

-Walala


0
Reply walala 12/29/2003 3:15:31 AM

you can easily change some code in canny method in Matlab to detect diagonal
edge




> I want to ask how to do diagonal edge detection? Can it be done simply in
> Matlab? I saw several edge detection techniques in Matlab, such as "canny"
> edge detection method, but I just want to detect the presence of "diagonal
> edge", are there any simple method?
>
> Thanks a lot,
>
> -Walala
>
>


0
Reply ANDY 12/29/2003 6:49:53 AM


Maybe you can try Prewitt or Sobel masks for detecting diagonal edges.
This is the simplest method.
Prewitt:
 0  1 1
-1  0 1
-1 -1 0 

-1 -1 0 
-1  0 1 
 0  1 1
Sobel:
 0  1 2
-1  0 1 
-2 -1 0

-2 -1 0
-1  0 1
 0  1 2

Moreover, If you can know the orientation of lines before processing,
the gabor filtering should be a good choice. Although it's relatively
complicated than conventional masks.
"walala" <mizhael@yahoo.com> wrote in message news:<bso6ko$ns4$1@mozo.cc.purdue.edu>...
> Dear all,
> 
> I want to ask how to do diagonal edge detection? Can it be done simply in
> Matlab? I saw several edge detection techniques in Matlab, such as "canny"
> edge detection method, but I just want to detect the presence of "diagonal
> edge", are there any simple method?
> 
> Thanks a lot,
> 
> -Walala
0
Reply zlli 12/29/2003 8:43:45 AM

"Arthur Lee" <zlli@nlpr.ia.ac.cn> wrote in message
news:781280fe.0312290043.75844de8@posting.google.com...
> Maybe you can try Prewitt or Sobel masks for detecting diagonal edges.
> This is the simplest method.
> Prewitt:
>  0  1 1
> -1  0 1
> -1 -1 0
>
> -1 -1 0
> -1  0 1
>  0  1 1
> Sobel:
>  0  1 2
> -1  0 1
> -2 -1 0
>
> -2 -1 0
> -1  0 1
>  0  1 2
>
> Moreover, If you can know the orientation of lines before processing,
> the gabor filtering should be a good choice. Although it's relatively
> complicated than conventional masks.

Hi,  Arthur,

The filters you've given helped a lot. But how about I just need to test the
presence of edges, ie., I don't need to actually take the edge-detection
filtering step, all I want is a logical value: 1-- a diagonal edge exists;
0 -- no diagonal edge exists...

Is there any simplest/fastest way of doing this?

Thanks a lot,

-Walala


0
Reply walala 12/29/2003 5:38:43 PM

"ANDY" <wangzhengyao@hotmail.com> wrote in message
news:bsoisp$1vvr$1@mail.cn99.com...
> you can easily change some code in canny method in Matlab to detect
diagonal
> edge
>
>
>
>

Hi,  Andy,

How to change the code? Can you give more details?

Another questions is how about I just need to test the presence of edges,
ie., I don't need to actually take the edge-detection filtering step, all I
want is a logical value: 1-- a diagonal edge exists; 0 -- no diagonal edge
exists...

Is there any simplest/fastest way of doing this?

Thanks a lot,

-Walala


0
Reply walala 12/29/2003 5:39:37 PM

walala:

For simplest/fastest way, do it with your eyes. Then you won't have to take
the "edge detection filtering step". I don't know any other way.

For the filter approach, simply make a threshold on the filter output: over
the threshold="logical value 1--a diagonal edge exists", and filter output
under the threshold="0--no diagonal edge exists".

Jim

> The filters you've given helped a lot. But how about I just need to test
the
> presence of edges, ie., I don't need to actually take the edge-detection
> filtering step, all I want is a logical value: 1-- a diagonal edge exists;
> 0 -- no diagonal edge exists...
>
> Is there any simplest/fastest way of doing this?
>
> Thanks a lot,
>
> -Walala
>
>


0
Reply Jim 12/29/2003 11:16:55 PM

hi,Walala

in Matlab (my matlab is version 6.5)
if you input "type edge" and run it
you can see:
_______________________________________________________________________

>> type edge

function [eout,thresh] = edge(varargin)
%EDGE Find edges in intensity image.
%   EDGE takes an intensity or a binary image I as its input, and returns a
%   binary image BW of the same size as I, with 1's where the function
%   finds edges in I and 0's elsewhere.
%
%   EDGE supports six different edge-finding methods:
%
%      The Sobel method finds edges using the Sobel approximation to the
%      derivative. It returns edges at those points where the gradient of
%      I is maximum.
%
%      The Prewitt method finds edges using the Prewitt approximation to
%      the derivative. It returns edges at those points where the gradient
%      of I is maximum.
%
%      The Roberts method finds edges using the Roberts approximation to
%      the derivative. It returns edges at those points where the gradient
%      of I is maximum.
%
%      The Laplacian of Gaussian method finds edges by looking for zero
%      crossings after filtering I with a Laplacian of Gaussian filter.
%
%      The zero-cross method finds edges by looking for zero crossings
%      after filtering I with a filter you specify.
%
%      The Canny method finds edges by looking for local maxima of the
%      gradient of I. The gradient is calculated using the derivative of a
%      Gaussian filter. The method uses two thresholds, to detect strong
%      and weak edges, and includes the weak edges in the output only if
%      they are connected to strong edges. This method is therefore less
%      likely than the others to be "fooled" by noise, and more likely to
%      detect true weak edges.
%
%   The parameters you can supply differ depending on the method you
%   specify. If you do not specify a method, EDGE uses the Sobel method.
%
%   Sobel Method
%   ------------
%   BW = EDGE(I,'sobel') specifies the Sobel method.
%
%   BW = EDGE(I,'sobel',THRESH) specifies the sensitivity threshold for
%   the Sobel method. EDGE ignores all edges that are not stronger than
%   THRESH.  If you do not specify THRESH, or if THRESH is empty ([]),
%   EDGE chooses the value automatically.
%
%   BW = EDGE(I,'sobel',THRESH,DIRECTION) specifies directionality for the
%   Sobel method. DIRECTION is a string specifying whether to look for
%   'horizontal' or 'vertical' edges, or 'both' (the default).
%
%   [BW,thresh] = EDGE(I,'sobel',...) returns the threshold value.
%
%   Prewitt Method
%   --------------
%   BW = EDGE(I,'prewitt') specifies the Prewitt method.
%
%   BW = EDGE(I,'prewitt',THRESH) specifies the sensitivity threshold for
%   the Prewitt method. EDGE ignores all edges that are not stronger than
%   THRESH. If you do not specify THRESH, or if THRESH is empty ([]),
%   EDGE chooses the value automatically.
%
%   BW = EDGE(I,'prewitt',THRESH,DIRECTION) specifies directionality for
%   the Prewitt method. DIRECTION is a string specifying whether to look
%   for 'horizontal' or 'vertical' edges, or 'both' (the default).
%
%   [BW,thresh] = EDGE(I,'prewitt',...) returns the threshold value.
%
%   Roberts Method
%   --------------
%   BW = EDGE(I,'roberts') specifies the Roberts method.
%
%   BW = EDGE(I,'roberts',THRESH) specifies the sensitivity threshold for
%   the Roberts method. EDGE ignores all edges that are not stronger than
%   THRESH. If you do not specify THRESH, or if THRESH is empty ([]),
%   EDGE chooses the value automatically.
%
%   [BW,thresh] = EDGE(I,'roberts',...) returns the threshold value.
%
%   Laplacian of Gaussian Method
%   ----------------------------
%   BW = EDGE(I,'log') specifies the Laplacian of Gaussian method.
%
%   BW = EDGE(I,'log',THRESH) specifies the sensitivity threshold for the
%   Laplacian of Gaussian method. EDGE ignores all edges that are not
%   stronger than THRESH. If you do not specify THRESH, or if THRESH is
%   empty ([]), EDGE chooses the value automatically.
%
%   BW = EDGE(I,'log',THRESH,SIGMA) specifies the Laplacian of Gaussian
%   method, using SIGMA as the standard deviation of the LoG filter. The
%   default SIGMA is 2; the size of the filter is N-by-N, where
%   N=CEIL(SIGMA*3)*2+1.
%
%   [BW,thresh] = EDGE(I,'log',...) returns the threshold value.
%
%   Zero-cross Method
%   -----------------
%   BW = EDGE(I,'zerocross',THRESH,H) specifies the zero-cross method,
%   using the specified filter H. If THRESH is empty ([]), EDGE chooses
%   the sensitivity threshold automatically.
%
%   [BW,THRESH] = EDGE(I,'zerocross',...) returns the threshold value.
%
%   Canny Method
%   ----------------------------
%   BW = EDGE(I,'canny') specifies the Canny method.
%
%   BW = EDGE(I,'canny',THRESH) specifies sensitivity thresholds for the
%   Canny method. THRESH is a two-element vector in which the first element
%   is the low threshold, and the second element is the high threshold. If
%   you specify a scalar for THRESH, this value is used for the high
%   threshold and 0.4*THRESH is used for the low threshold. If you do not
%   specify THRESH, or if THRESH is empty ([]), EDGE chooses low and high
%   values automatically.
%
%   BW = EDGE(I,'canny',THRESH,SIGMA) specifies the Canny method, using
%   SIGMA as the standard deviation of the Gaussian filter. The default
%   SIGMA is 1; the size of the filter is chosen automatically, based
%   on SIGMA.
%
%   [BW,thresh] = EDGE(I,'canny',...) returns the threshold values as a
%   two-element vector.
%
%   Class Support
%   -------------
%   I can be of class uint8, uint16, or double. BW is of class uint8.
%
%   Remarks
%   -------
%   For the 'log' and 'zerocross' methods, if you specify a
%   threshold of 0, the output image has closed contours, because
%   it includes all of the zero crossings in the input image.
%
%   Example
%   -------
%   Find the edges of the rice.tif image using the Prewitt and Canny
%   methods:
%
%       I = imread('rice.tif');
%       BW1 = edge(I,'prewitt');
%       BW2 = edge(I,'canny');
%       imshow(BW1)
%       figure, imshow(BW2)
%
%   See also FSPECIAL.

%   OBSOLETE syntax
%   --------------------
%   BW = EDGE(... ,K) allows the specification of a directionality
%   factor, K.  This only works for the 'sobel', 'prewitt', and
%   'roberts' methods.   K must be a 1-by-2 vector, K = [kx ky].
%   For Sobel and Prewitt, K=[1 0] looks for vertical edges,
%   K=[0 1] looks for horizontal edges, and K=[1 1], the default,
%   looks for non-directional edges.   For the Roberts edge detector,
%   K=[1 0] looks for 135 degree diagonal edges, K=[0 1] looks
%   for 45 degree diagonal edges, and K=[1 1], the default, looks
%   for non-directional edges.
%
%   Copyright 1993-2002 The MathWorks, Inc.
%   $Revision: 5.26 $  $Date: 2002/03/26 16:39:10 $

[a,method,thresh,sigma,H,kx,ky] = parse_inputs(varargin{:});

% Transform to a double precision intensity image if necessary
if ~isa(a, 'double')
   a = im2double(a);
end

m = size(a,1);
n = size(a,2);
rr = 2:m-1; cc=2:n-1;

% The output edge map:
e = repmat(false, m, n);

if strcmp(method,'canny')
   % Magic numbers
   GaussianDieOff = .0001;
   PercentOfPixelsNotEdges = .7; % Used for selecting thresholds
   ThresholdRatio = .4;          % Low thresh is this fraction of the high.

   % Design the filters - a gaussian and its derivative

   pw = 1:30; % possible widths
   ssq = sigma*sigma;
   width = max(find(exp(-(pw.*pw)/(2*sigma*sigma))>GaussianDieOff));
   if isempty(width)
      width = 1;  % the user entered a really small sigma
   end

   t = (-width:width);
   gau = exp(-(t.*t)/(2*ssq))/(2*pi*ssq);     % the gaussian 1D filter

   % Find the directional derivative of 2D Gaussian (along X-axis)
   % Since the result is symmetric along X, we can get the derivative along
   % Y-axis simply by transposing the result for X direction.
   [x,y]=meshgrid(-width:width,-width:width);
   dgau2D=-x.*exp(-(x.*x+y.*y)/(2*ssq))/(pi*ssq);

   % Convolve the filters with the image in each direction
   % The canny edge detector first requires convolution with
   % 2D gaussian, and then with the derivitave of a gaussian.
   % Since gaussian filter is separable, for smoothing, we can use
   % two 1D convolutions in order to achieve the effect of convolving
   % with 2D Gaussian.  We convolve along rows and then columns.

   %smooth the image out
   aSmooth=imfilter(a,gau,'conv','replicate');         % run the filter
accross rows
   aSmooth=imfilter(aSmooth,gau','conv','replicate');  % and then accross
columns

   %apply directional derivatives
   ax = imfilter(aSmooth, dgau2D, 'conv','replicate');
   ay = imfilter(aSmooth, dgau2D', 'conv','replicate');

   mag = sqrt((ax.*ax) + (ay.*ay));
   magmax = max(mag(:));
   if magmax>0
      mag = mag / magmax;   % normalize
   end

   % Select the thresholds
   if isempty(thresh)
      [counts,x]=imhist(mag, 64);
      highThresh = min(find(cumsum(counts) > PercentOfPixelsNotEdges*m*n)) /
64;
      lowThresh = ThresholdRatio*highThresh;
      thresh = [lowThresh highThresh];
   elseif length(thresh)==1
      highThresh = thresh;
      if thresh>=1
         error('The threshold must be less than 1.');
      end
      lowThresh = ThresholdRatio*thresh;
      thresh = [lowThresh highThresh];
   elseif length(thresh)==2
      lowThresh = thresh(1);
      highThresh = thresh(2);
      if (lowThresh >= highThresh) | (highThresh >= 1)
         error('Thresh must be [low high], where low < high < 1.');
      end
   end

   % The next step is to do the non-maximum supression.
   % We will accrue indices which specify ON pixels in strong edgemap
   % The array e will become the weak edge map.
   idxStrong = [];
   for dir = 1:4
      idxLocalMax = cannyFindLocalMaxima(dir,ax,ay,mag);
      idxWeak = idxLocalMax(mag(idxLocalMax) > lowThresh);
      e(idxWeak)=1;
      idxStrong = [idxStrong; idxWeak(mag(idxWeak) > highThresh)];
   end

   rstrong = rem(idxStrong-1, m)+1;
   cstrong = floor((idxStrong-1)/m)+1;
   e = bwselect(e, cstrong, rstrong, 8);
   e = bwmorph(e, 'thin', 1);  % Thin double (or triple) pixel wide contours

elseif any(strcmp(method, {'log','marr-hildreth','zerocross'}))
   % We don't use image blocks here
   if isempty(H),
      fsize = ceil(sigma*3) * 2 + 1;  % choose an odd fsize > 6*sigma;
      op = fspecial('log',fsize,sigma);
   else
      op = H;
   end

   op = op - sum(op(:))/prod(size(op)); % make the op to sum to zero
   b = filter2(op,a);

   if isempty(thresh)
      thresh = .75*mean2(abs(b(rr,cc)));
   end

   % Look for the zero crossings:  +-, -+ and their transposes
   % We arbitrarily choose the edge to be the negative point
   [rx,cx] = find( b(rr,cc) < 0 & b(rr,cc+1) > 0 ...
      & abs( b(rr,cc)-b(rr,cc+1) ) > thresh );   % [- +]
   e((rx+1) + cx*m) = 1;
   [rx,cx] = find( b(rr,cc-1) > 0 & b(rr,cc) < 0 ...
      & abs( b(rr,cc-1)-b(rr,cc) ) > thresh );   % [+ -]
   e((rx+1) + cx*m) = 1;
   [rx,cx] = find( b(rr,cc) < 0 & b(rr+1,cc) > 0 ...
      & abs( b(rr,cc)-b(rr+1,cc) ) > thresh);   % [- +]'
   e((rx+1) + cx*m) = 1;
   [rx,cx] = find( b(rr-1,cc) > 0 & b(rr,cc) < 0 ...
      & abs( b(rr-1,cc)-b(rr,cc) ) > thresh);   % [+ -]'
   e((rx+1) + cx*m) = 1;

   % Most likely this covers all of the cases.   Just check to see if there
   % are any points where the LoG was precisely zero:
   [rz,cz] = find( b(rr,cc)==0 );
   if ~isempty(rz)
      % Look for the zero crossings: +0-, -0+ and their transposes
      % The edge lies on the Zero point
      zero = (rz+1) + cz*m;   % Linear index for zero points
      zz = find(b(zero-1) < 0 & b(zero+1) > 0 ...
         & abs( b(zero-1)-b(zero+1) ) > 2*thresh);     % [- 0 +]'
      e(zero(zz)) = 1;
      zz = find(b(zero-1) > 0 & b(zero+1) < 0 ...
         & abs( b(zero-1)-b(zero+1) ) > 2*thresh);     % [+ 0 -]'
      e(zero(zz)) = 1;
      zz = find(b(zero-m) < 0 & b(zero+m) > 0 ...
         & abs( b(zero-m)-b(zero+m) ) > 2*thresh);     % [- 0 +]
  
   e(zero(zz)) = 1;
      zz = find(b(zero-m) > 0 & b(zero+m) < 0 ...
         & abs( b(zero-m)-b(zero+m) ) > 2*thresh);     % [+ 0 -]
      e(zero(zz)) = 1;
   end

else  % one of the easy methods (roberts,sobel,prewitt)

   % Determine edges in blocks for easy methods
   nr = length(rr); nc = length(cc);

   blk = bestblk([nr nc]);
   nblks = floor([nr nc]./blk); nrem = [nr nc] - nblks.*blk;
   mblocks = nblks(1); nblocks = nblks(2);
   mb = blk(1); nb = blk(2);

   if strcmp(method,'sobel')
      op = [-1 -2 -1;0 0 0;1 2 1]/8; % Sobel approximation to derivative
      bx = abs(filter2(op',a)); by = abs(filter2(op,a));
      b = kx*bx.*bx + ky*by.*by;
      if isempty(thresh), % Determine cutoff based on RMS estimate of noise
         cutoff = 4*sum(sum(b(rr,cc)))/prod(size(b(rr,cc))); thresh =
sqrt(cutoff);
      else                   % Use relative tolerance specified by the user
         cutoff = (thresh).^2;
      end
      rows = 1:blk(1);
      for i=0:mblocks,
         if i==mblocks, rows = (1:nrem(1)); end
         for j=0:nblocks,
            if j==0, cols = 1:blk(2); elseif j==nblocks, cols=(1:nrem(2));
end
            if ~isempty(rows) & ~isempty(cols)
               r = rr(i*mb+rows); c = cc(j*nb+cols);
               e(r,c) = (b(r,c)>cutoff) & ...
               ( ( (bx(r,c) >= (kx*by(r,c)-eps*100)) & ...
               (b(r,c-1) <= b(r,c)) & (b(r,c) > b(r,c+1)) ) | ...
               ( (by(r,c) >= (ky*bx(r,c)-eps*100 )) & ...
               (b(r-1,c) <= b(r,c)) & (b(r,c) > b(r+1,c))));
            end
         end
      end

   elseif strcmp(method,'prewitt')
      op = [-1 -1 -1;0 0 0;1 1 1]/6; % Prewitt approximation to derivative
      bx = abs(filter2(op',a)); by = abs(filter2(op,a));
      b = kx*bx.*bx + ky*by.*by;
      if isempty(thresh), % Determine cutoff based on RMS estimate of noise
         cutoff = 4*sum(sum(b(rr,cc)))/prod(size(b(rr,cc))); thresh =
sqrt(cutoff);
      else                   % Use relative tolerance specified by the user
         cutoff = (thresh).^2;
      end
      rows = 1:blk(1);
      for i=0:mblocks,
         if i==mblocks, rows = (1:nrem(1)); end
         for j=0:nblocks,
            if j==0, cols = 1:blk(2); elseif j==nblocks, cols=(1:nrem(2));
end
            if ~isempty(rows) & ~isempty(cols)
               r = rr(i*mb+rows); c = cc(j*nb+cols);
               e(r,c) = (b(r,c)>cutoff) & ...
               ( ( (bx(r,c) >= (kx*by(r,c)-eps*100) ) & ...
               (b(r,c-1) <= b(r,c)) & (b(r,c) > b(r,c+1)) ) | ...
               ((by(r,c) >= (ky*bx(r,c)-eps*100) )  & ...
               (b(r-1,c) <= b(r,c)) & (b(r,c) > b(r+1,c)) ) );
            end
         end
      end

   elseif strcmp(method, 'roberts')
      op = [1 0;0 -1]/sqrt(2); % Roberts approximation to diagonal
derivative
      bx = abs(filter2(op,a)); by = abs(filter2(rot90(op),a));
      b = kx*bx.*bx + ky*by.*by;
      if isempty(thresh), % Determine cutoff based on RMS estimate of noise
         cutoff = 6*sum(sum(b(rr,cc)))/prod(size(b(rr,cc))); thresh =
sqrt(cutoff);
      else                   % Use relative tolerance specified by the user
         cutoff = (thresh).^2;
      end
      rows = 1:blk(1);
      for i=0:mblocks,
         if i==mblocks, rows = (1:nrem(1)); end
         for j=0:nblocks,
            if j==0, cols = 1:blk(2); elseif j==nblocks, cols=(1:nrem(2));
end
            if ~isempty(rows) & ~isempty(cols)
               r = rr(i*mb+rows); c = cc(j*nb+cols);
               e(r,c) = (b(r,c)>cutoff) & ...
               ( ( (bx(r,c) >= (kx*by(r,c)-eps*100)) & ...
               (b(r-1,c-1) <= b(r,c)) & (b(r,c) > b(r+1,c+1)) ) | ...
               ( (by(r,c) >= (ky*bx(r,c)-eps*100)) & ...
               (b(r-1,c+1) <= b(r,c)) & (b(r,c) > b(r+1,c-1)) ) );
            end
         end
      end
   else
      error([method,' is not a valid method.']);
   end
end

if nargout==0,
   imshow(e);
else
   eout = e;
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%   Local Function : cannyFindLocalMaxima
%
function idxLocalMax = cannyFindLocalMaxima(direction,ix,iy,mag);
%
% This sub-function helps with the non-maximum supression in the Canny
% edge detector.  The input parameters are:
%
%   direction - the index of which direction the gradient is pointing,
%               read from the diagram below. direction is 1, 2, 3, or 4.
%   ix        - input image filtered by derivative of gaussian along x
%   iy        - input image filtered by derivative of gaussian along y
%   mag       - the gradient magnitude image
%
%    there are 4 cases:
%
%                         The X marks the pixel in question, and each
%         3     2         of the quadrants for the gradient vector
%       O----0----0       fall into two cases, divided by the 45
%     4 |         | 1     degree line.  In one case the gradient
%       |         |       vector is more horizontal, and in the other
%       O    X    O       it is more vertical.  There are eight
%       |         |       divisions, but for the non-maximum supression
%    (1)|         |(4)    we are only worried about 4 of them since we
%       O----O----O       use symmetric points about the center pixel.
%        (2)   (3)


[m,n,o] = size(mag);

% Find the indices of all points whose gradient (specified by the
% vector (ix,iy)) is going in the direction we're looking at.

switch direction
case 1
   idx = find((iy<=0 & ix>-iy)  | (iy>=0 & ix<-iy));
case 2
   idx = find((ix>0 & -iy>=ix)  | (ix<0 & -iy<=ix));
case 3
   idx = find((ix<=0 & ix>iy) | (ix>=0 & ix<iy));
case 4
   idx = find((iy<0 & ix<=iy) | (iy>0 & ix>=iy));
end

% Exclude the exterior pixels
if ~isempty(idx)
   v = mod(idx,m);
   extIdx = find(v==1 | v==0 | idx<=m | (idx>(n-1)*m));
   idx(extIdx) = [];
end

ixv = ix(idx);
iyv = iy(idx);
gradmag = mag(idx);

% Do the linear interpolations for the interior pixels
switch direction
case 1
   d = abs(iyv./ixv);
   gradmag1 = mag(idx+m).*(1-d) + mag(idx+m-1).*d;
   gradmag2 = mag(idx-m).*(1-d) + mag(idx-m+1).*d;
case 2
   d = abs(ixv./iyv);
   gradmag1 = mag(idx-1).*(1-d) + mag(idx+m-1).*d;
   gradmag2 = mag(idx+1).*(1-d) + mag(idx-m+1).*d;
case 3
   d = abs(ixv./iyv);
   gradmag1 = mag(idx-1).*(1-d) + mag(idx-m-1).*d;
   gradmag2 = mag(idx+1).*(1-d) + mag(idx+m+1).*d;
case 4
   d = abs(iyv./ixv);
   gradmag1 = mag(idx-m).*(1-d) + mag(idx-m-1).*d;
   gradmag2 = mag(idx+m).*(1-d) + mag(idx+m+1).*d;
end
idxLocalMax = idx(gradmag>=gradmag1 & gradmag>=gradmag2);



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%   Local Function : parse_inputs
%
function [I,Method,Thresh,Sigma,H,kx,ky] = parse_inputs(varargin)
% OUTPUTS:
%   I      Image Data
%   Method Edge detection method
%   Thresh Threshold value
%   Sigma  standard deviation of Gaussian
%   H      Filter for Zero-crossing detection
%   kx,ky  From Directionality vector

error(nargchk(1,5,nargin));

I = varargin{1};

checkinput(I,{'double','logical','uint8','uint16'},...
             {'nonsparse'},mfilename,'I',1);

% Defaults
Method='sobel';
Thresh=[];
Direction='both';
Sigma=2;
H=[];
K=[1 1];

methods =
{'canny','prewitt','sobel','marr-hildreth','log','roberts','zerocross'};
directions = {'both','horizontal','vertical'};

% Now parse the nargin-1 remaining input arguments

% First get the strings - we do this because the intepretation of the
% rest of the arguments will depend on the method.
nonstr = [];   % ordered indices of non-string arguments
for i = 2:nargin
   if ischar(varargin{i})
      str = lower(varargin{i});
      j = strmatch(str,methods);
      k = strmatch(str,directions);
      if ~isempty(j)
         Method = methods{j(1)};
         if strcmp(Method,'marr-hildreth')
            warning('''Marr-Hildreth'' is an obsolete syntax, use ''LoG''
instead.');
         end
      elseif ~isempty(k)
         Direction = directions{k(1)};
      else
         error(['Invalid input string: ''' varargin{i} '''.']);
      end
   else
      nonstr = [nonstr i];
   end
end

% Now get the rest of the arguments

switch Method

case {'prewitt','sobel','roberts'}
   threshSpecified = 0;  % Threshold is not yet specified
   for i = nonstr
      if prod(size(varargin{i}))<=1 & ~threshSpecified % Scalar or empty
         Thresh = varargin{i};
         threshSpecified = 1;
      elseif prod(size(varargin{i}))==2  % The dreaded K vector
         warning(['BW = EDGE(... , K) is an obsolete syntax. '...
           'Use BW = EDGE(... , DIRECTION), where DIRECTION is a string.']);
         K=varargin{i};
      else
         error('Invalid input arguments');
      end
   end

case 'canny'
   Sigma = 1.0;          % Default Std dev of gaussian for canny
   threshSpecified = 0;  % Threshold is not yet specified
   for i = nonstr
      if prod(size(varargin{i}))==2 & ~threshSpecified
         Thresh = varargin{i};
         threshSpecified = 1;
      elseif prod(size(varargin{i}))==1
         if ~threshSpecified
            Thresh = varargin{i};
            threshSpecified = 1;
         else
            Sigma = varargin{i};
         end
      elseif isempty(varargin{i}) & ~threshSpecified
         % Thresh = [];
         threshSpecified = 1;
      else
         error('Invalid input arguments');
      end
   end

case 'log'
   threshSpecified = 0;  % Threshold is not yet specified
   for i = nonstr
      if prod(size(varargin{i}))<=1  % Scalar or empty
         if ~threshSpecified
            Thresh = varargin{i};
            threshSpecified = 1;
         else
            Sigma = varargin{i};
         end
      else
         error('Invalid input arguments');
      end
   end

case 'zerocross'
   threshSpecified = 0;  % Threshold is not yet specified
   for i = nonstr
      if prod(size(varargin{i}))<=1 & ~threshSpecified % Scalar or empty
         Thresh = varargin{i};
         threshSpecified = 1;
      elseif prod(size(varargin{i})) > 1 % The filter for zerocross
         H = varargin{i};
      else
         error('Invalid input arguments');
      end
   end

case 'marr-hildreth'
   for i = nonstr
      if prod(size(varargin{i}))<=1  % Scalar or empty
         Thresh = varargin{i};
      elseif prod(size(varargin{i}))==2  % The dreaded K vector
         warning('The [kx ky] direction factor has no effect for
''Marr-Hildreth''.');
      elseif prod(size(varargin{i})) > 2 % The filter for zerocross
         H = varargin{i};
      else
         error('Invalid input arguments');
      end
   end

otherwise
   error('Invalid input arguments');
end

if Sigma<=0
   error('Sigma must be positive');
end

switch Direction
case 'both',
   kx = K(1); ky = K(2);
case 'horizontal',
   kx = 0; ky = 1; % Directionality factor
case 'vertical',
   kx = 1; ky = 0; % Directionality factor
otherwise
   error('Unrecognized direction string');
end


if isrgb(I)
   error('RGB images are not supported. Call RGB2GRAY first.');
end

_______________________________________________________________________

this is the code matlab use to detect edge in the image of any format

you can copy and paste it in your new M-file
then you can change it

there are many methods to detect edge in matlab,such as Sobel method,
Prewitt Method,Roberts Method,LoG(Laplacian of Gaussian) Method, Zero-cross
Method,Canny Method.
in Arthur Lee's reply you get some filter to detect diagonal edge
then you can change corresponding part of the code above

you must set a threshold,if the output of the filtering is over the
threshold then there exist diagnoal dege!

andy
2003.12.30

>
> Hi,  Andy,
>
> How to change the code? Can you give more details?
>
> Another questions is how about I just need to test the presence of edges,
> ie., I don't need to actually take the edge-detection filtering step, all
I
> want is a logical value: 1-- a diagonal edge exists; 0 -- no diagonal edge
> exists...
>
> Is there any simplest/fastest way of doing this?
>
> Thanks a lot,
>
> -Walala
>
>


0
Reply ANDY 12/30/2003 4:27:08 AM

"walala" <mizhael@yahoo.com> wrote in message news:<bspp72$769$1@mozo.cc.purdue.edu>...
> "Arthur Lee" <zlli@nlpr.ia.ac.cn> wrote in message
> news:781280fe.0312290043.75844de8@posting.google.com...
> > Maybe you can try Prewitt or Sobel masks for detecting diagonal edges.
> > This is the simplest method.
> > Prewitt:
> >  0  1 1
> > -1  0 1
> > -1 -1 0
> >
> > -1 -1 0
> > -1  0 1
> >  0  1 1
> > Sobel:
> >  0  1 2
> > -1  0 1
> > -2 -1 0
> >
> > -2 -1 0
> > -1  0 1
> >  0  1 2
> >
> > Moreover, If you can know the orientation of lines before processing,
> > the gabor filtering should be a good choice. Although it's relatively
> > complicated than conventional masks.
> 
> Hi,  Arthur,
> 
Well, you can refer to a Digital Image Processing book, and read the
information on Hough transform or Radon transform, which can do what
you want in some degree. But from a standview of application you can't
just get one pass to achieve what you want unless some considerations
are included. So maybe you need segmentation of your image, and do
line linking, and so on.

> The filters you've given helped a lot. But how about I just need to test the
> presence of edges, ie., I don't need to actually take the edge-detection
> filtering step, all I want is a logical value: 1-- a diagonal edge exists;
> 0 -- no diagonal edge exists...
> 
> Is there any simplest/fastest way of doing this?
> 
> Thanks a lot,
> 
> -Walala
0
Reply zlli 12/30/2003 12:01:15 PM

7 Replies
203 Views

(page loaded in 0.151 seconds)

Similiar Articles:













7/27/2012 5:23:31 PM


Reply: