question about what i did wrong

edited January 2017 in Library Questions

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

  • Answer ✓
    avg[square] = int(brightness(get(x + s2, y + s1 * width)));
    

    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.

  • edited January 2017

    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

    PImage img;
    
    public void setup() {
      size(640, 360, P2D);
      colorMode(HSB, 256, 256, 256);
      noLoop();
    
      img=loadImage("moonwalk.jpg");
      img.resize(width, height);
    }
    
    public void draw() {
      image(img, 0, 0);
      grid();
    }
    
    public void grid() {
      int gridSize=5;  //Must be an odd number
      int halfGrid;
    
      //Make par odd if it is not
      if (gridSize%2==0)     gridSize=gridSize+1;
    
      halfGrid=ceil(gridSize/2.0f); 
    
      //Handles trivial case when gridSize==1
      if (gridSize==1)  halfGrid=0;
    
      println("Current par:"+gridSize+" " +halfGrid);
    
      loadPixels();
      for (int x = halfGrid; x < width-halfGrid; x++) {
        for (int y = halfGrid; y < height-halfGrid; y++) {
    
          //Perform average on a grid defined by gridSize, e.g. gridSize=3
          //Position @ below corresponds to current x,y
          //
          //xxx
          //x@x
          //xxx
    
          float ccol=0;
          int ctr=0;
          for (int xx=x-halfGrid; xx<x-halfGrid+gridSize; xx++)
            for (int yy=y-halfGrid; yy<y-halfGrid+gridSize; yy++) {
              ccol += brightness(pixels[yy*width+xx]);
              ctr++;
            }
    
          if (y==height/2 && x==width/2)
            println("par "+ccol+" " +ctr);
    
          int c=get(x, y);
          ccol=ccol*1.0/ctr;            //To normalize it to color range aka. perform average
          pixels[y*width+x]=color(hue(c), saturation(c), int(ccol) );
        }
      }
      updatePixels();
    }
    
Sign In or Register to comment.