Dithering works not with posterize

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Dithering works not with posterize

Post by fmw42 »

Try putting -dither xxxx before -posterize.
Petari
Posts: 2
Joined: 2012-11-14T07:10:40-07:00
Authentication code: 6789

Re: Dithering works not with posterize

Post by Petari »

Folks, I really want this to be solved.

ImageMagick 6.8.0-4
OS: Windows XP

Command:
convert 1056.bmp -dither Riemersma -posterize 16 o.png
Dither does nothing.

While with:
convert 1056.bmp -dither Riemersma -colors 256 o.png
dithering works.

And it works for me under Linux on my second PC. Even works if put dither after posterize in command line. But I want to use it from Windows too - and guess that I'm not the only one .
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Dithering works not with posterize

Post by glennrp »

Even

Code: Select all

convert logo: -posterize 16 o.png
does not work. The result has many more than 16 colors (the input has 255 colors and the output has 164)

Code: Select all

convert logo: -dither Riemersma -colors 16 o.png
works (output has 16 colors which is probably what you wanted).
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Dithering works not with posterize

Post by snibgo »

Posterize "n" doesn't reduce the image to "n" colours. I think it does the same as Gimp (see http://docs.gimp.org/2.8/en/gimp-tool-posterize.html), yielding 2^n colours.
snibgo's IM pages: im.snibgo.com
Petari
Posts: 2
Joined: 2012-11-14T07:10:40-07:00
Authentication code: 6789

Re: Dithering works not with posterize

Post by Petari »

glennrp wrote:Even

Code: Select all

convert logo: -posterize 16 o.png
does not work. The result has many more than 16 colors (the input has 255 colors and the output has 164)

Code: Select all

convert logo: -dither Riemersma -colors 16 o.png
works (output has 16 colors which is probably what you wanted).
Argument by posterize is number of color levels per channel - so by color images it is 16 power 3 = 4096 .
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Dithering works not with posterize

Post by glennrp »

Argument by posterize is number of color levels per channel - so by color images it is 16 power 3 = 4096
Oh, OK. That wasn't obvious from the ImageMagick documentation; I'll fix that. It is clear in the comments in magick/quantize.c though, which also seem to indicate that dithering is always on for Posterize, but as you observed, dithering seems to have no effect.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Dithering works not with posterize

Post by anthony »

posterize used to be dithered. But that changed in one release over a year ago. I complained at the time. but to no avail.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Dithering works not with posterize

Post by magick »

Dithering occurs if the number of levels cubed exceed the colormap limits of ImageMagick (256 colors for Q8 or 65535 colors for Q16). For example, -posterize 7 dithers for the Q8 version of ImageMagick because 7*7*7 is 343 which exceeds the 256 color limit. Otherwise it does not make sense to dither because the number of levels can be represented by the image (e.g. -posterize 6 is 216 colors, there is no errors so dithering is meaningless).
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Dithering works not with posterize

Post by anthony »

But it can be useful to posterize dither with just two colors! And that was the old behaviour (according to the dither setting).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
markshep
Posts: 1
Joined: 2013-03-21T03:46:51-07:00
Authentication code: 6789

Re: Dithering works not with posterize

Post by markshep »

magick wrote:Dithering occurs if the number of levels cubed exceed the colormap limits of ImageMagick (256 colors for Q8 or 65535 colors for Q16). For example, -posterize 7 dithers for the Q8 version of ImageMagick because 7*7*7 is 343 which exceeds the 256 color limit. Otherwise it does not make sense to dither because the number of levels can be represented by the image (e.g. -posterize 6 is 216 colors, there is no errors so dithering is meaningless).
For me it makes sense to always allow dithering to be used with posterization. The way I'm using posterize is to reduce an image's colorspace from 32 bits to 16 bits (RGBA8888 -> RGBA4444). This is to prepare textures in an asset pipeline for an iOS game so as to halve the memory used. I'd like to enable error dithering to make the textures look as much like the original as possible, but that doesn't seem to be possible, i.e. when using "-posterize 16 -dither Riemersma" or "-posterize 16 -dither FloydSteinberg" the dither option is ignored.

