我正在尝试使用GPUImageHarrisCornerDetectionFilter从静止图像中获取拐角点。

我看了项目中的示例代码,看了文档,看了关于同一内容的这篇文章:
GPUImage Harris Corner Detection on an existing UIImage gives a black screen output

但是我无法使其工作-而且我很难理解该如何处理静态图像。

我现在所拥有的是:

func harrisCorners() -> [CGPoint] {
    var points = [CGPoint]()

    let stillImageSource: GPUImagePicture = GPUImagePicture(image: self.image)
    let filter = GPUImageHarrisCornerDetectionFilter()

    filter.cornersDetectedBlock = { (cornerArray:UnsafeMutablePointer<GLfloat>, cornersDetected:UInt, frameTime:CMTime) in
        for index in 0..<Int(cornersDetected) {
            points.append(CGPoint(x:CGFloat(cornerArray[index * 2]), y:CGFloat(cornerArray[(index * 2) + 1])))
        }
    }

    filter.forceProcessingAtSize(self.image.size)
    stillImageSource.addTarget(filter)
    stillImageSource.processImage()

    return points
}

此函数始终返回[],因此显然无法正常工作。

一个有趣的细节-我从GPUImage示例中编译了FilterShowcaseSwift项目,但滤镜无法找到非常清晰的角,就像在黑色背景上的一张纸上一样。

最佳答案

filter.cornersDetectedBlock = { (cornerArray:UnsafeMutablePointer<GLfloat>, cornersDetected:UInt, frameTime:CMTime) in
    for index in 0..<Int(cornersDetected) {
        points.append(CGPoint(x:CGFloat(cornerArray[index * 2]), y:CGFloat(cornerArray[(index * 2) + 1])))
    }
}

您在这里的这段代码设置了一个块,该块在每一帧都被调用。

这是一个异步过程,因此当函数返回时,该函数尚未被调用,并且数组应始终为空。框架完成处理后应调用它。

要验证这一点,请在该块中设置一个断点,然后查看是否被调用。

注释中来自Brad Larson(GPUImage的创建者)的警告:

您在此处创建的stillImageSource GPUImage将在此函数退出后被释放,在这种情况下可能会导致崩溃。

关于ios - 如何使用GPUImage哈里斯角点检测过滤器获得角点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36115650/

10-11 14:54