0

我在使用设备图像识别Catchoom CraftAR并使用Gi​​thub上的示例https://github.com/Catchoom/craftar-example-ios-on-device-image-recognitionCraftAR图像识别 - 将matchBoundingBox转换为屏幕中的点

图像识别的作品,我想用matchBoundingBox在所有的四个角上画一些正方形。不知怎的,我做的计算是不工作,我已经根据他们这篇文章:

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

我错过了什么吗?

回答

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%正确的,但它的工作对我来说相当不错。