We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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");
}
Answers
Yikes, there are lotsa bugs there! :O
feed.available()
, you should outrightreturn
[i + center]
, if i represents the row or y coord., you'd still need the column from j to get the pixel value!for (int j = i;
apparently makes no sense. Why not starting j w/ a simple 0?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 preferpixels[row*img.width + col]
:-BOh wow, I had no idea that
width
andheight
were 0 beforesetup()
so I will change those todisplayWidth
anddisplayHieght
.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 bothx
andy
to get the pixel.I have
for (int j = i;
because I figured that ifj
started wherei
was, it would still represent the row I am trying to change.. so I thoughtpixels[j]
along withj++
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.