边界框无法正常工作 - /withkinect

Bounding box doesn't work properly - /withkinect

我正在尝试在蓝色像素上制作一个边界框(来自 Kinect v1 相机,使用 Processing)。边界框的 Y 轴工作正常,但 x 轴非常偏离。

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);
}

您必须分别计算每个索引或坐标的最小值和最大值。为此使用 min respectively max 函数:

maxValue = max(maxValue, pix);
minValue = min(minValue, pix);

maxValueX = max(maxValueX, x);
minValueX = min(minValueX, x);

maxValueY = max(maxValueY, y);
minValueY = min(minValueY, y);

或使用 if 语句:

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; }