当前环境:
macOS 10.12.5
&& Xcode8
安装
使用homebrew安装:
1). brew install cmake
2). brew install opencv
运行报错:
➜ ~ brew install opencv
Error: No available formula with the name "opencv"
It was migrated from homebrew/core to homebrew/science.
You can access it again by running:
brew tap homebrew/science
➜ ~ brew search opencv
homebrew/science/opencv
If you meant "opencv" specifically:
It was migrated from homebrew/core to homebrew/science.
You can access it again by running:
brew tap homebrew/science
按提示先后执行如下命令,可以安装成功
$ brew tap homebrew/science
$ brew install opencv
安装完成后路径/usr/local/Cellar
下会有openCV
文件夹:
使用Xcode的Command Line Tool 调试OpenCV (当前2.4.13.2版本举例)
1). 创建Command Line Tool
新工程,语言选择C++
2). 复制/usr/local/Cellar/opencv/2.4.13.2/lib
文件夹到项目目录
3). targets
-> Build Phases
-> Link Binary With Libraries
选择lib中的.dylib
文件(不选替身文件)。删除项目目录下这个lib文件夹内的其他多余的文件。
4). targets
-> Build Setting
中,设置:
Header Search Path 为 /usr/local/include
Library Search Path 为 /usr/local/lib
(或者对应到项目中的lib目录,如$(PROJECT_DIR)/myproject/lib
)
完成上边操作,就算配置完了,写代码看看运行效果:
注: Build Setting 中的C++ Standard Libraray 仍然使用的工程默认libc++,没做修改
iOS工程中使用
使用非常简单:
首先从http://opencv.org/releases.html下载新版本的iOS pack
,解压后是个opencv2.framework
文件。
1). 新建一个iOS项目,将opencv2.framework
拖到工程中
2). 手动配置好pch文件,pch文件中必须要加:
#ifdef __cplusplus
#import <opencv2/opencv.hpp>
#endif
3). 用到opencv的.m文件后缀改为.mm
4). 编写测试代码:(这里完全照搬此处:http://blog.csdn.net/songrotek/article/details/40780073))
#import <opencv2/opencv.hpp>
#import <opencv2/imgproc/types_c.h>
#import <opencv2/imgcodecs/ios.h>
//using namespace cv;
@interface ViewController ()
{
cv::Mat cvImage;
}
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:@"2"];
// Convert UIImage * to cv::Mat
UIImageToMat(image, cvImage);
if (!cvImage.empty()) {
cv::Mat gray;
// Convert the image to grayscale;
cv::cvtColor(cvImage, gray, CV_RGBA2GRAY);
// Apply Gaussian filter to remove small edges
cv::GaussianBlur(gray, gray, cv::Size(5,5), 1.2,1.2);
// Calculate edges with Canny
cv::Mat edges;
cv::Canny(gray, edges, 0, 60);
// Fill image with white color
cvImage.setTo(cv::Scalar::all(255));
// Change color on edges
cvImage.setTo(cv::Scalar(0,128,255,255),edges);
// Convert cv::Mat to UIImage* and show the resulting image
self.imageView.image = MatToUIImage(cvImage);
}
}
运行效果:
参考
http://kurtjacobs.weebly.com/computer-vision/up-and-running-with-opencv-30-for-ios
https://imsun.net/posts/iOS-OpenCV-config/
swift 项目集成 https://medium.com/@borisohayon/ios-opencv-and-swift-1ee3e3a5735b