Morphology in MagickWand.

The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.
Post Reply
rpatelob
Posts: 62
Joined: 2017-04-17T22:17:01-07:00
Authentication code: 1151

Morphology in MagickWand.

Post by rpatelob »

What is the equivalent of below CLI into C?

Code: Select all

-morphology Distance:-1 Euclidean:5,1000
C code.

Code: Select all

  
  KernelInfo *kernel;
  const char *kernel_string = "EuclideanKernel";
  ExceptionInfo * exc = AcquireExceptionInfo();
  kernel = AcquireKernelInfo(kernel_string,exc);
  MagickMorphologyImage(wand2, DistanceMorphology, 1000, kernel);
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Morphology in MagickWand.

Post by snibgo »

Please, always state what version of IM you are using.
const char *kernel_string = "EuclideanKernel";
ExceptionInfo * exc = AcquireExceptionInfo();
kernel = AcquireKernelInfo(kernel_string,exc);
I suggest you read the documentation on AcquireKernelInfo(). The source code for that function explains the parameter. See also http://www.imagemagick.org/Usage/morphology/ for a fuller description.
snibgo's IM pages: im.snibgo.com
rpatelob
Posts: 62
Joined: 2017-04-17T22:17:01-07:00
Authentication code: 1151

Re: Morphology in MagickWand.

Post by rpatelob »

Thank you snibgo for the suggestion. And I'm using Version: ImageMagick 7.0.5-5 Q16 x86_64 2017-04-26 http://www.imagemagick.org.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Morphology in MagickWand.

Post by el_supremo »

I've replicated one of Anthony's morphology examples using MagickWand.

Code: Select all

#ifdef MORPHOLOGY_1
// https://www.imagemagick.org/discourse-server/viewtopic.php?f=6&t=31863

// Try one of Anthony's morphology examples first:
// convert man.gif   -morphology Erode Octagon  erode_man.gif
#include <windows.h>
#include <wand/magick_wand.h>
void test_wand(void)
{
	MagickWand *mw = NULL;
	KernelInfo *ki;
	char kernel[512];

	MagickWandGenesis();

	/* Create a wand */
	mw = NewMagickWand();

	/* Read the input image */
	MagickReadImage(mw,"man.gif");

	ki = AcquireKernelInfo("Octagon");
	if(ki == NULL) {
		MessageBox(NULL,"ki NULL","",MB_OK);
		if(mw) mw = DestroyMagickWand(mw);
		MagickWandTerminus();
		exit(10);
	}
	if(!MagickMorphologyImage(mw, ErodeMorphology, 1, ki)) {
		MessageBox(NULL,"Failed","",MB_OK);
	} else {
		/* write it */
		MagickWriteImage(mw,"man_erode_octagon.gif");
	}

	/* Tidy up */
	ki=DestroyKernelInfo(ki);
	if(mw) mw = DestroyMagickWand(mw);
	MagickWandTerminus();
}
#endif
I presume that "-morphology Distance:-1 Euclidean:5,1000" would require:

Code: Select all

	ki = AcquireKernelInfo("Euclidean:5,1000");
and

Code: Select all

if(!MagickMorphologyImage(mw, DistanceMorphology, -1, ki)) {
Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
rpatelob
Posts: 62
Joined: 2017-04-17T22:17:01-07:00
Authentication code: 1151

Re: Morphology in MagickWand.

Post by rpatelob »

Hello el_supremo,

Finally I got my solution this way.

Code: Select all

-morphology Distance:-1 Euclidean:5,1000

Code: Select all

  KernelInfo *kernel;
  GeometryInfo geometry;
  geometry.sigma = 1000;
  geometry.rho = 5;
  ExceptionInfo * exc = AcquireExceptionInfo();
  kernel = AcquireKernelBuiltIn(EuclideanKernel, &geometry, exc);
  MagickMorphologyImage(wand2, DistanceMorphology, 1, kernel);
Thank you for the Example. Could you please check my code? I have got the result so haven't spend so much time on it.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Morphology in MagickWand.

Post by snibgo »

If you need iterations of -1 at the command line, I expect you need -1 in the call to MagickMorphologyImage. But I haven't tested this, and "-1" probably has no effect for "Distance".

To check your code, you can test whether the code gives the same result as the command line.
snibgo's IM pages: im.snibgo.com
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Morphology in MagickWand.

Post by el_supremo »

I forgot to note that the code I wrote used IM version 6.9.7. It appears that the code you are using is IM v7 - I need to get around to upgrading!
In V7 the signature is:

Code: Select all

KernelInfo *AcquireKernelInfo(const char *kernel_string,  ExceptionInfo *exception)
but in V6 it is:

Code: Select all

KernelInfo *AcquireKernelInfo(const char *kernel_string)
Other than that, I agree with @snibgo that if the command line version needed -1, then the code should use -1 too, although it is possible that -1 and 1 would give the same result in your case.

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
Post Reply