CraftAR图像识别 - 将matchBoundingBox转换为屏幕中的点

时间:2016-09-29 08:59:28

标签: ios iphone augmented-reality image-recognition bounding-box

我正在使用来自Catchoom CraftAR的设备图片识别并使用Gi​​thub https://github.com/Catchoom/craftar-example-ios-on-device-image-recognition上提供的示例。

图像识别有效,我想使用matchBoundingBox在所有4个角上绘制一些正方形。不知怎的,我正在做的计算不起作用,我将它们基于这篇文章:

http://support.catchoom.com/customer/portal/articles/1886553-obtain-the-bounding-boxes-of-the-results-of-image-recognition

方形视图被添加到扫描覆盖图中,这就是我计算添加4个视图的点的方法:

CraftARSearchResult *bestResult = [results objectAtIndex:0];
BoundingBox *box = bestResult.matchBoundingBox;

float w = self._preview.frame.size.width;
float h = self._preview.frame.size.height;

CGPoint tr = CGPointMake(w * box.topRightX , h * box.topRightY);
CGPoint tl = CGPointMake(w * box.topLeftX, h * box.topLeftY);
CGPoint br = CGPointMake(w * box.bottomRightX, h * box.bottomRightY);
CGPoint bl = CGPointMake(w * box.bottomLeftX, h * box.bottomLeftY);

x位置看起来非常接近,但y位置完全关闭,看起来像镜像。

我正在测试iOS 10 iPhone 6s

我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

问题在于我正在使用预览框架来翻译屏幕中的点。但是边界框带来的点与预览视图无关,它们与VideoFrame相关(正如catchoom.com的支持人员指出的那样)。 VideoFrame大小由capturePreset设置,只接受两个值AVCaptureSessionPreset1280x720AVCaptureSessionPreset640x480。默认值为AVCaptureSessionPreset1280x720

所以在我的情况下,我必须使用1280x720的大小进行计算,然后从这些坐标转换为预览视图大小的坐标。

所以最终看起来像这样:

let box = bestResult.matchBoundingBox

let wVideoFrame:CGFloat = 1080.0;
let hVideoFrame:CGFloat = 720.0;

let wRelativePreview = wVideoFrame/CGFloat(preview.frame.size.height)
let hRelativePreview = wVideoFrame/CGFloat(preview.frame.size.width)

var tl = CGPoint(x: wVideoFrame * CGFloat(box.topLeftX),y: hVideoFrame * CGFloat(box.topLeftY));
var tr = CGPoint(x: wVideoFrame * CGFloat(box.topRightX) ,y: hVideoFrame * CGFloat(box.topRightY));
var br = CGPoint(x: wVideoFrame * CGFloat(box.bottomRightX),y: hVideoFrame * CGFloat(box.bottomRightY));
var bl = CGPoint(x: wVideoFrame * CGFloat(box.bottomLeftX),y: hVideoFrame * CGFloat(box.bottomLeftY));

tl = CGPoint(x: tl.x/wRelativePreview, y: tl.y/hRelativePreview)
tr = CGPoint(x: tr.x/wRelativePreview, y: tr.y/hRelativePreview)
br = CGPoint(x: br.x/wRelativePreview, y: br.y/hRelativePreview)
bl = CGPoint(x: bl.x/wRelativePreview, y: bl.y/hRelativePreview) 

// 4 square visualize top-left, top.right, bottom-left and bottom-right points
var fr = vTL.frame;
fr.origin = tl;
vTL.frame = fr;

fr.origin = tr;
vTR.frame = fr;

fr.origin = br;
vBR.frame = fr;

fr.origin = bl;
vBL.frame = fr;

现在屏幕上的点看起来很不错,但是他们看起来有些旋转。所以我将视图旋转了90度:

// overlay is the container of the 3 squares to visualize the points in screen
overlay.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI/2.0))

请注意,这不是来自catchoom支持的官方回复,这可能不是100%正确,但它对我很有用。