Morphology distance 'scale'

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Morphology distance 'scale'

Post by snibgo »

(a) The documentation http://www.imagemagick.org/Usage/morphology/ mentions "+depth". Could this be documented on the options page, please?

(b) Could the morphology distance 'scale' value please accept percent (%), meaning (as we might expect) the value is a percent of quantum?

The default value of 100 gives (I assume) very different values for Q8, Q16 or Q32. I would hope 50% would give very similar results.

For example (Windows script):

Code: Select all

convert images\f604_blackwhite_both.png ^
  -channel RGB -evaluate set 0%% ^
  -set option:showkernel 1 ^
  -channel alpha -negate ^
  -verbose ^
  -morphology Distance:-1 Euclidean:4,50%% ^
  +verbose ^
  x.png
IM currently ignores the percent sign and uses "50".

Thanks.
snibgo's IM pages: im.snibgo.com
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Morphology distance 'scale'

Post by anthony »

snibgo wrote:(a) The documentation http://www.imagemagick.org/Usage/morphology/ mentions "+depth". Could this be documented on the options page, please?
+depth is to do with reading GIF images, and is not really specifically part of Morphology.
(b) Could the morphology distance 'scale' value please accept percent (%), meaning (as we might expect) the value is a percent of quantum?
I seriously thought about this, and it could be implemented. But really you generally what that distance scaling to be a fixed known size for your script.
The default value of 100 gives (I assume) very different values for Q8, Q16 or Q32. I would hope 50% would give very similar results.
the value 100 is a fixed default. It does not change with the Q level of IM. If you actually did set depth to 50% of the Quantum Range then your distance results will clip only two pixels in from the edge!!!! That is not particularly good.

For example (Windows script):

Code: Select all

convert images\f604_blackwhite_both.png ^
  -channel RGB -evaluate set 0%% ^
  -set option:showkernel 1 ^
  -channel alpha -negate ^
  -verbose ^
  -morphology Distance:-1 Euclidean:4,50%% ^
  +verbose ^
  x.png
IM currently ignores the percent sign and uses "50".[/quote]

ASIDE: what is all that -evaluate handling in your example for? set all colors to black? Why?
And you negate the alpha channel! Why? I can't see any sense in this.

No mention is make on how the resulting image is used AFTER the Distance Gradient is generated.
how you plan to use the results afterward is VERY important.

For example if you are only trying to feather the edges using distance, than a -1 'infinite' iteration count is overkill, as you are only interested in the distance of pixels close to the edge.

For example a 'linear gradient around the edge 5 pixels deep can be done with one one iteration, and an appropriate 'level' to expand the edge gradient.

Code: Select all

   convert shape.png \
               -morphology Distance:1 Euclidean:5,50 \
               -level 0,250 -clamp \
               result.png
Note I used no iteration count, I only want the distance morphology method to be run once and once only as I don't care about any pixels more than 5 pixels from the edge.

The -level means that any pixel that has as a value of 250 (5 pixels distant from edge) should become 'white' (the maximum Quantum Range), the -clamp in the above clips anything that goes beyond white to white (only important for HDRI versions of IM and not needed otherwise).

What I have not documented very well is handling alpha shapes with 'semi-transparent' pixels.
Before getting calculating distance in these images you want to give these semi-transparent pixels
an appropriate 'distance' from the edge. In this case use...

Code: Select all

   convert alpha_shape.png -channel A \
               +level 0,50 -white-threshold 50 \
               -morphology Distance Euclidean:5,50 \
               -level 0,250 -clamp \
               result.png
This reduces the size of semi-transparent pixels into a 0 to 50 value range, while leaving fully-opaque pixels white.

This is not thoughly tested and assumes all operations treat the transparency channel as alpha values and not opaque values.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Morphology distance 'scale'

Post by anthony »

After some debugging use this instead for a 5 pixel feather...

Code: Select all

   convert alpha_shape.png \
               \( +clone -alpha extract \
                  +level 0,50 -white-threshold 49 \
                  -virtual-pixel black \
                  -morphology Distance Euclidean:5,50 \
                  -level 0,250 -clamp \
                  -sigmoidal-contrast 3,50% \
                \) -compose CopyOpacity -composite \
               result.png
Note I could have applied the above directly against the alpha channel except
that some operators do not treat the transparency channel as 'alpha values' but
as 'opacity values' (specifically -white-threshold).

As such I extacted the alpha channel and handle it as a separate greyscale
image, and merge it back into the final image-

The +level line converts semi-transparent pixels to an appropriate 'distance
value' for its transparency level.

The -virtual-pixel ensures the image is regarded as surrounded by
transparency, especially any part of the shape that touches the edge of the
image.

The the final level make any pixel more than 5 pixels away from the edge
fully-opaque. The -clamp is only needed if a HDRI version of IM is used,
otherwise it is simply a no-op.

The linear feathering was tempered slightly by using a small
-sigmoidal-contrast operation as a final step on the mask.


the above has been added to IM examples (raw notes) in
http://www.imagemagick.org/Usage/morpho ... feathering
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Morphology distance 'scale'

Post by snibgo »

