【问题标题】:iOS 8 Core Image saving a section of an Image via SwiftiOS 8 Core Image 通过 Swift 保存图像的一部分
【发布时间】:2014-12-19 01:03:42
【问题描述】:

我正在使用 CoreImage 框架来检测名片。当我检测到一个矩形 (CIDetectorTypeRectangle) 时,我使用此方法绘制了一个叠加层:

    func drawOverlay(image: CIImage, topLeft: CGPoint, topRight: CGPoint, bottomLeft: CGPoint, bottomRight: CGPoint) -> CIImage {

    var overlay = CIImage(color: CIColor(red: 0, green: 0, blue: 1.0, alpha: 0.3))
    overlay = overlay.imageByCroppingToRect(image.extent())
    overlay = overlay.imageByApplyingFilter("CIPerspectiveTransformWithExtent",
      withInputParameters: [
        "inputExtent": CIVector(CGRect: image.extent()),
        "inputTopLeft": CIVector(CGPoint: topLeft),
        "inputTopRight": CIVector(CGPoint: topRight),
        "inputBottomLeft": CIVector(CGPoint: bottomLeft),
        "inputBottomRight": CIVector(CGPoint: bottomRight)
      ])
    return overlay.imageByCompositingOverImage(image)
  }

现在我需要自动拍摄所选区域的照片并保存。 有谁知道该怎么做? 谢谢!

【问题讨论】:

  • 使用 CGImageCreateWithImageInRect 将其裁剪到您想要的确切部分
  • 感谢您的提问!现在我知道如何在 Swift 中裁剪 CIImage。谷歌拒绝提供帮助,苹果网站也拒绝提供帮助。当您可以轻松调用 imageByCroppingToRect 方法时,我尝试使用过滤器:)

标签: image-processing swift ios8 crop core-image


【解决方案1】:

我找到了解决办法!

func cropBusinessCardForPoints(image: CIImage, topLeft: CGPoint, topRight: CGPoint, bottomLeft: CGPoint, bottomRight: CGPoint) -> CIImage {

        var businessCard: CIImage
        businessCard = image.imageByApplyingFilter("CIPerspectiveTransformWithExtent",
            withInputParameters: [
                "inputExtent": CIVector(CGRect: image.extent()),
                "inputTopLeft": CIVector(CGPoint: topLeft),
                "inputTopRight": CIVector(CGPoint: topRight),
                "inputBottomLeft": CIVector(CGPoint: bottomLeft),
                "inputBottomRight": CIVector(CGPoint: bottomRight)
            ])
        businessCard = image.imageByCroppingToRect(businessCard.extent())

        return businessCard
    }

用法:

//Variables
    var context: CIContext!
    var orientation: UIImageOrientation = UIImageOrientation.Right



override func viewDidLoad() {
        super.viewDidLoad()

        //Initialize the CIContext
        context = CIContext(options:nil)

        ...
}

当检测到矩形时:

//...

let businessCardImage = cropBusinessCardForPoints(image, topLeft: feature.topLeft, topRight: feature.topRight, bottomLeft: feature.bottomLeft, bottomRight: feature.bottomRight)

//Convert CIImage to CGImage
let cgimg = context.createCGImage(businessCardImage, fromRect: businessCardImage.extent())

//Convert CGImage to UIImage
let newImage = UIImage(CGImage: cgimg, scale:1, orientation: orientation)

【讨论】:

  • 不确定您是否正在这样做,但如果您希望 Core Image 也能理顺图像,您可以使用 CIPerspectiveCorrection 过滤器。效果很好。
  • @Ale 您的解决方案有什么特点?
  • 但这会裁剪成矩形。知道如何裁剪到特征点吗? @AlokNair 特征是从 CIDetector 检测到的对象。
猜你喜欢
  • 2015-05-04
  • 2015-11-11
  • 2016-12-22
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 2011-10-20
  • 1970-01-01
相关资源
最近更新 更多