We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Pixels[] doubt. (Read 896 times)
Pixels[] doubt.
Jul 1st, 2009, 6:18am
 
Hi, I wondered if when working with video for example, you can work with the pixels[] function using it in this way:

pixels[x,y]= .....

instead of using it in the normal way:

pixels[x] = .....

I feel much more comfortable for certain things working in a 2 dimensional array instead of a 1 dimensional one. I mean, it's like more intuitive when u need to work comparing pixels that are near to each other and such things.

Thanx in advance Smiley

Re: Pixels[] doubt.
Reply #1 - Jul 1st, 2009, 7:20am
 
No, you cannot, not with the syntax you show.
But you can treat the pixels array as a bi-dimensional array with a simple computation:
pixels[x + width * y]
or
img.pixels[x + img.width * y]
the idea is to add width as many times as the row (minus one) we are in.
Re: Pixels[] doubt.
Reply #2 - Jul 1st, 2009, 7:47am
 
Ok, thanx, I feared I would have to do it that way xD I dont like it Sad but if it's the only way... Ill have to do it like that ^^. Thanx!!!
Re: Pixels[] doubt.
Reply #3 - Jul 1st, 2009, 7:51am
 
What's not to like?  It works just fine.  If you really wanted to you could write a function that takes x and y parameters and returns the pixel value, but I don't see much point in that...
Re: Pixels[] doubt.
Reply #4 - Jul 1st, 2009, 7:54am
 
I know, I know it works fine, it's just about working in a more "intuitive way" maybe it is cause Im used to Matlab / C++... who knows ^^ but nvm Tongue This way it's fine too.
Re: Pixels[] doubt.
Reply #5 - Jul 1st, 2009, 10:34pm
 
I think you may be looking for the

get(x,y)
and
set(x,y,color)

methods?

I mean, pixels[,]= is 10 characters, set(,,) is 7. Seems good to me.
Re: Pixels[] doubt.
Reply #6 - Jul 1st, 2009, 11:48pm
 
That's not necessarily the answer...  That gets the corresponding x,y coordinates from the display:

Get a section of the display window by specifying an additional width and height parameter.

Note that get doesn't allow you to specify an image to get colour values from.  So what if the pixels you want are in a loaded image but not in the display (e.g. when the image is used as a heightmap for a 3d terrain)?  Also note:

Getting the color of a single pixel with get(x, y) is easy, but not as fast as grabbing the data directly from pixels[]. The equivalent statement to "get(x, y)" using pixels[] is "pixels[y*width+x]".
Re: Pixels[] doubt.
Reply #7 - Jul 2nd, 2009, 1:50am
 
PImage has a get() method too.

And well, perhaps OP wants to trade speed for comfort... The difference is probably not much unless you do lot of iterations on a large image. One can also write a simplified get() function, not doing bound checks and format check...
Re: Pixels[] doubt.
Reply #8 - Jul 2nd, 2009, 3:01am
 
Embarrassed Oops!

Yes - but the documentation is a little hidden...  I still don't see the problem with the img.pixels[x + width * y] method of addressing though; the x and y are still clearly separate and identifiable and if it's quicker then so much the better...  Though it is of course important to remain in the bounds of the pixels array!
Page Index Toggle Pages: 1