CIDetector 透视图和裁剪 Swift
CIDetector Perspective And Crop in Swift
我在我的应用程序中实现了一个 CIDetector
来检测图像上的矩形,但现在我如何使用返回的 CGPoint
来裁剪图像以便我可以显示它回来了吗?
对于透视,我尝试应用 CIPerspectiveCorrection 过滤器,但无法正常工作。
我四处搜索并找到了一些线索,但在 Swift 中找不到解决方案。
如何使用 CIDetector
(检测到的矩形)提供的数据来修正透视和裁剪图像?
对于可能不熟悉什么是 CIDetectorTypeRectangle
returns 的人:它 returns 4 CGPoint
的 bottomLeft、bottomRight、topLeft、topRight。
这是有效的方法:
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()
我在我的应用程序中实现了一个 CIDetector
来检测图像上的矩形,但现在我如何使用返回的 CGPoint
来裁剪图像以便我可以显示它回来了吗?
对于透视,我尝试应用 CIPerspectiveCorrection 过滤器,但无法正常工作。
我四处搜索并找到了一些线索,但在 Swift 中找不到解决方案。
如何使用 CIDetector
(检测到的矩形)提供的数据来修正透视和裁剪图像?
对于可能不熟悉什么是 CIDetectorTypeRectangle
returns 的人:它 returns 4 CGPoint
的 bottomLeft、bottomRight、topLeft、topRight。
这是有效的方法:
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()