possible bug -color-matrix IM 6.7.5.6 Q16

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

possible bug -color-matrix IM 6.7.5.6 Q16

Post by fmw42 »

IM 6.7.5.6 Q16 Mac OSX Snow Leopard

I believe that -color-matrix should reproduce the following -fx expression. They produce the same results when using values between 0 and 1, but different results when I have values greater than 1 and negative values. I think that the fault is in -color-matrix.

Input:
Image


This works fine -- the results are the same:

rf=0.3
gf=0.6
bf=0.1
convert zelda3.png -separate \
-fx "$rf*u[0]+$gf*u[1]+$bf*u[2]" \
zelda3_gray_mix1a.png

Image


rf=0.3
gf=0.6
bf=0.1
convert zelda3.png -color-matrix "\
$rf 0 0 \
0 $gf 0 \
0 0 $bf \
" -separate -evaluate-sequence add \
zelda3_gray_mix1b.png

Image


But these produce different results and I believe that -color-matrix is the wrong one.

rf=-0.40
gf=1.44
bf=-0.04
convert zelda3.png -separate \
-fx "$rf*u[0]+$gf*u[1]+$bf*u[2]" \
zelda3_gray_mix2a.png

Image


rf=-0.40
gf=1.44
bf=-0.04
convert zelda3.png -color-matrix "\
$rf 0 0 \
0 $gf 0 \
0 0 $bf \
" -separate -evaluate-sequence add \
zelda3_gray_mix2b.png

Image


P.S. I noticed that there was a similar old bug report that might be related. See viewtopic.php?f=3&t=16787
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: possible bug -color-matrix IM 6.7.5.6 Q16

Post by magick »

Compile with HDRI and you will get the same result for both commands. Without HDRI you get a difference due to clamping the result between 0 and QuantumRange. The -fx option adds R+G+B then clamps. With -color-matrix, it clamps R, G, and B and only then does it sum R+G+B then clamps the sum.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: possible bug -color-matrix IM 6.7.5.6 Q16

Post by fmw42 »

magick wrote:Compile with HDRI and you will get the same result for both commands. Without HDRI you get a difference due to clamping the result between 0 and QuantumRange. The -fx option adds R+G+B then clamps. With -color-matrix, it clamps R, G, and B and only then does it sum R+G+B then clamps the sum.

Thanks, I understand. I worked out another way that works without HDRI.


rf=-0.40
gf=1.44
bf=-0.04
convert zelda3.png -color-matrix "\
$rf $gf $bf \
$rf $gf $bf \
$rf $gf $bf \
" \
zelda3_gray_mix3.png
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: possible bug -color-matrix IM 6.7.5.6 Q16

Post by anthony »

The last method given is the correct one for grayscaling with various weights.

See http://www.imagemagick.org/Usage/color_mods/#grayscale

HDRI is not needed for either FX or Matrix operators.
It is needed for some of the other 'piecemeal' techniques (like separate image adds), to get exactly the same results, especially if weightings are outside the 0..1 range. Basically you are hitting quantum rounding and clipping errors.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply