MAC 下安装OpenCV

1、首先安装Homebrew工具,有了这个工具安装OpenCV就很简单
安装Homebrew在终端输入下面命令行即可:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2、安装好Homebrew之后,使用下面命令安装OpenCV

# add opencv
brew tap homebrew/science

# install opencv
brew install opencv

有问题可以参考这篇文章:
http://www.pyimagesearch.com/2017/05/15/resolving-macos-opencv-homebrew-install-errors/

3、接下来打开XCode开发工具(可在商店中下载),新建一个工程选择macOS-Command Line Tool ,如图:

MAC 下安装OpenCV_第1张图片
image.png

开发语言选择c++

MAC 下安装OpenCV_第2张图片

4、修改工程 Build setting 中 Header Search Paths的值为 /usr/local/include
修改工程 Build setting 中 Library Search Paths的值为 /usr/local/lib/**
如图:

MAC 下安装OpenCV_第3张图片
image.png
MAC 下安装OpenCV_第4张图片
image.png

5、点到工程的General-Linked framework and libraries,点击+号

MAC 下安装OpenCV_第5张图片
image.png

在弹出的框中点击 AddOther 按钮,再按组合键 command+shift+G 在Go to the folder中填上路径 /usr/local/lib点击go

MAC 下安装OpenCV_第6张图片
image.png

把所有libopencv前缀dylib后缀的都选中加进工程

MAC 下安装OpenCV_第7张图片
image.png

把下面代码覆盖你的main.cpp 文件

//
//  main.cpp
//  test_OpenCV
//
//  Created by 朱松泽 on 2017/8/16.
//  Copyright © 2017年 com.gdtech. All rights reserved.
//

#include 
#include 
#include 
#include 

using namespace std;
using namespace cv;

IplImage* doCanny(IplImage* image_input,
                  double lowThresh,
                  double highThresh,
                  double aperture)
{
    if(image_input->nChannels != 1)
        return (0);
    
    IplImage* image_output = cvCreateImage(cvGetSize(image_input),
                                           image_input->depth,
                                           image_input->nChannels);
    
    cvCanny(image_input,image_output,lowThresh,highThresh,aperture);
    
    return(image_output);
}
int main(int argc, const char * argv[]) {
    cvNamedWindow("Camera" , CV_WINDOW_AUTOSIZE );
    
    CvCapture* capture = cvCreateCameraCapture(CV_CAP_ANY);
    
    assert(capture != NULL);
    
    IplImage *frame = 0;
    frame = cvQueryFrame(capture);
    
    IplImage *frame_edge = cvCreateImage(cvGetSize(frame),
                                         IPL_DEPTH_8U,
                                         1);
    while(1)
    {
        frame = cvQueryFrame(capture);
        if(!frame) break;
        
        cvConvertImage(frame,frame_edge,0);
        frame = cvCloneImage(frame_edge);
        
        frame_edge = doCanny(frame_edge,70,90,3);
        
        cvShowImage("Camera",frame_edge);
        char c = cvWaitKey(15);
        if(c == 27)  break;
    }
    
    cvReleaseCapture(&capture);
    cvReleaseImage( &frame_edge );
    cvReleaseImage( &frame);
    
    
    return (int)0;
}

运行工程,上面代码效果从Mac自带的摄像头读入图像,然后canny提取了边缘然后显示。
效果如图

MAC 下安装OpenCV_第8张图片
image.png

代码来自文章:http://blog.csdn.net/zhoufan900428/article/details/45968125
安装配置过程参考了文章
http://blog.devtang.com/2012/10/27/use-opencv-in-ios/

你可能感兴趣的:(MAC 下安装OpenCV)