6

CIDetector画像上の四角形を検出するためにアプリに を実装しましたが、返された を使用しCGPointて画像をトリミングし、表示できるようにするにはどうすればよいですか?

パースペクティブについては、CIPerspectiveCorrection フィルターを適用しようとしましたが、機能しませんでした。

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

CIDetector(検出された四角形) によって提供されるデータを使用して、遠近感を修正し、画像をトリミングするにはどうすればよいですか?

CIDetectorTypeRectangleが返すものに慣れていない可能性がある人のために: 4CGPointの bottomLeft,bottomRight,topLeft,topRight を返します。

4

1 に答える 1

9

うまくいったのは次のとおりです。

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()
于 2016-01-21T12:52:59.607 に答える