Page 1 of 1

possible bug -color-matrix IM 6.7.5.6 Q16

Posted: 2012-02-18T17:28:43-07:00
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

Re: possible bug -color-matrix IM 6.7.5.6 Q16

Posted: 2012-02-19T18:30:46-07:00
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.

Re: possible bug -color-matrix IM 6.7.5.6 Q16

Posted: 2012-02-19T19:40:23-07:00
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

Re: possible bug -color-matrix IM 6.7.5.6 Q16

Posted: 2012-02-20T21:39:27-07:00
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.