javaprocessingkinectpixelkinect-sdk

Bounding box doesn't work properly - /withkinect


I am trying to make a bounding box over the blue-colored pixels (from Kinect v1 camera, using Processing). Y-axis of bounding box works perfectly but x-axis is very off.

void display() { 

    PImage img = kinect.getDepthImage();

    float maxValue = 0;
    float minValue = kinect.width*kinect.height ;
    float maxValueX = 0;
    float maxValueY = 0;
    float minValueX = kinect.width;
    float minValueY = kinect.height;

    // Being overly cautious here
    if (depth == null || img == null) return;


    display.loadPixels();
    for (int x = 0; x < kinect.width; x++) { //goes through all the window
      for (int y = 0; y < kinect.height; y++) {

        int offset = x + y * kinect.width; 
        // Raw depth
        int rawDepth = depth[offset]; 
        int pix = x + y * display.width; //why is it y*width
        if (rawDepth < threshold) {
          // A blue color instead
          display.pixels[pix] = color(0, 0, 255); //set correct pixels to blue

          if(pix > maxValue){
             maxValue = pix;
             maxValueX = x;
             maxValueY = y;
          }

          if(pix < minValue){
            minValue = pix;
            minValueX = x;
            minValueY = y;
          }

        } else {
          display.pixels[pix] = img.pixels[offset];
        }
      }
    }
    display.updatePixels();

    image(display, 0, 0);

    rect(minValueX, minValueY, maxValueX-minValueX, maxValueY-minValueY);
}

Solution

  • You have to calculate the minimum and maximum values for each index or coordinate separately. Use the min respectively max function for this:

    maxValue = max(maxValue, pix);
    minValue = min(minValue, pix);
    
    maxValueX = max(maxValueX, x);
    minValueX = min(minValueX, x);
    
    maxValueY = max(maxValueY, y);
    minValueY = min(minValueY, y);
    

    or with an ifstatement:

    if (pix > maxValue) { maxValue = pix; }
    if (pix < minValue) { minValue = pix; }
    
    if (x > maxValueX) { maxValueX = x; }
    if (x < minValueX) { minValueX = x; }
    
    if (y > maxValueY) { maxValueY = y; }
    if (y < minValueY) { minValueY = y; }