日常开发主要是用 MacOS,今天介绍下 Mac 下 OpenCV 开发环境搭建。
1. 源码方式编译
执行下列命令编译安卓 OpenCV 库:
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
cd~ / opencv
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
make -j6#并行运行6个作业
sudo make install
复制代码
安装好 OpenCV 后,在/usr/local/lib
下能看到这样的文件这说明已经安装成功了
上述是最新版本 OpenCV 编译命令。OpenCV2.0 和 3.0 编译命令略有不同:
opencv2 安装
下载 OpenCV2: 2.4.13.6
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local/opencv/opencv2 ..
make -j8
sudo make install
复制代码
opencv3 安装
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local/opencv/opencv3 -D BUILD_opencv_world=ON -D WITH_GSTREAMER=OFF -D OPENCV_ENABLE_NONFREE=ON ..
make -j8
sudo make install
复制代码
查看编译安装的库:
ls /usr/local/opencv3/lib
libopencv_calib3d.3.4.12.dylib libopencv_imgcodecs.3.4.dylib libopencv_stitching.dylib
libopencv_calib3d.3.4.dylib libopencv_imgcodecs.dylib libopencv_superres.3.4.12.dylib
libopencv_calib3d.dylib libopencv_imgproc.3.4.12.dylib libopencv_superres.3.4.dylib
libopencv_core.3.4.12.dylib libopencv_imgproc.3.4.dylib libopencv_superres.dylib
libopencv_core.3.4.dylib libopencv_imgproc.dylib libopencv_video.3.4.12.dylib
libopencv_core.dylib libopencv_ml.3.4.12.dylib libopencv_video.3.4.dylib
libopencv_dnn.3.4.12.dylib libopencv_ml.3.4.dylib libopencv_video.dylib
libopencv_dnn.3.4.dylib libopencv_ml.dylib libopencv_videoio.3.4.12.dylib
libopencv_dnn.dylib libopencv_objdetect.3.4.12.dylib libopencv_videoio.3.4.dylib
libopencv_features2d.3.4.12.dylib libopencv_objdetect.3.4.dylib libopencv_videoio.dylib
libopencv_features2d.3.4.dylib libopencv_objdetect.dylib libopencv_videostab.3.4.12.dylib
libopencv_features2d.dylib libopencv_photo.3.4.12.dylib libopencv_videostab.3.4.dylib
libopencv_flann.3.4.12.dylib libopencv_photo.3.4.dylib libopencv_videostab.dylib
libopencv_flann.3.4.dylib libopencv_photo.dylib libopencv_world.3.4.12.dylib
libopencv_flann.dylib libopencv_shape.3.4.12.dylib libopencv_world.3.4.dylib
libopencv_highgui.3.4.12.dylib libopencv_shape.3.4.dylib libopencv_world.dylib
libopencv_highgui.3.4.dylib libopencv_shape.dylib pkgconfig
libopencv_highgui.dylib libopencv_stitching.3.4.12.dylib python2.7
libopencv_imgcodecs.3.4.12.dylib libopencv_stitching.3.4.dylib
复制代码
2. Brew 工具安装
可以通过brew search opencv
:
$ brew search opencv
homebrew/science/opencv
复制代码
安装后路径是:/usr/local/opt/opencv3,我们可以设置一个 OPENCV3_HOME 环境变量,方便以后快速打开这个文件夹。在 lib 目录下是 opencv 的库,在 include 目录下包含了两个子目录:opencv 和 opencv2,opencv 里面包含了 opencv1.x 的核心头文件,opencv2 安装模块功能组织,包括核心模块、图像处理模块、2D 功能模块、高层 GUI 图像用户界面模块、机器学习模块等。
3. XCode 环境搭建
打开 Xcode,新建一个 command line 工程:注意语言选择 C++。 接下来先来配置 xcode 再来写代码。 最左边选中工程,然后右边选中 Targets,再 BuildSettings 下,右边搜索框里输入 search,找到 Search Paths 设置项。在 Header Search Paths 里输入:/usr/local/include
在 Library Search Paths 里输入:/usr/local/lib
接着在 Build Phases 里找到 Link Binary With Libraries,点击+号 ,选择 add other,然后按下/键,输入 lib 的路径/usr/local/lib
,然后就是选择 OpenCV 的库了,用哪个添加哪个, 在 main.cpp 里输入以下内容,实现显示一张照片及显示灰度化后的:
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using std::string;
int main(int argc, const char * argv[]) {
string path = "./test.jpg";
Mat image = imread(path);
namedWindow("origin");
imshow("origin", image);
Mat gray;
cvtColor(image, gray, COLOR_RGBA2GRAY);
namedWindow("gray");
imshow("gray", gray);
waitKey(0);
return 0;
}
复制代码
4. python 环境搭建
python 可以使用 pip 工具安装:
pip install --upgrade setuptools
pip install numpy Matplotlib
pip install opencv-python
复制代码
pencv 环境已经整好,就是这么简单。只需要 numpy、Matplotlib、opencv-python 三个包,都不大很快就可以下好
5. python 测试代码
import cv2
import numpy as np
lenna = cv2.imread("2.jpeg")
row, col, channel = lenna.shape
lenna_gray = np.zeros((row, col))
for r in range(row):
for l in range(col):
lenna_gray[r, l] = 1 / 3 * lenna[r, l, 0] + 1 / 3 * lenna[r, l, 1] + 1 / 3 * lenna[r, l, 2]
cv2.imshow("lenna_gray", lenna_gray.astype("uint8"))
if cv2.waitKey()
复制代码
6. 总结
本文介绍了 OpenCV C++开发环境和 Python 开发环境的搭建。C++可以使用源码编译方式或者 Brew 方式安装;Python 可以使用 pip 安装 opencv 库。
本文还介绍了几个 OpenCV 简单的接口:
imread:解析图片问 OpenCV Mat 结构;
imshow:显示图像数据;
cvtColor:图像转换。
从 C++和 Python 示例看,两种语言 API 都很类似。
评论