Page 1 of 1
Algorithm for '-edge' Operator
Posted: 2010-10-22T12:43:22-07:00
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.
Re: Algorithm for '-edge' Operator
Posted: 2010-10-22T13:00:28-07:00
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().
Re: Algorithm for '-edge' Operator
Posted: 2010-10-22T13:56:30-07:00
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
Re: Algorithm for '-edge' Operator
Posted: 2010-10-22T14:57:04-07:00
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!
Re: Algorithm for '-edge' Operator
Posted: 2010-10-22T16:48:07-07:00
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);
}
Re: Algorithm for '-edge' Operator
Posted: 2010-10-25T11:16:08-07:00
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
Re: Algorithm for '-edge' Operator
Posted: 2010-10-25T11:37:08-07:00
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.
Re: Algorithm for '-edge' Operator
Posted: 2010-10-25T16:31:11-07:00
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.
Re: Algorithm for '-edge' Operator
Posted: 2010-10-28T06:52:00-07:00
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.