Algorithm for '-edge' Operator

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
alexRutherford

Algorithm for '-edge' Operator

Post by alexRutherford »

Hi

I would be really grateful if someone could tell me what algorithm is used for edge detection, or at least exactly what a value of 1.0 in the output actually means. Alternatively if someone could tell me where to find the source code in the ImgaeMagick bundle then I can have a look myself.

Any help gratefully appreciated.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Algorithm for '-edge' Operator

Post by magick »

Edge is just a standard image convolution where all elements of the operator is -1 except the center pixel. See magick/effect.c/EdgeImage().
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Algorithm for '-edge' Operator

Post by fmw42 »

Look in the ImageMagick directory under the folder magick in the code, effect.c for EdgeImage

There are other edge operators that can be implemented used by specifying your weights using -convolve or -morphology convolve.

see http://www.imagemagick.org/Usage/convolve/

also see my unix bash scripts for, laplacian, gradient, derivative, binomialedge, gaussianedge, sharpedge at the link below
alexRutherford

Re: Algorithm for '-edge' Operator

Post by alexRutherford »

Thanks so much for the guidance. I'm being stupid most likely but I can't seem to find the actual specification of the function, or effect.c. I can find effect.h at /opt/local/include/ImageMagick/magick which mentions the function but not the full specification.

Thanks again!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Algorithm for '-edge' Operator

Post by fmw42 »

download the source code package from http://www.imagemagick.org/download/www ... .html#unix

then decompress it and look in that folder (not the binary install) in the subdirectory magick for effect.c

then look in that file until you get to the section on edgeImage



Code: Select all

/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%     E d g e I m a g e                                                       %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  EdgeImage() finds edges in an image.  Radius defines the radius of the
%  convolution filter.  Use a radius of 0 and EdgeImage() selects a suitable
%  radius for you.
%
%  The format of the EdgeImage method is:
%
%      Image *EdgeImage(const Image *image,const double radius,
%        ExceptionInfo *exception)
%
%  A description of each parameter follows:
%
%    o image: the image.
%
%    o radius: the radius of the pixel neighborhood.
%
%    o exception: return any errors or warnings in this structure.
%
*/
MagickExport Image *EdgeImage(const Image *image,const double radius,
  ExceptionInfo *exception)
{
  Image
    *edge_image;

  double
    *kernel;

  register ssize_t
    i;

  size_t
    width;

  assert(image != (const Image *) NULL);
  assert(image->signature == MagickSignature);
  if (image->debug != MagickFalse)
    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  assert(exception != (ExceptionInfo *) NULL);
  assert(exception->signature == MagickSignature);
  width=GetOptimalKernelWidth1D(radius,0.5);
  kernel=(double *) AcquireQuantumMemory((size_t) width,width*sizeof(*kernel));
  if (kernel == (double *) NULL)
    ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
  for (i=0; i < (ssize_t) (width*width); i++)
    kernel[i]=(-1.0);
  kernel[i/2]=(double) (width*width-1.0);
  edge_image=ConvolveImage(image,width,kernel,exception);
  kernel=(double *) RelinquishMagickMemory(kernel);
  return(edge_image);
}
alexRutherford

Re: Algorithm for '-edge' Operator

Post by alexRutherford »

That's wonderful!

Thank you so much for your help @fmw42, I found the code and I can see the implementation of the simple LaPlacian filter.

My FINAL question is, how exactly is the pixel value calculated in the final resulting image? Presumably a +ve gradient and a -ve gradient with the same absolute value are given the same intensity? Are the edges then simply normalised so that the larger of the absolute values of the maximum and minimum gradients are 1.0?

I can't see that in this routine, and if you happen to know if and where this happens later in the code then you would really be saving my life!

Again, forever in your debt
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Algorithm for '-edge' Operator

Post by magick »

Take a look at ConvolveImage() in magick/effect.c. EdgeImage() generates the digital filter and passes the filter to ConvolveImage() to apply to each pixel in the image.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Algorithm for '-edge' Operator

Post by fmw42 »

what is the issue with your interest in -edge? you can create your own kernel and use that with -convolve or -morphology convolve. if you want a laplacian or gradient or edge sharpening, see my scripts at the link below and the list I suggested above. the -edge function is somewhat like subtracting a blurred image from the original image. I believe what Magick has said is that for example for a 3x3 filter size it is the same as a kernel of

-1 -1 -1
-1 8 -1
-1 -1 -1

properly normalized as above, which is a simple laplacian

or

-1/8 -1/8 -1/8
-1/8 1 -1/8
-1/8 -1/8 -1/8


If you can explain your interest better, perhaps we can suggest something for you.
alexRutherford

Re: Algorithm for '-edge' Operator

Post by alexRutherford »

Thanks again for your help. I have been out of town for a few days and I am just starting to look through the convolution routine.

My precise question is that I have found some edges in elevation data and I want to assign a measure of the actual change in real elevation for an edge value of say 0.25. I know what value of elevation corresponds to a pixel value of 1.0 in my original image, I just want to know what a given pixel value in the output represents.

Thanks again.
Post Reply