如何将帧坐标映射到视觉中的叠加

Raf*_*l T 13 camera android drawing screen-rotation android-vision

我觉得这个问题已经解决了很多次,但我无法弄清楚.我基本上遵循了关于移动视觉的这个小教程并完成了它.之后我尝试从ColorBlob开始检测对象并绘制边框.

想法是从框架的中间开始(有目的地将对象保持在相机的中间)并通过其颜色检测该对象的边缘.只要我以横向模式(Frame.ROTATION_0)保持手机,它就可以正常工作.一旦我处于纵向模式(Frame.Rotation_90),边界Rect就会被绘制成旋转,因此具有更多高度的对象将被绘制为具有更多宽度,并且还有点偏离.

文档说,探测器总是提供给COORDS未旋转直立框架,所以我怎么来计算相对于它的旋转边框COORDS?我认为这不重要,但这是我如何找到颜色Rect

public Rect getBounds(Frame frame){
  int w = frame.getMetadata().getWidth();
  int h = frame.getMetadata().getHeight();
  int scale = 50;
  int scaleX = w / scale;
  int scaleY = h / scale;
  int midX = w / 2;
  int midY = h / 2;
  float ratio = 10.0
  Rect mBoundary = new Rect();
  float[] hsv = new float[3];
  Bitmap bmp = frame.getBitmap();
  int px = bmp.getPixel(midX, midY);
  Color.colorToHSV(px, hsv);
  Log.d(TAG, "detect: mid hsv: " + hsv[0] + ", " + hsv[1] + ", " + hsv[2]);
  float hue = hsv[0];
  float nhue;
  int x, y;
  for (x = midX + scaleX; x < w; x+=scaleX){
      px = bmp.getPixel(x, midY);
      Color.colorToHSV(px, hsv);
      nhue = hsv[0];
      if (nhue <= (hue + ratio) && nhue >= (hue - ratio)){
          mBoundary.right = x
      } else {
          break;
      }
  }

  for (x = midX - scaleX; x >= 0; x-= scaleX){
      px = bmp.getPixel(x, midY);
      Color.colorToHSV(px, hsv);
      nhue = hsv[0];
      if (nhue <= (hue + ratio) && nhue >= (hue - ratio)){
          mBoundary.left = x
      } else {
          break;
      }
  }

  for (y = midY + scaleY; y < h; y+=scaleY){
      px = bmp.getPixel(midX, y);
      Color.colorToHSV(px, hsv);
      nhue = hsv[0];
      if (nhue <= (hue + ratio) && nhue >= (hue - ratio)){
          mBoundary.bottom = y;
      } else {
          break;
      }
  }

  for (y = midY - scaleY; y >= 0; y-=scaleY){
      px = bmp.getPixel(midX, y);
      Color.colorToHSV(px, hsv);
      nhue = hsv[0];
      if (nhue <= (hue + ratio) && nhue >= (hue - ratio)){
          mBoundary.top = y
      } else {
          break;
      }
  }
  return mBoundary;
}
Run Code Online (Sandbox Code Playgroud)

然后我简单地在画布上的GraphicOverlay.Graphic绘制方法中绘制它.我已经使用了transformX/YGraphic上的方法和思想,它也会考虑旋转.我也使用样本提供的CameraSourceCameraSourcePreview类.