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%正確的,但它的工作對我來說相當不錯。

相關問題