We are about to switch to a new forum software. Until then we have removed the registration on this forum.
my Code:
import processing.video.*;
Capture video;
void setup() {
size(640, 480);
pixelDensity(1);
frameRate(60);
String[] cameras = Capture.list();
printArray(cameras);
video = new Capture(this, cameras[1]);
video.start();
}
void captureEvent(Capture video) {
video.read();
}
void draw() {
video.loadPixels();
image(video, 0, 0);
grid();
}
void grid() {
int square = 0;
int nr = 20;
int[] avg = new int[width * height];
background(255, 0, 0);
loadPixels();
if (width % nr == 0 & height % nr == 0) {
for (int s1 = 0; s1 < height; s1 += height/nr) {
for (int s2 = 0; s2 < width; s2 += width/nr) {
for (int x = 0; x < width/nr; x++) {
for (int y = 0; y < height/nr; y++) {
square = (x + y * width) + s1 * width + s2;
avg[square] = int(brightness(get(x + s2, y + s1 * width)));
pixels[square] = color(avg[square]);
}
}
}
}
updatePixels();
}
}
END
when i run this code it only shows me a black screen. I already tried the same code with just slightly changed for() loops
void grid() {
int[] avg = new int[width*height];
loadPixels();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
avg[x+y*width] = int(brightness(get(x,y)));
pixels[x+y*width] = color(avg[x+y*width]);
}
}
updatePixels();
}
and it worked 100% correct, so I assume the problem must be in the for() loops or how i used the variables s1 and s2, but I just can not find the mistake. Thanks in advance.
~sLucas
Answers
why s1 * width? looks like you are trying to convert a position into an index into the pixel array. but get() doesn't use one.
yes you are right thank you very much now i can keep on coding my code :D#
I just figured out that now all brightness values are 255. but that doesnt make any sense, because my camera records different colors then just white. I would whish to have the same results with version 1 and version 2 and since version 2 gives me the exactly same camera view version 1 must contain another mistake.
What exactly are you trying to do?
Make sure you set the proper color mode when extracting brightness. Check the following links:
https://processing.org/reference/colorMode_.html
https://processing.org/reference/brightness_.html
If you want to calculate the average of each pixel based on neighboring pixels then use this code below. The average is calculated on a window of size gridSize*gridSize, where the center of the window is the pixel of interest. Parameter gridsize needs to be an odd number. The trivial case of gridSize=1 (aka no averaging) is also handled by the code below.
Kf