ARKit 2 - Crop Recognizing Images

痴心易碎 提交于 2019-12-08 12:17:47

问题


So, my goal is:

  1. Find known image

    guard let referenceImages = ARReferenceImage.referenceImages(inGroupNamed: "AR Resources", bundle: nil) else { return }
    
    let configuration = ARWorldTrackingConfiguration()
    configuration.detectionImages = referenceImages
    

.

  1. take a snapshot from sceneView

     func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
    
           let image = sceneView.snapshot()  //  or else self.sceneView.session.currentFrame?.capturedImage
    
  2. I need extract the image form the SceneView.

This is my code, I'm using the renderer.projectPoint method.

  guard let imageAnchor = anchor as? ARImageAnchor else {
            return
        }
        let posx = anchorTr.x// fnode.convertPosition(node.position, to: node).x //self.positionFromTransform(node.simdTransform).x// node.simdTransform.columns.3.x
        let posy = anchorTr.y//fnode.convertPosition(node.position, to: node).y //self.positionFromTransform(node.simdTransform).y//node.simdTransform.columns.3.y
        let posz = anchorTr.z //node.simdTransform.columns.3.z


                  let width = referenceImage.physicalSize.width
        let height = referenceImage.physicalSize.height


        let topLeftPosition = SCNVector3(posx - Float(width / 2), posy - Float(height / 2), posz )
        let topRightPosition = SCNVector3(posx + Float(width / 2), posy - Float(height / 2), posz )
        let bottomLeftPosition = SCNVector3(posx - Float(width / 2), posy + Float(height / 2), posz )
        let bottomRightPosition = SCNVector3(posx + Float(width / 2), posy  + Float(height / 2), posz)


        let topLeftProjection = renderer.projectPoint(topLeftPosition)
        let topRightProjection = '''
        let bottomLeftProjection = '''
        let bottomRightProjection = '''

        let topLeft = CGPoint(x: CGFloat(topLeftProjection.x), y: CGFloat( topLeftProjection.y))
        let topRight = ''
        let bottomLeft = ''''''
        let bottomRight = '''


  let points = [topLeft, topRight, bottomRight, bottomLeft]

   self.drawPolygon(points, color:.green)

but it seems that this method depends on the position of the cell phone.


回答1:


In my case, I solved it by doing this for each of the reference points.

let transform = node.simdConvertTransform(node.simdTransform, to: node.parent!)

let x = transform.columns.3.x
let y = transform.columns.3.y
let z = transform.columns.3.z

let position = SCNVector3(x, y, z)
let projection = renderer.projectPoint(position)

let screenPoint =  CGPoint(x: CGFloat(projection.x), y: CGFloat( projection.y))


来源:https://stackoverflow.com/questions/53453203/arkit-2-crop-recognizing-images

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!