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.
IndexProgramming Questions & HelpSyntax Questions › Pixels keyword confusion
Page Index Toggle Pages: 1
Pixels keyword confusion (Read 933 times)
Pixels keyword confusion
Jun 17th, 2009, 4:08pm
 
Hi - I've been working through some examples with video capture and am having some trouble understanding how the pixels keyword works and any help would be much appreciated.

My understanding is:
- It's a 1D array.
- If your screen is 200x100, the pixels array will be 20000 in size, effectively pixels[0 to 19999]
- pixels[0 to 199] will be the first 'row' of pixels
- pixels[200 to 399] will the next 'row' etc

I've setup a window, with video capture and am trying to just display a vertical sliver of the capture.

Here's what I've got so far, but my updating of pixels seems to be wrong.

import processing.video.*;
Capture cam;

void setup() {
 size(600, 240);
 background(0);
 cam = new Capture(this, 300, 240);
}

void draw() {
 if (cam.available() == true) {
   cam.read();
   cam.loadPixels();
   image(cam, 150, 0);
   loadPixels();
   for(int i=0; i < width; i++){
     for(int j=0; j < height; j++){
       if(i < 290 || i > 310){
        pixels[i*j] = color(0,0,0);
       }
     }
   }
   updatePixels();
 }
}

Any help on this would be great!


Re: Pixels keyword confusion
Reply #1 - Jun 18th, 2009, 12:51am
 

Try this it should work

Code:

  for(int i=0; i < width; i++){
    for(int j=0; j < height; j++){
      if(i < 290 || i > 310){
       pixels[i+j*width] = color(0,0,0);
      }
    }



Re: Pixels keyword confusion
Reply #2 - Jun 18th, 2009, 3:21pm
 
Thanks, this worked! (just trying to get my head around the logic of it now Smiley
Re: Pixels keyword confusion
Reply #3 - Jun 19th, 2009, 12:46am
 
its not that hard. if you think of the images as a matrix of pixels. like a chessboard...

0 1 2 3 4
1 1 2 3 4
2 2 4 6 8
3 3 6 9 12
4 4 8 12 16

the red numbers become "your pixel id" so like you see, if you just multiply i and j the id appears more then once cause  the pixel in line 3 and row 2 has the id 6 but in line 2 and row 3 also...
Re: Pixels keyword confusion
Reply #4 - Jun 19th, 2009, 4:30am
 
Ah ok - i*j ends up going back on itself.

Whereas, the correct way is using j*width as a kind of offset, to jump to whatever 'row' you'd be on. I confused myself by having width as the outer loop and height as in the inner one.

Many thanks for the explanation!
pixels[] contents
Reply #5 - Jun 19th, 2009, 1:48pm
 
(Meep... should read all of the thread before making corrections... the following might be useful to someone just the same)

y\x x0 x1 x2 x3
y0 00 01 02 03
y1 04 05 06 07
y2 08 09 10 11
y3 12 13 14 15

Pixel coordinates are 0-based. The top left corner of the window is (0,0) and the bottom right corner is (width-1,height-1). The pixels[] array is also 0-based. The first entry is the top left pixel, the next entry is one pixel to the right and so on, with width pixels forming each row.

There are width*height pixels in total, so the last pixels[] entry is pixels[width*height-1].

This can also be thought of as:

pixels[(width-1) + (height-1)*width]

where:
(width-1) is the highest x-value,
(height-1) is the highest y-value.

Generally, the pixel at position (x,y) is accessed by:
pixels[x + y*width]

-spxl
Capture.crop()
Reply #6 - Jun 19th, 2009, 4:53pm
 
Hey, have a look at the crop() method of the Capture class...

I think it does exactly what you want without having to draw the entire capture frame then black out the parts you don't want. :o)

Link: http://processing.org/reference/libraries/video/Capture_crop_.html

-spxl
Page Index Toggle Pages: 1