We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi all,
I'm new to the forum and fairly new to Processing. I've scoured around and founds lots of help so far, but now I'm at a point where I'm not totally sure where to head.
I'd like to load an image, analyze each pixel's RGB value, and based on those values move particles in a certain direction.
So... a little pseudo code to explain my thoughts.
Load image, get it's pixels
Go through each pixel, separate it's RGB values
If R < 128, velocity.x = -1
If R > 128, velocity.x = 1
If G < 128, velocity.y = -1
If G > 128, velocity.y = 1
Ignore the B values, I don't really need them.
position.add(speed) to move the particle.
Initially I was going using the get() function so each particle feeds it's position, looks at the color behind it, analyzes it, and moves accordingly, but that's waaaaay to slow for hundreds of particles.
I'm thinking of loading all of the pixel values into an array (does it need to be a 2D array, for x and y values?) at the start so it's all in memory, and then each particle reads where it's at on the screen, finds the corresponding image pixel index, and just pulls the vector out, without having to re-analyze the image on every frame since it's not changing.
I've been working with vectors for a bit, but I don't have much experience with arrays. Is this a good approach? Can anyone point me in the right direction?
Answers
The image is already in an array called pixels[] and yes, it's much faster than get()
https://processing.org/reference/pixels.html
Okay, I'm getting somewhere. I decided to use a PVector array. Here's what I have for that (I got a start from a sketch I found on here a while a go, but I can't find the author for credit):
Now I think I just have to figure out how to have each particle index its position and read from the loadPixelArray to get its acceleration.
Sorry about the formatting in the post, I'm not sure what I'm doing wrong.
https://forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text
GoToLoop, thank you!
So I've tried a different approach of splitting RGB into dual arrays. It works, but since I have to convert the x and y positions into integers in order to grab the values from the arrays it's causing jitter. Or is it caused from my if/else statements? Is there a way around this?
Function int() can convert whole arrays as well:
https://Processing.org/reference/intconvert_.html
You don't need constrain(). By default red(), green() & blue() return a range of 0 to 255 already.
Actually you don't need them at all. You're already using pixels[]. And they're very slow too.
This utility function returns an array w/ the 3 RGB colors split: