phpimagemagickimagemagick-convertcolormatrix

Imagemagick convert color-matrix


I have a ColorMatrix used to filter a image in html canvas:

matrix = [
                        0.6279345635605994, 0.3202183420819367, -0.03965408211312453, 0, 9.651285835294123,
                        0.02578397704808868, 0.6441188644374771, 0.03259127616149294, 0, 7.462829176470591,
                        0.0466055556782719, -0.0851232987247891, 0.5241648018700465, 0, 5.159190588235296,
                        0, 0, 0, 1, 0
         ]

Using same matrix I try to filter the image using imagemagick convert tool. If i use a 3x3 matrix all is good, but using this matrix wont work. Please help

Command from command line :

convert test.jpg  -color-matrix  
    "0.5997023498159715 0.34553243048391263 -0.2708298674538042 0      
    47.43192855600873 -0.037703249837783157 0.8609577587992641 
    0.15059552388459913 0 -36.96841498319127 0.24113635128153335 
    -0.07441037908422492 0.44972182064877153 0 -7.562075277591283 0 0 0 1 0" 
test2.jpg

Solution

  • The color matrix you have provided is invalid. For a RGB image, there's not much reason to go past 3x3, as they map to color channels.

    red'    = 1 * red + 0 * green + 0 * blue
    green'  = 0 * red + 1 * green + 0 * blue
    blue'   = 0 * red + 0 * green + 1 * blue
    

    ImageMagick can support any form of custom matrices up to 6x6. BUT you would be responsible for prototyping the matrix size ahead of the channel arrays. e.g...

     -color-matrix '6x2: 1 0 0 0 0 1
                         0 1 0 0 0 1'
    

    Some examples (copying data from your question randomly)

    rose: -color-matrix '0.599  0.3455 -0.2708 
                         0     47.4319 -0.0377 
                         0.8609 0.1505  0' rose_3x3.png
    

    rose_3x3.png

    convert rose: -color-matrix '4x1: 0 -36.9684 0.2411 -0.07441' rose_4x1.png
    

    rose_4x1.png

    convert rose: -color-matrix '6x6: -7.5620 0       0       0       1       0
                                       0.1505 0     -36.9684  0.2411 -0.0744  0.4497 
                                      -7.5620 0       0       0       1       0 
                                       0.5997 0.3455 -0.2708  0      47.4319 -0.0377
                                       0      1       0       0.1505  0     -36.9684 
                                      -0.2708 0      47.4319 -0.0377  0.8609  0.1505' rose_6x6.png
    

    rose_6x6.png