ios - ARKit 2 - 作物识别图像
问题描述
所以,我的目标是:
查找已知图像
guard let referenceImages = ARReferenceImage.referenceImages(inGroupNamed: "AR Resources", bundle: nil) else { return } let configuration = ARWorldTrackingConfiguration() configuration.detectionImages = referenceImages
.
从场景视图中拍摄快照
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) { let image = sceneView.snapshot() // or else self.sceneView.session.currentFrame?.capturedImage
我需要从 SceneView 中提取图像。
这是我的代码,我使用的是 renderer.projectPoint 方法。
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)
但似乎这种方法取决于手机的位置。
解决方案
就我而言,我通过对每个参考点执行此操作来解决它。
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))
推荐阅读
- python - 使用 python 调用函数给出未定义的错误
- python - 使用 NBA 统计数据在 Pandas 中创建三双列
- android - Android 在拖动时为 Fragment 设置动画并调整其大小
- php - 在上传文件的代码中找不到文件索引
- c# - 无法将 gameObject 数组的所有元素输出到统一控制台
- r - 如何在 R 中使用宏变量
- python - 有人可以举一个“operator.index()”的例子吗?
- php - Nginx 上的 Symfony4 和 WordPress 兼容性问题
- spring - 如何使用 Spring Boot Starter Data Cassandra 同时连接多个 cassandra 集群
- linux - nginx安装问题,不知道怎么解决