G'MIC math evaluator now deals with vectors (and matrices, and custom functions!)

Do you mean from within the math evaluator? If so:

gmic eval "M=[1,2,1,2,4,2,1,2,1];ext('(',vtos(M),')')"

Thanks. It is partially OK, but I need a square image 3x3, not an one row of 9 pixels.

A few ways to do that using resize with memory content mode (the -1):

# if size is known in advance
gmic eval "M=[1,2,1,2,4,2,1,2,1];ext('(',vtos(M),') r. 3,3,1,1,-1');"

# or outside the evaluator
gmic eval "M=[1,2,1,2,4,2,1,2,1];ext('(',vtos(M),')')"  r. 3,3,1,1,-1

# if size must be calculated
gmic eval "M=[1,2,1,2,4,2,1,2,1];W=sqrt(size(M));ext('(',vtos(M),') r. ',vtos([W,W]),',1,1,-1');"

And so on. Some other commands if you need to re-order afterwards: transpose unroll permute

Edit: and I forgot to say hello - welcome to the forum (and to G’MIC) @helour !

1 Like

Thank you for welcoming :slight_smile:

I’ve created this:

  _M=[1,2,1,2,4,2,1,2,1]
  ...
  size={sqrt(size($_M))}
  {ext('(',vtos($_M),')')} #  <- How to properly insert image from expression "(1,2,1,2,4,2,1,2,1)"?
  resize[-1] $size,$size,1,1,-1

but the error has appeared:

[gmic]-1.// Set global variable '_M=[1,2,1,2,4,2,1,2,1]'.
[gmic]-1.// Set local variable 'size=3'.
[gmic]-1.//*ext/ Input image at position 1, with values (1,2,1,2,4,2,1,2,1) (1 image 9x1x1x1).
[gmic]-2.// *** Error *** Command 'input': Invalid argument 'nan'.

In the G’MIC reference manual, under “Input Data Items”, https://gmic.eu/reference.shtml#section5, there appears this item:

(v1,v2,…): Insert a new image from specified prescribed values. Value separator inside parentheses can be , (column separator), ; (row separator), / (slice separator) or ^ (channel separator). For instance, expression (1,2,3;4,5,6;7,8,9) creates a 3x3 matrix (scalar image), with values running from 1 to 9.

1 Like

I’m wondering if a broader view would help, please forgive statements about which you already know.

There are primarily two ways to use matrix algebra in g’mic. First, you can treat an image as a matrix and use the various “native” commands with that (here using run to avoid command line escaping issues):

gmic run "(1,2,0;3,1,0;4,0,1) invert transpose (1;2;3) mmul"

That way you immediately get to visualise the output as an image. The second way is within the “math evaluator”, which is in a way a language within the g’mic language:

gmic eval "M=[1,2,0,3,1,0,4,0,1];M=inv(M);M=transp(M,3);R=M*[1,2,3];echo(vtos(R))"

Mixing the two can get a little tricky if you aren’t very familiar with g’mic string/macro handling. Anyway, if you really do want to mix both methods this is how you would:

test_matrix :
  M=[1,2,1,2,4,2,1,2,1] # an unquoted string, not a vector
  size={sqrt(size($M))} # the string $M is now evaluated as a vector
  ({$M}) # string -> vector -> comma sep. values -> input an image
  resize[-1] $size,$size,1,1,-1 # resize it
2 Likes
({$M})

Big Thanks. That’s exactly what I need ( string → vector → comma sep. values ). I want/need to separate the mathematical calculation(s) from “image processing”.

1 Like