6

我已经CIDetector在我的应用程序中实现了一个来检测图像上的矩形,但是现在我如何使用返回CGPoint的 's 来裁剪图像以便我可以将其显示回来?

对于透视图,我尝试应用 CIPerspectiveCorrection 过滤器,但无法使其正常工作。

我四处搜索并找到了一些线索,但在 Swift 中找不到解决方案。

如何使用CIDetector(检测到的矩形)提供的数据来修复透视和裁剪图像?

对于任何可能不熟悉 aCIDetectorTypeRectangle返回的人:它返回 4CGPoint的 bottomLeft、bottomRight、topLeft、topRight。

4

1 回答 1

9

这是有效的:

func flattenImage(image: CIImage, topLeft: CGPoint, topRight: CGPoint,bottomLeft: CGPoint, bottomRight: CGPoint) -> CIImage {

    return image.applyingFilter("CIPerspectiveCorrection", withInputParameters: [

        "inputTopLeft": CIVector(cgPoint: topLeft),
        "inputTopRight": CIVector(cgPoint: topRight),
        "inputBottomLeft": CIVector(cgPoint: bottomLeft),
        "inputBottomRight": CIVector(cgPoint: bottomRight)


        ])

}

无论您在哪里检测到矩形:

UIGraphicsBeginImageContext(CGSize(width: flattenedImage!.extent.size.height, height: flattenedImage!.extent.size.width))

UIImage(ciImage:resultImage!,scale:1.0,orientation:.right).draw(in: CGRect(x: 0, y: 0, width: resultImage!.extent.size.height, height: resultImage!.extent.size.width))

let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
于 2016-01-21T12:52:59.607 回答