[英]CIDetector Perspective And Crop in Swift
I've implemented a CIDetector
in my app to detect rectangles on a image, but now how can i use the returned CGPoint
's to crop the image so that i can display it back? 我在我的应用程序中实现了一个CIDetector
来检测图像上的矩形,但是现在我如何使用返回的CGPoint
来裁剪图像以便我可以显示它?
For the perspective i've tried applying the CIPerspectiveCorrection filter, but couldn't get it to work. 从视角我尝试应用CIPerspectiveCorrection过滤器,但无法使其工作。
I've searched around and found some clues but couldn't find a solution in Swift. 我已经四处寻找并发现了一些线索,但在Swift中找不到解决方案。
How do i use the data provided by the CIDetector
(detected rectangle) to fix perspective and crop my image? 如何使用CIDetector
(检测到的矩形)提供的数据来修复透视并裁剪图像?
For anyone who might not be familiar with what a CIDetectorTypeRectangle
returns: it returns 4 CGPoint
's bottomLeft,bottomRight,topLeft,topRight. 对于那些可能不熟悉CIDetectorTypeRectangle
返回的人:它返回4 CGPoint
的bottomLeft,bottomRight,topLeft,topRight。
Here's what worked: 这是有效的:
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)
])
}
Wherever you detect your rectangle: 无论您在何处检测到矩形:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.