Attempting to manipulate pixels from live webcam

I am attempting to write a Processing sketch that will take each row's center pixel's color and apply that color to the entire row. However, I am having trouble with even getting the pixels to change. It seems like the sketch doesn't even go through the for-loops where I am trying to change the pixels because it doesn't print out any of the statements except the end draw at the end of draw(). I just end up with an unmanipulated feed.

Testing out my algorithm with an array of numbers, it seems like using an inner for loop isn't working like how I thought it should. The i of the outer-loop is only incremented once after the first time the inner for loop completes itself, and then it just exits the outer loop instead of starting the inner loop again. What's going on here?

import processing.video.*;

Capture feed; // webcam

int pixelCount = width * height; // total # of pixels
int center = width / 2; // value for center pixel
int widthPlus = width + 1; // value to go row-to-row

color c; // center pixel color

void setup(){

  size(displayWidth, displayHeight);

  feed = new Capture(this);
  feed.start();
}

void draw(){

  if (feed.available() == true){
    feed.read();
  }

  image(feed, 0, 0);
  feed.loadPixels(); // load pixels from webcam

  /** Use to look at each row one at a time*/
  for (int i = 0; i < pixelCount; i+=widthPlus){
    println("Outer for-loop");
    c = feed.pixels[i + center]; //get center pixel

    /** Make each pixel in row the color of 'c' */
    for (int j = i; j < width; j++){
      println("Inner for-loop");
      feed.pixels[j] = c; // set pixel to 'c'
      println(i + " - " + j);
    }
  }

  feed.updatePixels(); // update pixels from webcam
  println("end draw"); 
}
Tagged:

Answers

  • Answer ✓

    Yikes, there are lotsa bugs there! :O

    • Variables width & height are 0 before setup()!
    • If you got no feed.available(), you should outright return
    • @ #31, in [i + center], if i represents the row or y coord., you'd still need the column from j to get the pixel value!
    • @ #34, for (int j = i; apparently makes no sense. Why not starting j w/ a simple 0?
    • @ #36, pixels[j] is the column of which row???

    In order to get a color value from pixels[], in terms of (x, y) coordinates, the formula I know about is:

    pixels[y*img.width + x] or if you prefer pixels[row*img.width + col] :-B

  • Oh wow, I had no idea that widthand height were 0 before setup() so I will change those to displayWidthand displayHieght.

    While doing this, I was trying to look at the pixels as just a single array rather than looking at them as x and y because it sounded easier to work off of.. guess it was the wrong way to do it.

    So I assumed that if i represented the row I was at, pixels[i + center] would get me the color of the pixel in that row, but I guess that cannot be done until I reach the inner loop since I need both x and y to get the pixel.

    I have for (int j = i; because I figured that if j started where i was, it would still represent the row I am trying to change.. so I thought pixels[j] along with j++ would help me iterate through the row I needed to.

    Also, I definitely had no idea about that formula to get the color from a pixel but this makes everything a bit more confusing for me, in terms of going back and starting again.

Sign In or Register to comment.