Alternative to "-morphology dilate octagon".

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
weblife
Posts: 21
Joined: 2016-01-06T22:23:33-07:00
Authentication code: 1151

Alternative to "-morphology dilate octagon".

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Alternative to "-morphology dilate octagon".

Post 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.
snibgo's IM pages: im.snibgo.com
weblife
Posts: 21
Joined: 2016-01-06T22:23:33-07:00
Authentication code: 1151

Re: Alternative to "-morphology dilate octagon".

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Alternative to "-morphology dilate octagon".

Post 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.
snibgo's IM pages: im.snibgo.com
weblife
Posts: 21
Joined: 2016-01-06T22:23:33-07:00
Authentication code: 1151

Re: Alternative to "-morphology dilate octagon".

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Alternative to "-morphology dilate octagon".

Post 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.
snibgo's IM pages: im.snibgo.com
weblife
Posts: 21
Joined: 2016-01-06T22:23:33-07:00
Authentication code: 1151

Re: Alternative to "-morphology dilate octagon".

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Alternative to "-morphology dilate octagon".

Post by fmw42 »

weblife
Posts: 21
Joined: 2016-01-06T22:23:33-07:00
Authentication code: 1151

Re: Alternative to "-morphology dilate octagon".

Post 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.
weblife
Posts: 21
Joined: 2016-01-06T22:23:33-07:00
Authentication code: 1151

Re: Alternative to "-morphology dilate octagon".

Post 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.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Alternative to "-morphology dilate octagon".

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Alternative to "-morphology dilate octagon".

Post 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
Post Reply