我觉得这个问题已经解决了很多次,但我无法弄清楚。我基本上是按照这个关于移动视觉的小教程完成的。之后,我尝试从 ColorBlob 开始自己检测对象并绘制其边框。
这个想法是从画面的中间开始(故意将对象保持在相机的中间)并通过其颜色检测该对象的边缘。只要我将手机保持在横向模式(Frame.ROTATION_0),它就可以工作。一旦我处于纵向模式(Frame.Rotation_90),边界 Rect 就会被旋转绘制,因此具有更高高度的对象会以更大的宽度被绘制,并且也有点偏离。
文档说检测器总是将坐标传递给未旋转的直立框架,那么我应该如何计算相对于其旋转的边界矩形坐标?我认为这并不重要,但这是我找到颜色的方法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;
}
然后我简单地在画布上的GraphicOverlay.Graphic的 draw 方法中绘制它。我已经使用了transformX/Y
Graphic 上的方法,并认为它也可以解释旋转。我还使用示例中提供的CameraSource
andCameraSourcePreview
类。