However I can use ordered dithering and posterize together, for example "-ordered-dither checks,16,16,16,16" works. So there seems to be a slight inconsistency here in that ordered dithering works with posterization, but not error dithering. I've managed to work round this by creating an RGBA4444 colormap (using "-channel" and "-fx" options), then using "-remap" and "-dither" together. So I have achieved what I want, but have had to jump through an extra hoop to get there.

Also I was wondering if there any plans to make the "-posterize" command take per-channel arguments like "-ordered-dither" can? I'm also doing some conversions to RGB565 colorspace for which this would be useful. Currently I have to use the colormap and "-remap" method just to do a straight conversion to that colorspace without dithering.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Dithering works not with posterize

Post by snibgo »

I've managed to work round this by creating an RGBA4444 colormap (using "-channel" and "-fx" options), then using "-remap" and "-dither" together.
Another way to get posterize to dither is to use unique-colors, then remap:

Code: Select all

convert in.png ^
  ( +clone -posterize 3 -unique-colors -write temp.mpr +delete ) ^
  -remap temp.mpr ^
  post.png
It wastes resources, but it works.
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: Dithering works not with posterize

Post by anthony »

You do not need the unique-colors in the colormap for dithering, that will happen automatically when it reads a colormap image. However it does make the save image a lot smaller and makes it obvious the image is a colormap and not a real image.

Also the above method also may not generate all the colors a posterizaion many need to generate a good dither. The input image may not for example cover 'blues', or extremes, like white, red, black.

The real problem with color reduction (quantization) is they generally don't generate 'outliner' colors, that an e-dither needs to prevent dither errors drifting beyond the images normal color ranges, and becomming extreme. This is the cause of dither 'speckling', when a extra (very different) color is also introduced. (a color way beyond the images normal color range).
For examples see... E-Dither Pixel Speckling
http://www.imagemagick.org/Usage/quanti ... er_speckle

As such if you do not have a complete posterization colormap (and even if you do) you can have problems with speckling, due to a lack of good 'outliner' colors just on the edge of the color zones the image needs.

As an alternative, you can also generate power of two posterization colormaps, by directly using "HALD:" images, as color maps. For example a HALD:4 is the same colors as a 16x16x16 colorcube. The IM Hald image coder was added long afetr I wrote the dither examples section, and probably should be added to that section.


PS: There has been some discussion about using srgb colormaps, converted to linrar RGB space for the disther, and then the result back to sRGB. I have no experimented with this, but it seems like a fairly good idea.


PPS: I am wanting to actually separate the 'quantization colormap' generation aspect from the 'dithering' aspect of the -color operator.
Especially as this will allow for 'user adjustments' of the colormap between the two steps, such as spreading the colors to generate 'outliners', and for ways of determining 'common colors' of an image.

NOTE: just because the quantization step generated N colors, does not mean dither actually uses all N colors! This aspect is another problem with the dithering step.

And Finally (I promise).... Dithering with fewer colors can provide opportunities or other dithering methods that is not possible when lots of colors are involved (more than 256). For example... ordered dithers using user provided colormaps, (which requires generating 'psuedo-colors' to do the ordered dither threshold maps. This should make ordered dithers even better!
See... Arbitrary-palette positional dithering algorithm
http://bisqwit.iki.fi/story/howto/dither/jy/
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: Dithering works not with posterize

Post by snibgo »

anthony wrote:You do not need the unique-colors in the colormap for dithering, ...
True. I generally create one colormap for a large number of images, and a smaller map saves a lot of time re-reading it.
anthony wrote:PS: There has been some discussion about using srgb colormaps, converted to linrar RGB space for the disther, and then the result back to sRGB. I have no experimented with this, but it seems like a fairly good idea.
For photos and video, I've experimented with posterisation etc, with and without dithering, in various colorspaces: RGB, sRGB, CIELab, OHTA, HSL and so on. I don't consistently prefer one colorspace over another; it all depends on the image. Modifying the image very slightly can make a big difference to which colorspace I prefer.

However, I have found a close correlation between my preferred colorspace and the RMSE difference between the original and posterized version. The closer the images are (i.e. the lower RMSE value), the more I prefer the colorspace.

Some examples on http://www.youtube.com/watch?feature=pl ... YwhjiUsVsM
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: Dithering works not with posterize

Post by anthony »

very interesting video... could use some description and maybe sub-titles defining the effect being using in each scene.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply