offset column in 5x5 color-matrix is ignored?

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
adamj
Posts: 3
Joined: 2011-09-16T11:36:04-07:00
Authentication code: 8675308

offset column in 5x5 color-matrix is ignored?

Post by adamj »

I've been struggling for a few hours with imagemagick's color-matrix feature, and it seems to me the offset column of the matrix is being completely ignored.

As a simple test, try this:

* Grab the wizard image from http://imagemagick.com/image/wizard.jpg

* Setup an identity transformation using the convert command:

Code: Select all

convert -color-matrix "1 0 0 0 0    0 1 0 0 0    0 0 1 0 0    0 0 0 1 0    0 0 0 0 1" wizard.jpg colored_wizard.jpg
This doesn't change the image, as expected.

* Now try setting some RGB, offsets:

Code: Select all

convert -color-matrix "1 0 0 0 1    0 1 0 0 0.5    0 0 1 0 -0.75    0 0 0 1 0    0 0 0 0 1" wizard.jpg colored_wizard.jpg
No matter what I do, it still applies the identity transformation. I can scale the RGB values fine using the other columns, but the last column seems to be ignored.

I'm not certain I set up the matrix correctly, so I tried all sorts of other things including:
* Using floating point values (i.e. 0.0 and 1.0 instead of 0 and 1) for all values
* Using the range 0-255
* Using the bottom row instead of the last column
* Performing the tranformation in a Ruby script with the rmagick gem
* Inserting commas into the matrix's number list, as I've seen in the docs and some examples on this forum

No matter what I do, the offset values are ignored.

Then I tried with the 6x6 color matrix, and it works the way I expect. Unfortunately I really need to work in the RGB color space, so I need the 5x5 matrix to work. In case imagemagick was assuming CMYK internally I've tried using -colorspace RGB with the convert command, but nothing helps.

I've tried this with ImageMagick 6.7.2-0 2011-09-16 Q16, and also some earlier 6.6 version, with the same results.

Can anyone help? Am I doing it wrong, or is this feature buggy? If it's a bug, are there any workarounds or will I need to wait for a fix?

Thanks in advance for any help,
Adam
adamj
Posts: 3
Joined: 2011-09-16T11:36:04-07:00
Authentication code: 8675308

Re: offset column in 5x5 color-matrix is ignored?

Post by adamj »

So I guess it's not that big of a deal to calculate my color matrix in CMYK colorspace and use the 6x6 filter. I will do that for now... it seems to be working correctly.

But it still looks to me like there's a bug in the 5x5 color-matrix.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: offset column in 5x5 color-matrix is ignored?

Post by Bonzo »

Not 100% sure of what you want as I have not done this before but for a start you should read your image in first.

