同时区分多个跟踪图像?
Distinguish between multiple tracked images at the same time?
我的应用正在跟踪一组 5 张参考图像。但是,我希望它能够区分参考图像(即图像是图像 A、图像 B 还是图像 C。跟踪我打印在明信片上的真实照片)。
目前,我的代码能够检测到图像并在其上放置一个平面(即一个简单的矩形)。但是,我的问题是,是否可以区分该应用程序检测到的是图片A还是图片B?如果可以,怎么做?
我知道有一个使用 ML 的选项,但想看看 SceneKit/ARKit 中是否有我没有考虑过的更简单的选项;特别是因为我使用的是准确的图像,而不是试图让应用程序猜测一个对象。
class ViewController: UIViewController, ARSCNViewDelegate {
@IBOutlet var sceneView: ARSCNView!
override func viewDidLoad() {
super.viewDidLoad()
sceneView.delegate = self
}
//MARK-: WHERE I CONFIGURE MY APP TO DETECT AN IMAGE
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let configuration = ARImageTrackingConfiguration()
guard let trackedImages = ARReferenceImage.referenceImages(inGroupNamed: "Photos", bundle: Bundle.main) else {
print("No images available")
return
}
configuration.trackingImages = trackedImages
configuration.maximumNumberOfTrackedImages = 7
sceneView.session.run(configuration)
}
//MARK-: WHERE I PLACE A PLANE OVER THE DETECTED IMAGE
func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
let node = SCNNode()
if let imageAnchor = anchor as? ARImageAnchor {
let plane = SCNPlane(width: imageAnchor.referenceImage.physicalSize.width, height: imageAnchor.referenceImage.physicalSize.height)
plane.firstMaterial?.diffuse.contents = UIColor(white: 1, alpha: 0.8)
let planeNode = SCNNode(geometry: plane)
planeNode.eulerAngles.x = -.pi / 2
node.addChildNode(planeNode)
}
return node
}
}
您可以使用 referenceImage
和 name
.
等实例属性轻松完成此操作
// The detected image referenced by the image anchor.
var referenceImage: ARReferenceImage { get }
和:
// A descriptive name for your reference image.
var name: String? { get set }
代码如下所示:
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
guard let imageAnchor = anchor as? ARImageAnchor,
let _ = imageAnchor.referenceImage.name
else { return }
anchorsArray.append(imageAnchor)
if imageAnchor.referenceImage.name == "apple" {
print("Image with apple is successfully detected...")
}
}
根据安迪的解决方案,我使用了下面的代码,并使用变量 imageName 作为参考。
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
guard let imageAnchor = anchor as? ARImageAnchor else { return }
let referenceImage = imageAnchor.referenceImage
let imageName = referenceImage.name ?? "no name"
}
我的应用正在跟踪一组 5 张参考图像。但是,我希望它能够区分参考图像(即图像是图像 A、图像 B 还是图像 C。跟踪我打印在明信片上的真实照片)。
目前,我的代码能够检测到图像并在其上放置一个平面(即一个简单的矩形)。但是,我的问题是,是否可以区分该应用程序检测到的是图片A还是图片B?如果可以,怎么做?
我知道有一个使用 ML 的选项,但想看看 SceneKit/ARKit 中是否有我没有考虑过的更简单的选项;特别是因为我使用的是准确的图像,而不是试图让应用程序猜测一个对象。
class ViewController: UIViewController, ARSCNViewDelegate {
@IBOutlet var sceneView: ARSCNView!
override func viewDidLoad() {
super.viewDidLoad()
sceneView.delegate = self
}
//MARK-: WHERE I CONFIGURE MY APP TO DETECT AN IMAGE
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let configuration = ARImageTrackingConfiguration()
guard let trackedImages = ARReferenceImage.referenceImages(inGroupNamed: "Photos", bundle: Bundle.main) else {
print("No images available")
return
}
configuration.trackingImages = trackedImages
configuration.maximumNumberOfTrackedImages = 7
sceneView.session.run(configuration)
}
//MARK-: WHERE I PLACE A PLANE OVER THE DETECTED IMAGE
func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
let node = SCNNode()
if let imageAnchor = anchor as? ARImageAnchor {
let plane = SCNPlane(width: imageAnchor.referenceImage.physicalSize.width, height: imageAnchor.referenceImage.physicalSize.height)
plane.firstMaterial?.diffuse.contents = UIColor(white: 1, alpha: 0.8)
let planeNode = SCNNode(geometry: plane)
planeNode.eulerAngles.x = -.pi / 2
node.addChildNode(planeNode)
}
return node
}
}
您可以使用 referenceImage
和 name
.
// The detected image referenced by the image anchor.
var referenceImage: ARReferenceImage { get }
和:
// A descriptive name for your reference image.
var name: String? { get set }
代码如下所示:
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
guard let imageAnchor = anchor as? ARImageAnchor,
let _ = imageAnchor.referenceImage.name
else { return }
anchorsArray.append(imageAnchor)
if imageAnchor.referenceImage.name == "apple" {
print("Image with apple is successfully detected...")
}
}
根据安迪的解决方案,我使用了下面的代码,并使用变量 imageName 作为参考。
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
guard let imageAnchor = anchor as? ARImageAnchor else { return }
let referenceImage = imageAnchor.referenceImage
let imageName = referenceImage.name ?? "no name"
}