2016-01-21 9 views
5

イメージに長方形を検出するために私のアプリケーションにCIDetectorを実装しましたが、返されたCGPointを使って画像を切り抜いて表示する方法を教えてください。CIDetectorパースペクティブとスウィフトの切り抜き

私はCIPerspectiveCorrectionフィルタを適用しようとしましたが、動作させることができませんでした。

私は検索していくつかの手がかりを見つけましたが、Swiftで解決策を見つけることができませんでした。

CIDetector(検出された矩形)によって提供されるデータを使用して、パースペクティブを修正し、画像をトリミングするにはどうすればよいですか?

CIDetectorTypeRectangleが返すものに慣れていない場合は、CGPointのbottomLeft、bottomRight、topLeft、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() 
関連する問題