This example from the Imagemagick site seems to have an effect ( http://www.imagemagick.org/script/comma ... lor-matrix ):

Code: Select all

convert wizard.jpg -color-matrix "1.5 0.0 0.0 0.0, 0.0, -0.157 0.0 1.5 0.0 0.0, 0.0, -0.157 0.0 0.0 1.5 0.0, 0.0, -0.157 0.0 0.0 0.0 1.0, 0.0,  0.0 0.0 0.0 0.0 0.0, 1.0,  0.0 0.0 0.0 0.0 0.0, 0.0,  1.0" colored_wizard.jpg
I have just downloaded and installed 6.7.2 Q16
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: offset column in 5x5 color-matrix is ignored?

Post by fmw42 »

-color-matrix matrix

apply color correction to the image.
This option permits saturation changes, hue rotation, luminance to alpha, and various other effects. Although variable-sized transformation matrices can be used, typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA (or RGBA with offsets). The matrix is similar to those used by Adobe Flash except offsets are in column 6 rather than 5 (in support of CMYKA images) and offsets are normalized (divide Flash offset by 255).

As an example, to add contrast to an image with offsets, try this command:

convert kittens.jpg -color-matrix \
" 1.5 0.0 0.0 0.0, 0.0, -0.157 \
0.0 1.5 0.0 0.0, 0.0, -0.157 \
0.0 0.0 1.5 0.0, 0.0, -0.157 \
0.0 0.0 0.0 1.0, 0.0, 0.0 \
0.0 0.0 0.0 0.0, 1.0, 0.0 \
0.0 0.0 0.0 0.0, 0.0, 1.0" kittens.png

The docs must be read carefully. I think you need to provide all 6x6 entries if you want to use offsets. The 4th and 5th rows and columns are placeholders for the alpha channel (RGBA) or the black channel and alpha channel (CMYKA). The 6th row and column are for the offsets

From the docs above:
"one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA (or RGBA with offsets)"

NOTE the highlight in blue for RGBA with offsets.

If you want to avoid the offsets and alpha you can just use 3x3.

This will be a no-change:

convert rose: -color-matrix \
" \
1 0 0 0, 0, 0 \
0 1 0 0, 0, 0 \
0 0 1 0, 0, 0 \
0 0 0 1, 0, 0 \
0 0 0 0, 1, 0 \
0 0 0 0, 0, 1 \
" \
rose_tmp1.png


NOTE the 4th and 5th columns need commas.

This will offset the red channel

convert rose: -color-matrix \
" \
1 0 0 0, 0, 0.5 \
0 1 0 0, 0, 0 \
0 0 1 0, 0, 0 \
0 0 0 1, 0, 0 \
0 0 0 0, 1, 0 \
0 0 0 0, 0, 1 \
" \
rose_tmp2.png


this is a no-change:


convert rose: -color-matrix \
" \
1 0 0 \
0 1 0 \
0 0 1 \
" \
rose_tmp3.png



This attenuates the red:

convert rose: -color-matrix \
" \
0.5 0 0 \
0 1 0 \
0 0 1 \
" \
rose_tmp4.png
adamj
Posts: 3
Joined: 2011-09-16T11:36:04-07:00
Authentication code: 8675308

Re: offset column in 5x5 color-matrix is ignored?

Post by adamj »

fmw42 wrote:
The docs must be read carefully...

"one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA (or RGBA with offsets)"

NOTE the highlight in blue for RGBA with offsets.
Oh man! I tried to read carefully, and I stared at that probably a dozen times recently, but somehow it didn't register. Thanks so much for pointing that out so clearly!

I understand color matrix filters fairly well, and have worked with them a good bit in Flash, but I find it really difficult translating to ImageMagick. If the 6x6 matrix is used for offset purposes, then what are the extra rows and columns? It's really not clear. It would probably help a lot of people if the docs could explain this scenario a bit better (using offsets with RGBA).

Here's what I'm guessing is going on:
* ImageMagick knows when the image is in an RGB colorspace, and will use an RGBA transformation regardless of the size of the color matrix.
* The first 4 rows calculate the resulting R,G,B,A values, respectively.
* The last 2 rows are (probably?) ignored (but you should fill them in with the identity matrix values for good measure)
* The 5th column is ignored??? (again fill in with the identify matrix values for good measure). This seems so weird....
* The 6th column is the offsets

Also, regarding the question of whether the commas are required, I'm not using any commas, and it seems to work fine. No one seems to know what's up with this, so again, it would be nice if the docs could be updated to explain (even just something like commas are optional or were required in older versions of ImageMagick, or whatever)

Thanks again, I think I've got this all sorted out now.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: offset column in 5x5 color-matrix is ignored?

Post by fmw42 »

As I wrote above:

The 4th and 5th rows and columns are placeholders for the alpha channel (RGBA) or the black channel and alpha channel (CMYKA). The 6th row and column are for the offsets

Also note the commas after the 4th and 5th columns.

I think the commas are needed to distinguish the alpha (for RGBA) and black and alpha (for CMYKA). That is why you have to have a 6x6 matrix to use offsets as they must be separated from a 4x4 or 5x5 matrix for RGBA and CMYKA.

That is my best understanding.

I got bus errors when I tried leaving out the commas for a 6x6 matrix, though I may have mistyped something else. So am not 100% sure about the commas.

IM 6.7.2.6 Q16 Mac OSX Tiger
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: offset column in 5x5 color-matrix is ignored?

Post by anthony »

I never did get to check out the color matrix changes when they were introduced (I was in china, mostly offline, and writing morphology at the time). Otherwise I would probably have tied the matrix to the channel setting, so you can select what channels the matrix rows and columns are applied to, or at least provided better control of the matrix actions.

For example I never did like having to specify the extra 'useless' row in a matrix.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply