iOS使用openCV检测来自摄像头的矩形

jde*_*dev 7 iphone opencv computer-vision ios

我正在尝试使用openCV用iPhone相机检测名片的边缘(并绘制它们).我是这个框架的新手,以及计算机视觉或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
Run Code Online (Sandbox Code Playgroud)

编辑:

如果我这样做,它给了我一个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;
    });
});
Run Code Online (Sandbox Code Playgroud)

我很确定我错过了一些东西,可能是因为我没有正确传递某个对象,指向对象的指针等等,而我需要修改的对象则没有.

无论如何,如果这不是正确的方法,我真的很感谢他们做这样的事情的教程或例子,使用openCV或GPUImage(也不熟悉它)......

jde*_*dev 0

所以解决方案实际上非常简单......

它不需要尝试使用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
Run Code Online (Sandbox Code Playgroud)