If sentence for jit.matrix

uforange's icon

Hello everyone,

I wonder if I can fill out different colors based on the grayscale values in jit.matrix.

For example,
if pixel values are less than 50, color those pixels in RGB(100, 0, 0) .
if pixel values are between 50 and 100, color those pixels in RGB(0, 200, 0).
if pixel values are greater than 100, color those pixels in RGB(0, 0, 200).

Something like this.


Thank you so much.

Max Patch
Copy patch and select New From Clipboard in Max.

testcase's icon

you can do this with jit.charmap . one thing is you will have to make the grayscale matrix into a 3 (or 4) plane matrix before sending to jit.charmap.

uforange's icon

Thank you @TESTCASE

I've looked at the jit.charmap reference, but I don't think I fully understand what it is. How can I make a if statement using jit.charmap? What I am trying to do is, in the image attached, depending on the grayscale level, I want to apply different colors. I hope this makes sense. Thank you so much.

ak's icon

This is how I would approach this task. In Max (and Jitter) it profits to think in terms of data streams. Instead of if-than think how to transform input value (here 1 pixel luminosity) to output value (1 pixel color made of 3 values RGB). hth. cheers!

Max Patch
Copy patch and select New From Clipboard in Max.

uforange's icon

Thank you @AK. I have a couple of questions.
1. It seems like jit.< @val 50 is outputing 0 or 255, does this mean that if a pixel value is less than 50, output 255, else 0?
2. If I want to make more conditions like when l < 50, 50 <= l <100, 100<=l <150, 150<=l <200, 200<=l <255, how do I approach them?
3. If I want to use all RGB channels for one condition, how can I apply them? For example, RGB(100, 50, 10) for l< 50.
Thank you so much.

ak's icon

1. yes. But then jit.* has some small print regarding how it computes values for char matrices. It’s in reference for @val attribute.

2.&3. In this case I would go with jit.pix object and codebox. Directly implements your original idea of if-then processing. Personally I don’t like this solution, because it hardcodes lots of stuff, and gen objects are very basic regarding structured data (here threshold and color values). But here it works and is quite fast to type in.

Max Patch
Copy patch and select New From Clipboard in Max.

uforange's icon

Thank you so much @AK. Sorry for asking too many questions but I am very new to this method, so I would like make everything clear.
1. What does int.r mean? .r has nothing to do with the red channel of the frame, right?
2. Can I make variables like vec(a, b, c) and input values outside the codebox?
3. In your new example, the range of the pixel values becomes 0 to 1 instead of 0 to 255, am I correct?

Thank you for your time.

Jean-Francois Charles's icon

@uforange, what you want to do is table lookup, so, @testcase's suggestion of [charmap] is great.
I would suggest you work through the good old Jitter Tutorials "What is a matrix", Tutorial 3 "Math Operations" and Tutorial 12 "Color Lookup Table". Here is a quick example of how to realize your initial idea. Changing the output means changing the color lookup table (I generate here with exprfill, but you could generate the table other ways, of course).

Max Patch
Copy patch and select New From Clipboard in Max.

ak's icon

@UFORANGE

1. .r = red, as its the first channel; probably it is not necessary at all, as all channels have the same value (monochromatic input)
2. jit.pix / gen can get 'params' from outside, but from my knowledge only single numerical values are supported
3. yep, exactly

@JEAN-FRANCOIS CHARLES
perfectly fine solution, but nicely shows something I dislike about Max: on the one hand we have a number of ready-to-use objects, that should make experimenting with ideas easy; otoh they impose a high cognitive load to do anything but very basic things (mostly copy-paste from help patcher). This is just LUT, but using "specialized" object you have to prepare matrix with special data (extra knowledge 1), and to do so you have to use some specific way to generate those data (extra knowledge 2). [jit.expr] syntax demands some effort to grasp, and (*imho*) doesn't pay off (I mean go straight to gen).

uforange's icon

@AK and @JEAN-FRANCOIS CHARLES I think I can figure this out now. Thank you for your help!