我已经在我的应用程序中实现了一个CIDetector来检测图像上的矩形,但是现在我如何使用返回的CGPoint来裁剪图像以便我可以将它显示回来呢?
对于透视图,我尝试了应用CIPerspectiveCorrection过滤器,但无法使它工作。
我四处搜寻,发现了一些线索,但在Swift中找不到解决办法。
如何使用CIDetector (检测到的矩形)提供的数据来修复透视图和裁剪图像?
对于任何可能不熟悉CIDetectorTypeRectangle返回的内容的人来说:它返回4CGPoint的bottomLeft、bottomRight、topLeft、topRight。
发布于 2016-01-21 12:52:59
以下是起作用的原因:
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()https://stackoverflow.com/questions/34920045
复制相似问题