Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift displayTransform调用后图像方向错误_Swift_Augmented Reality_Arkit_Ciimage_Cvpixelbuffer - Fatal编程技术网

Swift displayTransform调用后图像方向错误

Swift displayTransform调用后图像方向错误,swift,augmented-reality,arkit,ciimage,cvpixelbuffer,Swift,Augmented Reality,Arkit,Ciimage,Cvpixelbuffer,我正在尝试使用以下方法从当前ARFrame获取图像: if let imageBuffer = sceneView.session.currentFrame?.capturedImage { let orientation = UIApplication.shared.statusBarOrientation let viewportSize = sceneView.bounds.size let transformation = sceneView.session.cu

我正在尝试使用以下方法从当前
ARFrame
获取图像:

if let imageBuffer = sceneView.session.currentFrame?.capturedImage {
    let orientation = UIApplication.shared.statusBarOrientation
    let viewportSize = sceneView.bounds.size
    let transformation = sceneView.session.currentFrame?.displayTransform(for: orientation, viewportSize: viewportSize)

    let ciImage = CIImage(cvPixelBuffer: imageBuffer).transformed(by: transformation)
}    


对于景观,它非常有效。对于纵向,我得到的图像角度错误(旋转180度)。知道为什么吗

首先,我应该说这绝对是一个令人不快的bug

一个问题是,当您将ARFrame包含的
肖像
图像转换为CIImage或CGImage时,它会丢失方向并逆时针旋转180度。此问题仅影响
纵向
图像<代码>横向一点也不受影响

这是因为
纵向
图像在转换阶段没有关于其方向的信息,因此,纵向中的图像即使转换为CIImage或CGImage,仍保持纵向模式

要解决此问题,您应该将“标准”风景的
宽度
/
高度
“非标准”肖像的
宽度
/
高度
,如果这些值不同,请将图像顺时针旋转180度(或应用方向格)


希望这有帮助

坐标系

我们需要非常清楚我们在哪个坐标系中工作。 我们知道UIKit的左上角有
(0,0)
,右上角有
(1,1)
,但CoreImage的情况并非如此:

由于核心图像的坐标系与UIKit不匹配。。。(见附件)

或视觉(包括CoreML识别):

Vision使用0.0到1.0之间的标准化坐标空间,并使用较低的 左原点。(见附件)

但是,
displayTransform
使用UIKit方向:

从中的标准化图像坐标转换而来的变换矩阵 将捕获的图像转换为说明 指定的参数。标准化图像坐标范围为 图像左上角的(0,0)到下角的(1,1) 右拐角。(见附件)

因此,如果您将
CVPixelBuffer
加载到
CIImage
中,然后尝试应用
displayTransform
矩阵将被翻转(如您所见)。 但是,它也弄乱了图像

什么是显示变换

显示变换似乎主要用于金属,或其他倾向于匹配核心图像方向的较低级别绘图例程。 变换缩放图像并移动图像,使其在指定边界内“纵横比填充”

如果要在
UIImageView
中显示图像,则图像会反转,因为它们的方向不同。但是,图像视图会为您进行纵横向填充转换, 因此没有理由移动或缩放,因此根本没有理由使用
displayTransform
。只需将图像旋转到正确的方向

// apply an orientation. You can easily make a function
// which converts the screen orientation to this parameter

let rotated = CIImage(cvPixelBuffer: frame.capturedImage).oriented(...)
imageView.image = UIImage(ciImage: rotated)
如果要在图像上覆盖内容,例如通过将子视图添加到
UIImageView
,则
displayTransform
可能会有所帮助。 它将图像坐标(UIKit方向)转换为图像视图中的坐标
与显示的图像对齐(由于纵横比填充而移动和缩放)。

@SachinVas尝试过,但不起作用。。。据我所知,这使用了来自图像的元数据这不是
displayTransform
应该做的吗?知道为什么吗?如果我通过
.graphitoupsidedown
而不是
.graphito
(反之亦然),它会按预期工作…
.graphitoupsidedown
顺时针旋转180度相同。)我刚刚看了你的编辑,谢谢你explaining@AndydisplayTransform还可以缩放和移动坐标,这可能不是您想要做的。看看我的答案。