为什么我的图片CIPerspectiveCorrection每个设备上有什么不同?

问题描述 投票:0回答:1

我想用CIPerspectiveCorrection图像,从AVCaptureSession承担。但是所得到的校正后的图像是不同的设备上的不同,但它是使用相同的代码。这是我的代码来获得积分:

targetRectLayer是我在绘制一个矩形,以突出的聚焦的矩形层。 scannerViewis,我在显示视频会话视图

let request = VNDetectRectanglesRequest { req, error in
    DispatchQueue.main.async {
        if let observation = req.results?.first as? VNRectangleObservation {
            let points = self.targetRectLayer.drawTargetRect(observation: observation, previewLayer: self.previewLayer, animated: false)
            let size = self.scannerView.frame.size
            self.trackedTopLeftPoint = CGPoint(x: points.topLeft.x / size.width, y: points.topLeft.y / size.height )
            self.trackedTopRightPoint = CGPoint(x: points.topRight.x / size.width, y: points.topRight.y / size.height )
            self.trackedBottomLeftPoint = CGPoint(x: points.bottomLeft.x / size.width, y: points.bottomLeft.y / size.height )
            self.trackedBottomRightPoint = CGPoint(x: points.bottomRight.x / size.width, y: points.bottomRight.y / size.height )
        } else {
            _ = self.targetRectLayer.drawTargetRect(observation: nil, previewLayer: self.previewLayer, animated: false)
        }
    }
}

func drawTargetRect(observation: VNRectangleObservation?, previewLayer: AVCaptureVideoPreviewLayer?, animated: Bool) -> (topLeft: CGPoint, topRight: CGPoint, bottomLeft: CGPoint, bottomRight: CGPoint) {
    guard let observation = observation, let previewLayer = previewLayer else {
        draw(path: nil, animated: false)
        return (topLeft: CGPoint.zero, topRight: CGPoint.zero, bottomLeft: CGPoint.zero, bottomRight: CGPoint.zero)
    }

    let convertedTopLeft: CGPoint = previewLayer.layerPointConverted(fromCaptureDevicePoint: CGPoint(x: observation.topLeft.x, y: 1 - observation.topLeft.y))
    let convertedTopRight: CGPoint = previewLayer.layerPointConverted(fromCaptureDevicePoint: CGPoint(x: observation.topRight.x, y: 1 - observation.topRight.y))
    let convertedBottomLeft: CGPoint = previewLayer.layerPointConverted(fromCaptureDevicePoint: CGPoint(x: observation.bottomLeft.x, y: 1 - observation.bottomLeft.y))
    let convertedBottomRight: CGPoint = previewLayer.layerPointConverted(fromCaptureDevicePoint: CGPoint(x: observation.bottomRight.x, y: 1 - observation.bottomRight.y))

    let rectanglePath = UIBezierPath()
    rectanglePath.move(to: convertedTopLeft)
    rectanglePath.addLine(to: convertedTopRight)
    rectanglePath.addLine(to: convertedBottomRight)
    rectanglePath.addLine(to: convertedBottomLeft)
    rectanglePath.close()

    draw(path: rectanglePath, animated: animated)

    return (topLeft: convertedTopLeft, topRight: convertedTopRight, bottomLeft: convertedBottomLeft, bottomRight: convertedBottomRight)
}

这就是我把指向新的位置继续致力于CIPerspectiveCorrection:

let imageTopLeft: CGPoint = CGPoint(x: image.size.width * trackedBottomLeftPoint.x, y: trackedBottomLeftPoint.y * image.size.height)
let imageTopRight: CGPoint = CGPoint(x: image.size.width * trackedTopLeftPoint.x, y: trackedTopLeftPoint.y * image.size.height)
let imageBottomLeft: CGPoint = CGPoint(x: image.size.width * trackedBottomRightPoint.x, y: trackedBottomRightPoint.y * image.size.height)
let imageBottomRight: CGPoint = CGPoint(x: image.size.width * trackedTopRightPoint.x, y: trackedTopRightPoint.y * image.size.height)

当应用CIPerspectiveCorrection,笛卡儿坐标系采取谨慎:

extension UIImage {
   func cartesianForPoint(point:CGPoint,extent:CGRect) -> CGPoint {
       return CGPoint(x: point.x,y: extent.height - point.y)
   }
}

其他数学是所有基于这些4个self.trackedPoints我已经在这里设置。这里有什么区别我的意思是,看到未切断地区的一些图片。突出显示targetRect是完全画在文档的边缘。这个结果是在这些设备上持久。所有图片都拍摄于portrait,运行iOS 12.1.3。

为什么这些图像有什么不同?计算使用比例值,没有硬编码值,并且它们是独立的大小比率。

iPhone X,见左图空间和右

iphonex

iPad的空气2,看空间的顶部和底部

ipadair2

iPhone 5S,这就是我希望所有的人iphone5s

swift core-image
1个回答
1
投票

我发现了videoGravity属性配置失败。它被设置为.resizeAspectFill,因此在推动的观点边境视频边之一,以填补对方留下任何空间。而且,由于所有的设备提供不同的空间,空白处补偿是不同的,以视频流的图片时,在全分辨率,而不仅仅是可见一个导致所描述的行为。我过于注重数学部分,错过了可能。

在iphone5s的完美契合是完全随机的。

这是纠正代码如下:

// setup view preview layer
    let previewLayer = AVCaptureVideoPreviewLayer(session: newCaptureSession)
    self.previewLayer = previewLayer
    previewLayer.videoGravity = .resize
    self.scannerView.layer.addSublayer(previewLayer)
    self.scannerView.layer.addSublayer(targetRectLayer)
© www.soinside.com 2019 - 2024. All rights reserved.