2016-01-21 25 views
5

我在我的應用程序中實現了CIDetector來檢測圖像上的矩形,但是現在如何使用返回的CGPoint來裁剪圖像以便我可以顯示它?CIDetector透視和作物在迅速

對於我嘗試應用CIPerspectiveCorrection過濾器,但無法使其工作的觀點。

我搜索了一下,發現了一些線索,但在Swift中找不到解決方案。

如何使用CIDetector(檢測到的矩形)提供的數據來修復透視並裁剪我的圖像?

對於任何人誰可能不熟悉的東西一CIDetectorTypeRectangle回報:它返回4 CGPoint的BOTTOMLEFT,bottomRight,左上,topRight。

回答

8

這裏是什麼工作:

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()