Forums > Jitter

Better option for posterization?

February 10, 2012 | 4:51 pm

I have a patch (below) that works for basic posterization in Jitter. It uses jit.op to accomplish the same as:

if $i1 > 50 && $i1 < 100

It divides the matrix by ranges of brightness, then colors them using jit.op as well. Not too clean compared to similar operations in Processing, etc. I tried jit.expr but there’s not much documentation for if/then statements that I could find.

In any case, it seems there must be a cleaner or more processing-efficient way to do this? JavaScript in Jitter is too slow for per-pixel processing (at least how I’ve accomplished it using for loops). GL shaders?

Any thoughts would be great – not really a pressing issue but I’d be interested to hear some suggestions. Thanks all!

– Pasted Max Patch, click to expand. –


dtr
February 10, 2012 | 10:37 pm

[ jit.expr @inputs 3 @expr (in[0]>in[1])&&(in[0]< =in[2]) ]

this can easily be done with shaders but you’ll only benefit from that if you do all the operations like the adding and crossfade in slabs on the GPU too.


February 11, 2012 | 1:27 am

Thanks Dieter – there’s no way to do much in the way of the ‘then’ side of ‘if/then’ using jit.expr, right?

Agreed about the shaders at this point. This is the first step in homemade video compression algorithms. I’m interested in per-pixel manipulation based on neighboring pixels. For example, if the neighboring pixel is different enough to the current pixel by a certain threshold, set the neighbor’s color to that of the current pixel. This would be done across each frame in all four directions (up, down, left, right).

ie (in pseudo-code):
if absVal(pxLeft-pxCurrent) < threshold then pxLeft == 100,0,0

Here’s an example created in Processing:

Thoughts? Possible in real-time?



dtr
February 11, 2012 | 3:39 pm

sounds like a job for jit.gen/jit.gl.gen… have a look at the samples here: http://cycling74.com/forums/topic.php?id=37876



dtr
February 11, 2012 | 3:43 pm

btw, there are ways to package if/then constructs mathematically in expr. if a>b then c else d translates to:

@expr (in[0]>in[1])*in[3]+(in[0]< =in[1])*in[4]

expr takes a bit of work to get into but once you do it’s quite powerful.


February 11, 2012 | 4:43 pm

Yep, Gen’s looking like an interesting possibility (and looks much simpler than shaders).

Let’s see if I can parse your example:
1. Test if the values from matrix 0 are greater than a certain value, which returns white (if not, then black)
2. Multiply the result by a fixed number; black (0) * anything will still stay black, while the white (1) will now be the new value
3. Same happens using a < = comparison

What I can’t figure out is the + operation – is that the same as && in this case?

Suggestions for a place to find good documentation on if/then statements? Seems to be a good tutorial here on basic math functions, but not much for logical comparisons.

(and thanks for the replies!)


February 11, 2012 | 5:08 pm

i’m still learning gen, but maybe this will get you started:

– Pasted Max Patch, click to expand. –


dtr
February 11, 2012 | 5:50 pm

> What I can’t figure out is the + operation – is that the same as && in this case?

no, * would equal && (AND)
+ equals ⎜⎜ (OR)

in our case (in[0]>in[1])*in[3]+(in[0]< =in[1])*in[4]
the comparisons either yield 0*in[3]+1*in[4] or 1*in[3]+0*in[4]
which equates to in[4] or in[3] (= our then and else values)


February 12, 2012 | 6:27 pm

Fantastic – took me a while to figure out what you had written! :)

As I understand it, your expression returns values above or below a certain value, rather than if it falls in a range (ie: 0.5 – 0.7). If a second test value is added (in[2]) wouldn’t that give the range?

"(in[0] > in[1]) * in[3] + (in[0] < = in[2]) * in[4]"

It appears that the expression could then be written more traditionally as:

if (in[0] > in[1]) then output in[3]
else if (in[0] < = in[2]) then output in[4]

Everything works great, but I get three colors, not two… Looks like the third color is a combination of the two.

----------begin_max5_patcher----------
1586.3oc6Z0tjhhCE825SAE+Z+nWq7IJas6Vy9.rOA8zUWnlVyLH3.os6cmZ
d22PBHfBZ.Qk1xt5BEBPN2St2at4D+9vA1SCemEaa86VOZMXv2GNXf5RIWXP
54CrW489LeuX0sYGvdKb5WreP2jf8tPc40VwLw+3Ih3um01KgAh.uULU6+cD
2yOqk0dhYK4AKdNhMSn6bJkNB7fkKH4HD3p9XxHf0SoOCet58H66eiRydQAu
thG3yDJngyuX3qhrqBKflX9+oPCLoWzWVemh+cMSiC6uvEOuRaGa6aEdYQor
RJsHekbe1FVTLOLnPOMvN2pTVi5fCQ8AYBH2ljT8B+vYekoLMP1Emydw72P3
ZVPU2c1gx2IOXcDKlEH7DoftXu58pu34R7zjQU19KdyX09vUNjOvdQDedXPB
HJ8jIWNq6dzBpbBRNlia0cD3sthGVDF5O0KZCOlO0mUZLP5D6EvkCj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0MS4JVbr2B
1dinQLu4sZzTuWvk1jJmpGLgiqzzQcxFB2N1PDtXgOy9XyEpOh1drRqqIoxg
0YFp867otaC++1JlHJzBia0XKtP4RTmCEnBwmww11WkTMrRR382DiVEtgyjN
s.YrKv5SaB8y2sq1PTo+rHvGbJe34bQzUtugmTQ4sY9eSpu18izJSZyz5lPB
iuYIAj4quumu79CjBg895HqOwCjkKGaQs9j57Oa+S7fGAOY8WVxOgO8yV+Rx
WvOY8qVos7G+YxUPYMQd5y11sS+IEupmWhBObZmJYY5YOqiBKk2sLMquq50o
b+9+NIKpEcMBVW5mP79ZUuqN0H4hxj0o3NQUshaRoZ5ypwAwTnBMApk17fii
UG810QT6cGFje1ohUjg7ZCvJViNnBqN4mbhPcrgHE0.jhJ3AfgpEy0AN.Tfg
PEaNTIPRAVkHykNti3UpogVDyQ6IBIhIPZGbe1wD1TLAubXBYJlPWLL4ZBj.
WzgNLvTLc4F5vPSwzkanynICPNWzwNnQo8QMCStpIklnmLc62OUjZTdT7kk8
vmA1amB6F9ig+O34dicE
-----------end_max5_patcher-----------


dtr
February 12, 2012 | 7:09 pm

almost there ;)

"(in[0] > in[1]) * in[2] + (in[0] < = in[1]) * in[3]" equals:

if (in[0]>in[1]) then output in[2]
else output in[3]

so if you want:
if (in[0] > in[1]) then output in[3]
else if (in[0] < = in[2]) then output in[4]

you need:
(in[0] > in[1]) * in[3] + (in[0] < = in[1]) * (in[0]<=in[2]) * in[3]

this function will output 0 if in[0]< =in[1] and in[0]>in[2]


February 12, 2012 | 10:05 pm

Whew, ok – took about 2 hours to get my head around this. It’s a matter of thinking in terms of a series of 0/1 statements – so weird and arcane compared to modern if/then/else series. In any case, for others banging their heads against this I built an example patch. Thanks Dieter for your patience! (and time to take a look at Gen)

– Pasted Max Patch, click to expand. –


dtr
February 13, 2012 | 10:37 am

there you have it! arcane but efficient ;)


Viewing 12 posts - 1 through 12 (of 12 total)