1、首先安裝Homebrew工具,有了這個(gè)工具安裝OpenCV就很簡(jiǎn)單
安裝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
有問(wèn)題可以參考這篇文章:
http://www.pyimagesearch.com/2017/05/15/resolving-macos-opencv-homebrew-install-errors/
3遂庄、接下來(lái)打開(kāi)XCode開(kāi)發(fā)工具(可在商店中下載),新建一個(gè)工程選擇macOS-Command Line Tool 劲赠,如圖:
開(kāi)發(fā)語(yǔ)言選擇c++
4涛目、修改工程 Build setting 中 Header Search Paths
的值為 /usr/local/include
修改工程 Build setting 中 Library Search Paths
的值為 /usr/local/lib/**
如圖:
5、點(diǎn)到工程的General-Linked framework and libraries经磅,點(diǎn)擊+號(hào)
在彈出的框中點(diǎn)擊 AddOther 按鈕泌绣,再按組合鍵 command+shift+G 在Go to the folder中填上路徑 /usr/local/lib
點(diǎn)擊go
把所有l(wèi)ibopencv前綴dylib后綴的都選中加進(jìn)工程
把下面代碼覆蓋你的main.cpp 文件
//
// main.cpp
// test_OpenCV
//
// Created by 朱松澤 on 2017/8/16.
// Copyright ? 2017年 com.gdtech. All rights reserved.
//
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
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;
}
運(yùn)行工程,上面代碼效果從Mac自帶的攝像頭讀入圖像预厌,然后canny提取了邊緣然后顯示阿迈。
效果如圖
代碼來(lái)自文章:http://blog.csdn.net/zhoufan900428/article/details/45968125
安裝配置過(guò)程參考了文章
http://blog.devtang.com/2012/10/27/use-opencv-in-ios/