color-matrix

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
PaulAThompson
Posts: 26
Joined: 2012-06-23T10:50:36-07:00
Authentication code: 13

color-matrix

Post by PaulAThompson »

I am looking for a clear explanation of the color-matrix option. I am trying to convert GIMP-like choices into ImageMagick commands. In GIMP, you can modify the levels of a color. This seems difficult to do in ImageMagick. I would like to use color-matrix, but the explanations are not clear or easy to follow. Is there a set of instructions available for this option?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: color-matrix

Post by snibgo »

snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: color-matrix

Post by fmw42 »

PaulAThompson
Posts: 26
Joined: 2012-06-23T10:50:36-07:00
Authentication code: 13

Re: color-matrix

Post by PaulAThompson »

Those help some, but not completely. In the designstacks.net/color-matrix tutorial, the matrix is 4x5 (r x c). In the imagemagic "help" (which is not clear or specific), the matrix is 6x6 with 2 commas. In one discussion, fmw says"

I think snibgo is correct, in IM the offsets are in the range 0 to 1.

The IM matrix uses 6x6 to permit c,m,y,k,alpha,offset for a CMYKA image

For RGBA image if you need the offset, then you can use 5x5. If no offset, then 4x4 and if no alpha, then 3x3 are permitted.

OK, it's either 6x6, 5x5, 4x4, or 3x3.

I gotta admit I'm still not clear on when it is either of these 4. Is there also a discussion somewhere about offsets? or about the A channel?

I'm experimenting with the color matrix. It sort of does what I need, sort of
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: color-matrix

Post by fmw42 »

Offsets are like adding to the value, whereas the other entries are like a multiplier. Alpha is always in the range 0 to 1 in ImageMagick. Offsets are also expressed as fractions typically in the range -1 to 1.

1=R or C
2=G or Y
3=B or M
4=K
5=A
6=offsets

I believe IM fills channels not used after the last supplied channel with 0. So

If you want to just change an RGB image without offsets, then use 3x3.

If you want to change an RGBA image without offsets use 5x5, since you need the placeholder for the K channel even though it is not used.

If you want to change an RGB image with or without alpha, but with offsets, use 6x6, since you still need the placeholder for the alpha and K channels.

Any use of offsets, means 6x6. Any use of alpha without offsets, means 5x5.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: color-matrix

Post by snibgo »

As Fred says.

You should give "-color-matrix" a square matrix of numbers: 1x1, 2x2, ... 6x6. If you give less than 6x6, IM internally pads to that size, with zeros in all entries except for ones on the diagonal.

So you can give any square size you want. IM will assume "sensible" values for the ones you haven't supplied. If you need offsets, you must supply 6x6 values.

I think about is this way: pixel values are normalised to 0.0 to 1.0 (but HDRI can exceed that range). Each output channel is one row of the matrix, multiplied by the input pixel. An HDRI example (Windows BAT syntax):

Code: Select all

%IMDEV%convert xc:rgb(10%%,-20%%,30%%) -color-matrix ^
1.1,0,0,0,0,0,^
1,0,0,0,0,1.2,^
0,2,0,0,0,0,^
1,0,0,0,0,0,^
1,0,0,0,0,0,^
1,0,0,0,0,0^
  txt:

0,0: (4.72446e+08,5.58346e+09,1.28849e+09)  #1C28F5C2FFFFFFFF4CCCCCCD  srgb(11%,
130%,-40%)
Output red is input red * 1.1. IM calculates: 0.10 * 1.1 = 0.11, or 11%.

Output green is input red * 1, plus 1.2. IM calculates: 0.10 * 1 + 1.2 = 1.3, or 130%.

Output blue is input green * 2. IM calculates: -0.20 * 2 = -0.40, or -40%.
snibgo's IM pages: im.snibgo.com
Post Reply