如何使用GPUImageHarrisCornerDetectionFilter获得角落 [英] How to get corners using GPUImageHarrisCornerDetectionFilter

查看:104
本文介绍了如何使用GPUImageHarrisCornerDetectionFilter获得角落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to get the corner points from a still image using GPUImageHarrisCornerDetectionFilter.

我查看了项目中的示例代码,我查看了文档,我看过这篇文章的内容大致相同:
GPUImage Harris Corner在现有的UIImage上进行检测会产生黑屏输出

I have looked at the example code from the project, I have looked at the documentation, and I have looked at this post that is about the same thing: GPUImage Harris Corner Detection on an existing UIImage gives a black screen output

但我无法使其正常工作 - 我很难理解这是怎么回事使用静止图像。

But I can't make it work - and I have a hard time understanding how this is supposed to work with still images.

我现在所拥有的是:

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
}

此函数始终返回 [] 所以它显然不起作用。

This function always returns [] so it's obviously not working.

一个有趣的细节 - 我从GPUImage示例中编译了FilterShowcaseSwift项目,并且过滤器无法找到非常清晰的角落,就像在一个上面t在黑背景的纸。

An interesting detail - I compiled the FilterShowcaseSwift project from GPUImage examples, and the filter fails to find very clear corners, like on a sheet of paper on a black background.

推荐答案

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

这里的代码设置了一个每帧调用的块。

This code you have here sets a block that gets called every frame.

这是一个异步过程,所以当你的函数返回时从未被调用过你的数组始终为空。它应该在帧完成处理后调用。

This is an asynchronous process so when your function returns that has never been called yet and your array should always be empty. It should be called after the frame has finished processing.

要验证这一点,在该块内设置一个断点,看看它是否被调用。

To verify this, set a breakpoint inside that block and see if it gets called.

评论中来自Brad Larson(GPUImage的创建者)的警告:

您在此处创建的GPUImage <$退出此函数后,将取消分配c $ c> stillImageSource ,在这种情况下可能会导致崩溃。

The GPUImage you create here stillImageSource will be deallocated after this function exits and may cause crashes in this case.

这篇关于如何使用GPUImageHarrisCornerDetectionFilter获得角落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