我已经尝试了几个小时来使用 openCV 运行一个 xcode 项目。我已经构建了源代码,将其导入项目并包含 #ifdef __cplusplus #import opencv2/opencv.hpp> #万一 在 .pch 文件中。
我按照 http://docs.opencv.org/trunk/doc/tutorials/introduction/ios_install/ios_install.html 中的说明进行操作
我在编译时仍然遇到许多 Apple Mach-O 链接器错误。
Undefined symbols for architecture i386:
"std::__1::__vector_base_common<true>::__throw_length_error() const", referenced from:
请帮帮我,我真的迷路了..
更新:
所有错误都已修复,现在我正在尝试检测圆圈..
Mat src, src_gray;
cvtColor( image, src_gray, CV_BGR2GRAY );
vector<Vec3f> circles;
/// Apply the Hough Transform to find the circles
HoughCircles( src_gray, circles, CV_HOUGH_GRADIENT, 1, image.rows/8, 200, 100, 0, 0 );
/// Draw the circles detected
for( size_t i = 0; i < circles.size(); i++ )
{
Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
int radius = cvRound(circles[i][2]);
// circle center
circle( src, center, 3, Scalar(0,255,0), -1, 8, 0 );
// circle outline
circle( src, center, radius, Scalar(0,0,255), 3, 8, 0 );
}
我正在使用上面的代码,但是没有在图像上绘制圆圈。有什么明显的地方我做错了吗?
最佳答案
尝试我对这个问题的回答中的解决方案...
How to resolve iOS Link errors with OpenCV
在 github 上我还有 a couple的 simple working samples - 使用最近构建的 openCV 框架。
注意 - OpenCVSquares 比 OpenCVSquaresSL 更简单。后者适用于 Snow Leopard 向后兼容 - 它包含两个 openCV 框架构建和 3 个目标,因此如果它能在您的系统上运行,您最好使用更简单的 OpenCVSquares。
要使 OpenCVSquares 适应圆圈检测,我建议您从 Hough Circles c++ sample 开始来自 openCV 发行版,并使用它来改编/替换 CVSquares.cpp
和 CVSquares.h
,比如说 CVCircles.cpp
和 CVCicle.h
原理完全一样:
- 从 C++ 中删除 UI 代码,UI 在 obj-C 端提供
- 将 main() 函数转换为头文件中声明的类的静态成员函数。这应该以 Objective-C 消息的形式镜像到包装器(它将 obj-c 方法转换为 c++ 函数调用)。
从 objective-C 方面,您将 UIImage 传递给包装器对象,它:
- 将 UIImage 转换为 cv::Mat 图像
- 将 Mat 传递给 c++ 类进行处理
- 将 Mat 的结果转换回 UIImage
- 将处理后的 UIImage 返回给 objective-C 调用对象
更新
改编后的 houghcircles.cpp 在最基本的部分应该看起来像这样(我用 CVCircles 类替换了 CVSquares 类):
cv::Mat CVCircles::detectedCirclesInImage (cv::Mat img)
{
//expects a grayscale image on input
//returns a colour image on ouput
Mat cimg;
medianBlur(img, img, 5);
cvtColor(img, cimg, CV_GRAY2RGB);
vector<Vec3f> circles;
HoughCircles(img, circles, CV_HOUGH_GRADIENT, 1, 10,
100, 30, 1, 60 // change the last two parameters
// (min_radius & max_radius) to detect larger circles
);
for( size_t i = 0; i < circles.size(); i++ )
{
Vec3i c = circles[i];
circle( cimg, Point(c[0], c[1]), c[2], Scalar(255,0,0), 3, CV_AA);
circle( cimg, Point(c[0], c[1]), 2, Scalar(0,255,0), 3, CV_AA);
}
return cimg;
}
请注意,为简单起见,输入参数减少为一个 - 输入图像。很快我将在 github 上发布一个示例,其中将包含一些与 iOS UI 中的 slider 控件相关的参数,但您应该先让这个版本运行。
由于函数签名已更改,您应该在链中跟进它...
更改 houghcircles.h 类定义:
static cv::Mat detectedCirclesInImage (const cv::Mat image);
修改 CVWrapper 类以接受调用 detectedCirclesInImage
的类似结构方法
+ (UIImage*) detectedCirclesInImage:(UIImage*) image
{
UIImage* result = nil;
cv::Mat matImage = [image CVGrayscaleMat];
matImage = CVCircles::detectedCirclesInImage (matImage);
result = [UIImage imageWithCVMat:matImage];
return result;
}
请注意,我们正在将输入 UIImage 转换为灰度图像,因为 houghcircles 函数需要输入灰度图像。 注意拉取我的 github 项目的最新版本,我发现 CVGrayscaleMat 类别中的错误现已修复。输出图像是彩色的(颜色应用于灰度输入图像以挑选出找到的圆圈)。
如果您希望您的输入和输出图像是彩色的,您只需要确保对输入图像进行灰度转换以发送到 Houghcircles() - 例如 cvtColor(input_image , 灰度图像, CV_RGB2GRAY)
;并将您找到的圆圈应用到彩色输入图像(这将成为您的返回图像)。
最后在您的 CVViewController 中,将您的消息更改为 CVWrapper 以符合这个新签名:
UIImage* image = [CVWrapper detectedCirclesInImage:self.image];
如果您遵循所有这些细节,您的项目将产生圆圈检测结果。
更新 2
OpenCVCircles now on Github
使用 slider 调整 HoughCircles() 参数
关于ios - iOS 的 OpenCV 错误/检测 Hough Circles,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14416089/