iOS 使用 openCV 从相机检测矩形

标签 ios iphone opencv computer-vision

我正在尝试使用 iPhone 相机使用 openCV 检测名片的边缘(并绘制它们)。我是这个框架以及计算机视觉或 C++ 的新手。

我正在尝试使用此处解释的解决方案:https://stackoverflow.com/a/14123682/3708095 ,哪个github项目是https://github.com/foundry/OpenCVSquares

它适用于预定义的图像,但我正在尝试让它适用于相机。

为此,我使用 CvVideoCameraDelegate 协议(protocol)CVViewController.mm 中实现它,就像他们在 http://docs.opencv.org/doc/tutorials/ios/video_processing/video_processing.html 中解释的那样像这样:

#ifdef __cplusplus
-(void)processImage:(cv::Mat &)matImage
{
//NSLog (@"Processing Image");
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    matImage = CVSquares::detectedSquaresInImage(matImage, self.tolerance, self.threshold, self.levels, [self accuracy]);

    UIImage *image = [[UIImage alloc]initWithCVMat:matImage orientation:UIImageOrientationDown];

    dispatch_async(dispatch_get_main_queue(), ^{
        self.imageView.image = image;
    });
});

}
#endif

编辑:

如果我这样做,它会给我一个 EXC_BAD_ACCESS...

如果我在处理它之前克隆 matImage,通过记录它,它似乎处理了图像甚至找到了矩形,但是矩形没有绘制到图像输出到 imageView。

cv::Mat temp = matImage.clone();    

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    UIImage *image = [[UIImage alloc]CVSquares::detectedSquaresInImage(temp, self.tolerance, self.threshold, self.levels, [self accuracy])
                                       orientation:UIImageOrientationDown];

    dispatch_async(dispatch_get_main_queue(), ^{
        self.imageView.image = image;
    });
});

我很确定我遗漏了什么,可能是因为我没有正确传递一些对象,不是指向对象的指针,而我需要修改的对象不是。

无论如何,如果这不是正确的方法,我将非常感谢他们使用 openCV 或 GPUImage 做类似事情的教程或示例。 (也不熟悉)...

最佳答案

所以解决方案其实很简单......

与其尝试使用 matImage 设置 imageView.image,它只需要将 matImage 转换为在 imageView 中实际修改因为 CvVideoCamera 已经用 imageView 初始化(并链接到):

self.videoCamera = [[CvVideoCamera alloc]initWithParentView:self.imageView];

最后的功能是这样的:

#ifdef __cplusplus
-(void)processImage:(cv::Mat &)matImage
{
    matImage = CVSquares::detectedSquaresInImage(matImage, self.angleTolerance, self.threshold, self.levels, self.accuracy);
}
#endif

关于iOS 使用 openCV 从相机检测矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27181068/

相关文章:

ios - 如何将带有字符串 @"XYZ"的部分设置为表格 View 中的第一部分

ios - Live555 编译 iOS 构建错误

iphone - NSMutableArray大小的监听器和观察器

c++ - 如何在 OpenCV 中使用高斯过滤单列垫

python - 具有多个种子的 OpenCV FloodFill

android - NDK 编码中 OpenCV 的未定义引用错误过多

ios - 停止从 textView 中获取文本

ios - HeaderDoc不生成HTML文档

ios - 目前有什么方法可以在装有 iOS 9 的 iOS 设备上浏览应用程序文件夹/应用程序沙箱

iphone - 整个应用程序的圆角屏幕角和状态栏