Page 1 of 1

Alternative to "-morphology dilate octagon".

Posted: 2017-09-19T11:01:59-07:00
by weblife
Hello,

I am trying to find an alternative to using "-channel A -morphology dilate octagon:61" because it takes 1 minute 59 seconds to complete. I have tried using "bordercolor none -border 60x60 -background White -alpha background -channel A -blur 60x10 -level 0,0% -threshold 0" but the shape doesn't evenly size the surrounding pixels like the morphology functionality.

I should step back and explain what I am doing first. I am simulating the functional purpose of Photoshop's expand visible pixels selection and smooth it by a radius of 30. I have been able to mirror the effect almost identically with the below commands:

Code: Select all

convert a.png -fill black +opaque transparent b.png
convert b.png -channel A -morphology dilate octagon:61 c.png
convert c.png -fill black +opaque transparent d.png
convert d.png -blur 40x20 +channel e.png
convert e.png -channel A -threshold 50% f.png
Could anyone help explain to me where to start looking to reproduce that step faster or how I may produce it?

I am using ImageMagick 7.0.7-3 Q16 x86_64 2017-09-18 in Term2 on OSX El Capitan.

Re: Alternative to "-morphology dilate octagon".

Posted: 2017-09-19T11:34:38-07:00
by snibgo
How about:

Code: Select all

convert b.png -alpha extract -morphology dilate:30 octagon c2.png
Not identical, but very close, and much faster.

Re: Alternative to "-morphology dilate octagon".

Posted: 2017-09-19T11:54:09-07:00
by weblife
That is much quicker @snibgo and the shape output is identical to that step, it took 24 seconds. Thank you very much!

This is a monumental improvement but still very long, I have a goal of making this happen in a second if possible. This may help me find an even faster solution once I understand why it is much quicker.

Here is the command line I'm working with now:

Code: Select all

convert source.png -alpha extract -morphology dilate:30 octagon \
    -alpha copy -fill black +opaque transparent \
    -blur 40x20 +channel \
    -channel A -threshold 50% alpha.png

Re: Alternative to "-morphology dilate octagon".

Posted: 2017-09-19T12:49:44-07:00
by snibgo
My "-alpha extract" was merely because I can more easily see the result. You probably don't need it.

What takes the time now? Morphology or blur?

Large blurs take time. I show some alternatives on Windowed mean and standard deviation. Integral images would need you to rebuild IM. Resizing down then up again might help you.

Re: Alternative to "-morphology dilate octagon".

Posted: 2017-09-19T13:04:13-07:00
by weblife
The primary leg of the time comes from the "morphology" kernel. I am wondering if creating my own kernel could speed things up?

Re: Alternative to "-morphology dilate octagon".

Posted: 2017-09-19T14:14:37-07:00
by snibgo
At the command line? I can't see why that would be any faster (or slower). As the input, kernel and output values are all either 0 or 100%, C code might be faster than the general-purpose morphology code.

Re: Alternative to "-morphology dilate octagon".

Posted: 2017-09-20T13:09:42-07:00
by weblife
I may have a better time integrating it in python. There is a intermediary step I can hook into to achieve my goal. There are a few options it seems, would there be a official library for python that can tackle this type of editing granularity?

Re: Alternative to "-morphology dilate octagon".

Posted: 2017-09-20T15:01:14-07:00
by fmw42

Re: Alternative to "-morphology dilate octagon".

Posted: 2017-09-20T15:08:43-07:00
by weblife
I am noticing convolve treats things a little differently, as a weighted sum. I would imagine this would be faster than "Dilate" because it is performing an operation on every opaque pixel.

Also didn't expect the non IM ref for the library :) Thanks Fred(@fmw42), let me see how convolve works first.

Re: Alternative to "-morphology dilate octagon".

Posted: 2017-09-20T19:50:19-07:00
by weblife
Convolve shaved a few seconds but not enough. Thank you for your help I will start looking into some of the other options discussed here.

Re: Alternative to "-morphology dilate octagon".

Posted: 2017-09-20T23:30:38-07:00
by anthony
Convolve and Simple Morpholgy are essentually applied by the same routines,
UNLESS they somehow invooke GPU's to do the function.

The simple reason -morpohology dialate:30 octagon is faster than -morpohology dialate octagon:61 is that it applies a vastly smaller kernel 30 times rather than one very very big kernel. The general effect is the same, except that intermediate values are involved. For boolean morphology the result will be the same.
https://www.imagemagick.org/Usage/morphology/#iteration

Blurring can be sped up in the same way. In fact -blur is is a LOT faster simply by splitting a Huge 2 dimensional convolution blur, into two smaller 1 dimentional blurs (same linear blure 90 degrees from each other).

I detail this quite throughly in Im Examples, Convolution, Blur
https://www.imagemagick.org/Usage/convo ... an_vs_blur

However while the result of this convolution should be the same, as weighted values are used there is some loss due to rounding errors either in the kernel, or in intermediate images.

Still LARGE blurs can be sped up by using smaller blurs, though not to as great an extent as morphology, due to the use of 1D burring kernels.

PS: the 40 in -blur 40x20 is purely used to 'limit' the size of the blurring kernel to as 40x1 array of values.
Again the exact details are looked at in the Convolution example pages.

Re: Alternative to "-morphology dilate octagon".

Posted: 2017-09-20T23:33:12-07:00
by fmw42
If large blur operations are slowing things down, you can combine blur with resize to speed things up. See http://www.fmwconcepts.com/imagemagick/ ... fast_gblur