Sam*_*ing 5 uiimage swift cidetector
我已经CIDetector在我的应用程序中实现了一个检测图像上的矩形,但现在我如何使用返回CGPoint的图像来裁剪图像,以便我可以显示它?
从视角我尝试应用CIPerspectiveCorrection过滤器,但无法使其工作.
我已经四处寻找并发现了一些线索,但在Swift中找不到解决方案.
如何使用CIDetector(检测到的矩形)提供的数据来修复透视并裁剪图像?
对于那些可能不熟悉CIDetectorTypeRectangle返回内容的人:它返回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)
])
}
Run Code Online (Sandbox Code Playgroud)
无论您在何处检测到矩形:
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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1558 次 |
| 最近记录: |