Page 1 of 1

Help with MagickMorphologyImage for thinning.

Posted: 2010-06-10T16:53:12-07:00
by diziet
Hi,
A bit new here and have been using MagickWand trying to get some images ready for OCR. I have had my own hit and miss algorithm working separately getting the raw image data from MagickWand and converting it. Today I noticed MagickMorphologyImage which isn't in my build so I've built from source and am trying to get my head around how to go about doing what I want. As far as I can tell I need to use ThinningMorphology as the MorthologyMethod argument and the 'Type 10' KernelInfo mentioned here:

http://www.imagemagick.org/api/morphology.php

Can somebody clarify with a snippet how this should be done. Something like:

/* Apply Morphology */
KernelInfo l_KI=AcquireKernelBuiltIn('Type 10',-1);
MagickMorphologyImage(morph_wand,ThinningMorphology,2,&l_KI);

Etc. would be useful. Yes I know the above is completely wrong. :P Think of it as a kind of pseudo code. For what it's worth though if you do try to compile the above you get:

jpegtobw.c:116:40: warning: character constant too long for its type
jpegtobw.c: In function ‘ConvertJPEGtoBWArray’:
jpegtobw.c:116: warning: passing argument 2 of ‘AcquireKernelBuiltIn’ makes pointer from integer without a cast
/usr/include/ImageMagick/magick/morphology.h:126: note: expected ‘const struct GeometryInfo *’ but argument is of type ‘int’
jpegtobw.c:116: error: invalid initializer

Are there any pointers? Searching the web for these functions simply gives the C and Header documentation found here and on mirrors. Thanks in advance.

**EDIT**
I have something that compiles with this:

/* Apply Morphology */
MorphologyMethod l_MM=ThinningMorphology;
KernelInfoType l_KIT=FreiChenKernel;
struct _GeometryInfo l_GI;
l_GI.rho=0; l_GI.sigma=0; l_GI.xi=0; l_GI.psi=0; l_GI.chi=0;
struct KernelInfo *l_KI;
l_KI=AcquireKernelBuiltIn(l_KIT,&l_GI);
MagickMorphologyImage(morph_wand,l_MM,2,l_KI);

but it does nothing appreciable. Any pointers? Other wand functions like Quantize and the Sigma Threshold Pixel iterator example in the docs are working aok.

Diziet

Re: Help with MagickMorphologyImage for thinning.

Posted: 2010-06-10T18:28:28-07:00
by el_supremo
I've scanned the source and if I read it correctly you would set l_GI.rho to the type (0,1,...,9) and l_GI.sigma to the angle. Beyond that I doubt that I can help much :-)
CORRECTION - the type is one of 0,1,2,10,11,...,19

Pete

Re: Help with MagickMorphologyImage for thinning.

Posted: 2010-06-11T10:53:49-07:00
by diziet
Hi,
Ah thanks for the help. It's definitely a push in the right direction. My first attempt (rho=10, sigma=45) produced a lovely black image as did the second so I'm clearly passing the wrong parameters but at least I know where to put them now. I'll investigate more with the source as you did. I'll have to read the examples in your sig too.

Diziet

Re: Help with MagickMorphologyImage for thinning.

Posted: 2010-06-24T17:59:42-07:00
by anthony
The builtin kernel types can be generated from the AcquireKernelInfo(). It calls the AcquireKernelBuiltIn() after pasing the requested kernel string.

If you really want to pass numbers however, the built in types should be available as constant definations.


I wrote the core library functions at the start of the year (while on long vacation in China) with testing from CLI. I have no control of the PHP or other interfaces, though I do try to accomidate them.

Just recently I added a whole stack of new kernel definitions. FreiChen just last month!
I've barely finishing my initial documentation in IM Examples
http://www.imagemagick.org/Usage/convolve/

Actually I am not even certain the special set of FreiChen Kernels 11 to 19 are correct. I only have two references, and they don't match! It does not appear to make logical sense in terms of its directionality either.

Re: Help with MagickMorphologyImage for thinning.

Posted: 2010-06-24T18:11:13-07:00
by anthony
el_supremo wrote:I've scanned the source and if I read it correctly you would set l_GI.rho to the type (0,1,...,9) and l_GI.sigma to the angle. Beyond that I doubt that I can help much :-)
CORRECTION - the type is one of 0,1,2,10,11,...,19

Pete
Actually you were probably right them being 0 to 9. They were.

But I then needed to add some special 'pre-weights diagonal specific' versions that were more important and so moved the 1 to 9 to 11 to 19. And added a type 10, to return all 9 as a multi-kernel list.

It's all still under development, though now the documentation is done, shouldn't change any more. No guarantee though. I have more morphology additions to add. (FAST 2-pass distance function, Conditional Dilation, Voronoi and Diffusion Distance Masked varients. Etc.

As a FYI this is my morphologt ToDo list...

Code: Select all

  * Morphology / Convolve
    + Add faster 2-pass Distance method
    + Add +morphology for higher level methods, and default kernel selection
    + Rotation of any 1-d kernel by given angle  (angled linear blurs)
    + Read Kernel Definitions from a text file  '@filename'
    + Kernel from small imager(s)    '@image'  ???
    + More kernels...
       - hexagonal shaped kernel
       - octogonal shaped kernel
       - line of given length and angle
       - Calculated Larger sized Angled Deritive Kernels
       - Inverse Square falloff  convolution kernels
       - Expotental falloff convolution kernels.
       - add 'filter' and 'filter2d' convolution kernels
    + More Morphology methods...
        - True GreyScale Morphology (not simply flat kernel)
        - Feather  (Like Distance but anti-aliased shape handling)
        - Conditional Dilate  (Really a masked multi-point flood fill)
        - Final Erode   (Peaks in Distance Method)
        - Voronoi - Distance of 'matte' spreading the 'minimal distance color'
        - Diffusion - A iterative masked blur
        - Distance (MAT) Skeletion Determination (how?)
        - Neighbourhood Count
        - Game of Life Transform
    + Replace -convolve, -gaussian, -blur, -sharpen etc to use the
      new morphology funtions.  Also fixs '-bias' bug in these.
      Make automatic normalization the default?
    + Also look at  -edge and -shade, as these probably use convolve too.
Comments welcome.