gpt4 book ai didi

ios - ios中相应标记的标记检测和3d模型显示(使用ARkit)

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:07:12 25 4
gpt4 key购买 nike

之前我使用 vuforia(unity) 为 iOS 开发了一个 AR 应用程序。现在我必须使用 ARKit 实现相同的应用程序。

ARKit 很棒,除了没有标记检测。

我尝试过使用视觉来检测标记,但到目前为止没有成功。我可以提供一些用于标记检测并在 iOS 标记上显示 3d 模型的样本吗?

提前致谢。

最佳答案

有多种方法可以实现您的目标,但可以说最简单的方法是使用图像作为标记。

ARKit 1.5 开始,您可以使用 ReferenceImages 放置 AR 内容,这与您在 Vuforia 中使用的标记基本相同> 或 EasyAR

供您引用,referenceImage 很简单:

An image to be recognized in the real-world environment during a world-tracking AR session.

要使用此功能,您需要传入:

collection of reference images to your session configuration's detectionImages property.

可以这样设置:

var detectionImages: Set<ARReferenceImage>! { get set }

ARKit 1.5 需要注意的一件重要事情是,与 Vuforia 不同的是,Vuforia 可以允许对图像进行扩展跟踪:

Image detection doesn't continuously track real-world movement of the image or track when the image disappears from view. Image detection works best for cases where AR content responds to static images in the scene—for example, identifying art in a museum or adding animated elements to a movie poster.

正如@Alexander 所说,要了解这如何适合您的情况,最好的办法是在线查看 SampleCodeDocumentation,可在此处获取:

Recognizing Images In An AR Experience

然而,核心要点是:

启用图像检测:

您需要先提供一个或多个ARReferenceImage资源。这些可以使用 Xcode 中的 AR Assets 目录手动添加,记住您必须尽可能准确地在 Xcode 中输入图像的物理尺寸,因为 ARKit 依靠此信息来确定图像与相机的距离。

ARReferenceImages 也可以使用以下方法即时创建:

init(CGImage, orientation: CGImagePropertyOrientation, physicalWidth: CGFloat)

Which creates a new reference image from a Core Graphics image object.

init(CVPixelBuffer, orientation: CGImagePropertyOrientation, physicalWidth: CGFloat)

Which creates a new reference image from a Core Video pixel buffer.

完成此操作后,您需要创建一个世界跟踪配置,在运行 ARSession 之前传入 ARReferenceImages,例如:

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

let configuration = ARWorldTrackingConfiguration()
configuration.detectionImages = referenceImages
session.run(configuration, options: [.resetTracking, .removeExistingAnchors])

处理图像检测:

ARSession 检测到 ARReferenceImage 并创建 ARImageAnchor 时,它简单地提供:

Information about the position and orientation of an image detected in a world-tracking AR session.

因此,如果图像检测成功,您将需要使用以下 ARSCNViewDelegate 回调来处理对象的放置等:

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) { }

使用它来处理 3D 内容放置的示例如下:

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {

//1. If Out Target Image Has Been Detected Than Get The Corresponding Anchor
guard let currentImageAnchor = anchor as? ARImageAnchor else { return }

//2. Get The Targets Name
let name = currentImageAnchor.referenceImage.name!

//3. Get The Targets Width & Height
let width = currentImageAnchor.referenceImage.physicalSize.width
let height = currentImageAnchor.referenceImage.physicalSize.height

//4. Log The Reference Images Information
print("""
Image Name = \(name)
Image Width = \(width)
Image Height = \(height)
""")

//5. Create A Plane Geometry To Cover The ARImageAnchor
let planeNode = SCNNode()
let planeGeometry = SCNPlane(width: width, height: height)
planeGeometry.firstMaterial?.diffuse.contents = UIColor.white
planeNode.opacity = 0.25
planeNode.geometry = planeGeometry

//6. Rotate The PlaneNode To Horizontal
planeNode.eulerAngles.x = -.pi/2

//7. The Node Is Centered In The Anchor (0,0,0)
node.addChildNode(planeNode)

//8. Create AN SCNBox
let boxNode = SCNNode()
let boxGeometry = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)

//9. Create A Different Colour For Each Face
let faceColours = [UIColor.red, UIColor.green, UIColor.blue, UIColor.cyan, UIColor.yellow, UIColor.gray]
var faceMaterials = [SCNMaterial]()

//10. Apply It To Each Face
for face in 0 ..< 5{
let material = SCNMaterial()
material.diffuse.contents = faceColours[face]
faceMaterials.append(material)
}
boxGeometry.materials = faceMaterials
boxNode.geometry = boxGeometry

//11. Set The Boxes Position To Be Placed On The Plane (node.x + box.height)
boxNode.position = SCNVector3(0 , 0.05, 0)

//12. Add The Box To The Node
node.addChildNode(boxNode)
}

希望对你有帮助

关于ios - ios中相应标记的标记检测和3d模型显示(使用ARkit),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46768493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com