Having trouble multiplying a four channel image by a matrix

I have a camera which has a four color sensor. I have debayered some raw images and stored as four channel tiff files using libraw (yes, I know it can convert to a 3 channel tiff if a color space is specified but I have reasons for wanting to do this manually. The reasons why are not important here).

I have been trying to use gmic to take this 4 channel tif and multiply it by the Camera2RGB matrix


This matrix as the name implies takes the four color channel raw data and consolidates it into three channel raw RGB format. However I cannot seem to get the expected result using the mmul command, and mix_rgb only allows 3x3 matrices it seems. Is what I am wanting to do possible with gmic?

Yes, definitely, with command mix_channels.

mmul does a matrix product, but by considering that all your image is a single matrix (right-hand operand of the matrix multiplication), which is not your case here.

I am still getting errors with that command.

I also tried with the transpose of the matrix and tried a few things with the syntax since following the manual’s indicated syntax did not work.

gmic -input DSC00254-raw.tif +mix_channels (1.6374,0.0672,-0.0009,-0.2528;0.8224,-0.3551,-0.0035,-0.5306;1.4153,-0.3811,0.641,-0.0593)

gmic -input DSC00254-raw.tif +mix_channels (1.6374,-0.2528,-0.0035;-0.3811,0.0672,0.8224;-0.5306,0.641,-0.0009;-0.3551,1.4153,-0.0593)

Unexpected token ‘)’ in expression or statement.
+ CategoryInfo : ParserError: (: ) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEndParenthesisInExpression

gmic -input DSC00254-raw.tif +mix_channels ‘(1.6374,0.0672,-0.0009,-0.2528;0.8224,-0.3551,-0.0035,-0.5306;1.4153,-0.3811,0.641,-0.0593)’

gmic -input DSC00254-raw.tif +mix_channels ‘(1.6374,-0.2528,-0.0035;-0.3811,0.0672,0.8224;-0.5306,0.641,-0.0009;-0.3551,1.4153,-0.0593)’

[gmic] *** Error in ./mix_channels/*repeat/local/ *** Command ‘mmul’: [instance(3,4,1,1,00000000073441e0,non-shared)] gmic::operator(): Invalid multiplication of instance by specified
matrix (8083560,4,1,1,0000000012d5a040).

gmic -input DSC00254-raw.tif +mix_channels 1.6374,0.0672,-0.0009,-0.2528;0.8224,-0.3551,-0.0035,-0.5306;1.4153,-0.3811,0.641,-0.0593

gmic -input DSC00254-raw.tif +mix_channels 1.6374,-0.2528,-0.0035;-0.3811,0.0672,0.8224;-0.5306,0.641,-0.0009;-0.3551,1.4153,-0.0593

[gmic] *** Error in ./mix_channels/*if/ *** Command ‘input’: File ‘1.6374’, format does not take any input options (options ‘-0.2528,-0.0035’ specified).

Seemingly your command interpreter interferes with gmic script language!
Try gmic run “…”!

Besides that write your matrix with commas and semicolon. Look into
gmic h mix_channels

@bkv this is typical from the problems you’ll get when trying to mix bash and G’MIC syntax in the same line.
You should try writing this exact command in a G’MIC command file instead, otherwise, you’ll have to add some double-quotes or backslashes to escape the character that may be interpreted in bash (like ,, ;, ) and so on…).

I’m actually using powershell in windows, but I imagine the same risk can occur. If powershell and bash are prone to this kind of error, is there some other CLI that can be downloaded which lacks the issue?

It’s not really an error so much as respecting the rules for shell scripting. Shells are designed to process command lines with space-separated parameters, so surrounding parameters in quotes is how you keep them from interpreting your g’mic commands as separate parameters.

Putting the g’mic commands in a file and telling g’mic to execute that file is the best way to separate the strictures of the shell and g’mic parsers…

The advice given works. Thanks for your help everyone!

Make sure your matrix has the correct number of elements. If your image has 4 channels, the argument to mix_channels must have 4 in a row.


As for syntax, in this case, PS requires double quotes while CMD doesn’t. I typically use CMD because it is simpler and therefore would have less conflicts with G’MIC.

PS ..> gmic sp tiger r 100%,100%,100%,4 mix_channels "(1.6374,-0.2528,-0.0035,-0.3811;0.0672,0.8224,-0.5306,0.641;-0.0009,-0.3551,1.4153,-0.0593)"