+depth is to do with reading GIF images, and is not really specifically part of Morphology.
My point was that +depth seems to be a useful option, so mentioning it on the options page seems like a good idea. The only mention I can find of it is on the Morphology page.
I seriously thought about this, and it could be implemented. But really you generally what [want] that distance scaling to be a fixed known size for your script.
That's fine, if the script knows what quantum is being used. The documentation says:
But in the previous example the 'largest distance' value assigned was '1700' which would overflow users using a Q8 version of ImageMagick (See Quality, in memory bit depth). A IM Q8, only allows color values to reach a maximum value of 255. As such using a small scale such as '10' or '20' will work better for users using the IM Q8 compile time variant.
So a sensible value will vary according to quantum. Allowing a percentage would avoid having to mess around in the script. (True, "50%" was a bad example. A better example would be "0.15%".)

My code was intended only as a sample script to show the location of the scale I was talking about.

My sample input image is http://www.snibgo.org/images/f604_blackwhite_both.png

It is part of a movie frame, with black and white line-pairs up to about 6 pixels thick from edge-detection, leaving most of the image fully transparent. My interest is in creating a movie that slowly creates the lines. Morphology might be able to help by successive erosion, which I would then play backwards. This command was part of my experiments towards this goal.
snibgo's IM pages: im.snibgo.com
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Morphology distance 'scale'

Post by anthony »

snibgo wrote:
+depth is to do with reading GIF images, and is not really specifically part of Morphology.
My point was that +depth seems to be a useful option, so mentioning it on the options page seems like a good idea. The only mention I can find of it is on the Morphology page.
All setting options have a + version that resets the option to its defaults. This not only include -depth, but also -channel -compose -gravity -delay -dispose -page etc etc etc etc...

I talk about this almost in the very first section on 'Basics'...
Basic Usage, Settings and Operators
http://www.imagemagick.org/Usage/basics/#options

perhaps having a read of this section will let you discover more things you may have missed.

However yes you are right it should also be mentioned in the options reference guide. Though most setting options don't do so.
I seriously thought about this, and it could be implemented. But really you generally what [want] that distance scaling to be a fixed known size for your script.
That's fine, if the script knows what quantum is being used. The documentation says:
But in the previous example the 'largest distance' value assigned was '1700' which would overflow users using a Q8 version of ImageMagick (See Quality, in memory bit depth). A IM Q8, only allows color values to reach a maximum value of 255. As such using a small scale such as '10' or '20' will work better for users using the IM Q8 compile time variant.
So a sensible value will vary according to quantum. Allowing a percentage would avoid having to mess around in the script. (True, "50%" was a bad example. A better example would be "0.15%".)
And make it much more difficult to interpret and control the results. If you look at my 'feathering examples, you need much finer control.

In any case a Q8 version of IM is really only suitable for very basic image conversions and processing. When you get to the point where you are doing more extreme image processing a Q16 version of IM become essential.

However I will add percentage flag handling for the distance scaling. IM v6.6.1-6

My code was intended only as a sample script to show the location of the scale I was talking about.

My sample input image is http://www.snibgo.org/images/f604_blackwhite_both.png

It is part of a movie frame, with black and white line-pairs up to about 6 pixels thick from edge-detection, leaving most of the image fully transparent. My interest is in creating a movie that slowly creates the lines. Morphology might be able to help by successive erosion, which I would then play backwards. This command was part of my experiments towards this goal.
I see. that explains things.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Morphology distance 'scale'

Post by snibgo »

The basics page says:
Many of the [setting] options have both a '-' and a '+' style. The latter is generally used to turn off the setting, or reset it to its normal default state.
Some of the "+" options are documented on the options page. For the ones that aren't, the user has to guess that a "+" option might exist, and what it might do. If you are now saying that ALL setting options have a "+" version that ALWAYS resets the option to its defaults, then I suggest you say that in the basics page.

I agree that Q16 is better than Q8 for most purposes. The exception, in my case, is trial runs of special effects on video frames.

Many thanks for your good work.
snibgo's IM pages: im.snibgo.com
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Morphology distance 'scale'

Post by anthony »

Getting back to Morphology Distance Scale.

I have now submitted into SVN two special flags for distance scaling.
  • Adding % will make the scale a percentage of the color range.
    So 10% will let you colovr around 10 pixels from the edge before the distance gradient reaches white.
  • Continuing this line of reasoning. I also added another probably much more useful flag. '!'
    If given, the scales will be set to the color range divided by the value given. that means 10! should hit maximum (white) at 10 pixels from the edge.
Note however that both of these depend on how accurate your IM is between each 'iteration' (Q8 is not real accurate). For example doing a 500! scaling factor will not be very accurate in IM Q8, and probably will probably fail dramatically, as it can not even correctly represent a single pixel distance! A Q8 could only represent a maximum distance of 255, and then only if you set a scale of 1 using a Chebyshev or Manhatten Distance Kernel.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Morphology distance 'scale'

Post by anthony »

See an example of using this new distance scaling flag in...
IM Examples, Thumbnails, Soft Edges, (feathered edge example)
http://www.imagemagick.org/Usage/thumbnails/#soft_edges

I have yet to write up a complete 'feathering techniques' section in IM examples.

Though I did have a section on the old 'False Feathering' technique that is based on using alpha channel blurring.
Im Examples, Blurring and Sharpening Images, Feathering...
http://www.imagemagick.org/Usage/blur/#feathering

That was written a long time before morphology was added to IM (at the start of this year).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply