使用CIFilter渲染时,实时摄影机会被拉伸. -迅捷4 [英] Live camera is getting stretched while rendering using CIFilter. - Swift 4

查看:106
本文介绍了使用CIFilter渲染时,实时摄影机会被拉伸. -迅捷4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在渲染时应用相机滤镜,我的代码是...

I want to apply camera filter while rendering, my code is...

func session(_ session: ARSession, didUpdate frame: ARFrame) {
let image = CIImage(cvPixelBuffer: frame.capturedImage)
        var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0

        color.getRed(&r, green: &g, blue: &b, alpha: &a)
        filter.setDefaults()
        filter.setValue(image, forKey: kCIInputImageKey)
        filter.setValue(CIVector(x: r, y: 0, z: 0, w: 0), forKey: "inputRVector")
        filter.setValue(CIVector(x: 0, y: g, z: 0, w: 0), forKey: "inputGVector")
        filter.setValue(CIVector(x: 0, y: 0, z: b, w: 0), forKey: "inputBVector")
        filter.setValue(CIVector(x: 0, y: 0, z: 0, w: a), forKey: "inputAVector")

        if let result = filter.outputImage,
            let cgImage = context.createCGImage(result, from: result.extent) {
            sceneView.scene.background.contents = cgImage
            sceneView.scene.background.contentsTransform = SCNMatrix4MakeRotation(.pi / 2, 0, 0, 1)
        }
}

在运行时,输出被拉伸.我已经附上了两张图片,

at runtime, output is getting stretched. I have attached two images,

  1. 普通相机渲染
  2. 使用滤镜相机渲染.

请帮助我解决它,如果您提供任何演示代码或项目,将对您有很大帮助.谢谢你.

Please help me to resolve it, it would be great help if you provide any demo code or project. Thank you.

推荐答案

首先,我建议您制作一份ARFrame的副本,否则,如果未及时处理,currentFrame将会产生各种各样的怪异现象,因为内存将被硬件丢弃.

First of all, i recommend you make a copy of the ARFrame, otherwise currentFrame will get all sort of weirdness if not processed soon enough, because the memory will be discarded by the hardware.

第二,拉伸是因为您使用的是与视口图像不同的缓冲图像,请抓取session.snapshot并将其保存到mac,然后再抓取cgImage并将其保存,然后在中打开它们预览时,您会看到大小有所不同,因此,当您要将较宽的图像放回视口大小时,它将拉伸.

Second, the stretching is because you are using the buffer image that is different from the viewport image, grab a session.snapshot and save it to you mac, and then grab your cgImage and save it too, open them in Preview, you will see the sizes differ, so, when you want to put that wider image back to the viewport size, it stretches.

您无需为此演示一个项目,只需打印您的场景帧大小和缓冲区大小,检查两者之间的比率即可.

You do not need a demo project for this one, just print your scene frame size, and your buffer size, check the ratios between both and you will understand your issue.

这篇关于使用CIFilter渲染时,实时摄影机会被拉伸. -迅捷4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