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

Swift 在检测到的矩形上添加图像,swift,core-image,cidetector,Swift,Core Image,Cidetector,我正在使用CIDetector检测一个矩形,我想用一个矩形图像替换它 检测效果很好,我可以在上面画一个覆盖图,你知道我怎样才能使图像适合所有的角落吗 代码如下: func drawHighlightOverlayForPoints(image: CIImage, topLeft: CGPoint, topRight: CGPoint, bottomLeft: CGPoint, bottomRight: CGPoint)

我正在使用
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项目可以得到我想要的结果,虽然帧速率在一段时间后下降到1fps以下,但我不知道是什么原因导致了这一点,这可能是我的实现

  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

        }
    }

看看这篇博文:谢谢Simon的文章,这也可以实时完成吗?实时运行CIFilters没有问题,但我发现在视频中查找矩形时检测器有点笨拙。SimonYeah注意到了这一点,为了让你的文章与实时提要一起工作,我需要按照你的步骤使用CISourceAtopCompositing过滤器吗?我是否应该修改上面的代码以应用所有过滤器?我得到了不同的结果..我有另一个项目你可能会发现有用:
cameracaptureheloper
打包所有用于捕获实时视频的代码,并返回
CIImage
流。然后你可以在我的第一篇博文中使用这些代码。看看这篇博文:谢谢Simon的文章,这也可以实时完成吗?实时运行CIFilters没有问题,但我发现在视频中查找矩形时检测器有点笨拙。SimonYeah注意到了这一点,为了让你的文章与实时提要一起工作,我需要按照你的步骤使用CISourceAtopCompositing过滤器吗?我是否应该修改上面的代码以应用所有过滤器?我得到了不同的结果..我有另一个项目你可能会发现有用:
cameracaptureheloper
打包所有用于捕获实时视频的代码,并返回
CIImage
流。然后,您可以在我的第一篇博文中使用该代码。