在检测到的矩形上添加图像

时间:2016-04-16 07:43:41

标签: swift core-image cidetector

我正在使用CIDetector来检测我想用矩形图像替换的矩形。

检测工作正常,我可以在上面绘制覆盖图,任何想法如何使图像适合并粘在各个角落?

以下是代码:

 func drawHighlightOverlayForPoints(image: CIImage, topLeft: CGPoint, topRight: CGPoint,
                                     bottomLeft: CGPoint, bottomRight: CGPoint) -> CIImage {
    var overlay = CIImage(color: CIColor(red: 1.0, green: 0, blue: 0, alpha: 0.5))


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

如果我只是用图像替换叠加层,图像显示非常小并且仅粘贴到一个特定角落并且不填充矩形。

let testImage = UIImage(named: "card.jpg")

var overlayImage = CIImage(image: testImage!)

更新

使用Simons项目给了我想要的结果,虽然帧速率在一段时间后下降到不到1 fps,我不知道什么导致这可能是我的实现?

  func newCameraImage(cameraCaptureHelper: CameraCaptureHelper, image: CIImage)
    {
        //halftone.setValue(image, forKey: kCIInputImageKey)

        if let rect = detector.featuresInImage(image).first as? CIRectangleFeature {

            perspectiveTransform.setValue(CIVector(CGPoint:rect.topLeft),
                                          forKey: "inputTopLeft")
            perspectiveTransform.setValue(CIVector(CGPoint:rect.topRight),
                                          forKey: "inputTopRight")
            perspectiveTransform.setValue(CIVector(CGPoint:rect.bottomRight),
                                          forKey: "inputBottomRight")
            perspectiveTransform.setValue(CIVector(CGPoint:rect.bottomLeft),
                                          forKey: "inputBottomLeft")


            perspectiveTransform.setValue(addImage,
                                          forKey: kCIInputImageKey)

            composite.setValue(image, forKey: kCIInputBackgroundImageKey)

            composite.setValue(perspectiveTransform.outputImage!, forKey: kCIInputImageKey)

            // let transformFinal = composite.outputImage

            imageView.image = composite.outputImage


        } else {

        imageView.image = image

        }
    }

0 个答案:

没有答案