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

CraftAR Image recognition - Translating matchBoundingBox to points in screen

我正在使用来自 Catchoom CraftAR and working with the example available on Github 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

我是不是漏掉了什么?

问题是我使用预览框来转换屏幕上的点。但是通过边界框的点与预览视图无关,它们与 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% 正确,但对我来说效果很好。