Install OpenCV 2 on Mac OSX

转载:http://www.learnopencv.com/install-opencv-3-on-yosemite-osx-10-10-x/

Life was good the last time you installed OpenCV on your Mac. You instantly brewed it and thanked the good folks at Homebrew. All it took were these few commands.

Install OpenCV 2 on Mac OSX

1
2
brew tap homebrew /science
brew install opencv

Set up Python by creating a couple of symlinks.

1
2
3
cd /Library/Python/2 .7 /site-packages/
ln -s /usr/local/Cellar/opencv/2 .4.9 /lib/python2 .7 /site-packages/cv .py cv.py
ln -s /usr/local/Cellar/opencv/2 .4.9 /lib/python2 .7 /site-packages/cv2 .so cv2.so

Install OpenCV 3 on Mac OSX with brew

You can now install OpenCV 3 using brew. See the next section to install from source. Life is good again!

1
2
brew tap homebrew /science
brew install opencv3

You can choose the different options you can use with install in the subsections below. Here is what I recommend

1
2
3
4
# Easy install for beginners
brew install opencv3 --with-contrib
# For intermediate and advanced users.
brew install opencv3 --with-contrib --with-cuda --with-ffmpeg --with-tbb --with-qt5

OpenCV3 brew install options

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
--32-bit
     Build 32-bit only
--c++11
     Build using C++11 mode
--with-contrib
     Build "extra" contributed modules
--with-cuda
     Build with CUDA v7.0+ support
--with-ffmpeg
     Build with ffmpeg support
--with-gphoto2
     Build with gphoto2 support
--with-gstreamer
     Build with gstreamer support
--with-jasper
     Build with jasper support
--with-java
     Build with Java support
--with-libdc1394
     Build with libdc1394 support
--with-opengl
     Build with OpenGL support (must use --with-qt5)
--with-openni
     Build with openni support
--with-openni2
     Build with openni2 support
--with-python3
     Build with python3 support
--with-qt
     Build the Qt4 backend to HighGUI
--with-qt5
     Build the Qt5 backend to HighGUI
--with-quicktime
     Use QuickTime for Video I /O instead of QTKit
--with-tbb
     Enable parallel code in OpenCV using Intel TBB
--without-eigen
     Build without eigen support
--without-numpy
     Use a numpy you've installed yourself instead of a Homebrew-packaged numpy
--without-opencl
     Disable GPU code in OpenCV using OpenCL
--without-openexr
     Build without openexr support
--without-python
     Build without Python support
--without-tests
     Build without accuracy & performance tests
--HEAD
     Install HEAD version

Build OpenCV 3 from source with CUDA support

I had the following goals while building OpenCV 3.0.

  1. Not mess up OpenCV 2.4 installation because I still need it for my other projects.
  2. Include opencv_contrib . This repository of new and non-free algorithms is a hidden gem in OpenCV.
  3. Build with CUDA support. This applies only if you have a CUDA enabled GPU.
  4. Be able to use pkg-config for compiling code from the command line.  E.g. 
    1
      g++ -ggdb `pkg-config --cflags --libs opencv3` test .cpp -o   test .out

1. Download OpenCV 3.0

Download the source from the following link

https://github.com/Itseez/opencv/archive/3.0.0.zip

Alternatively, you can get it directly from the source.

1
2
3
git clone https: //github .com /Itseez/opencv .git
cd opencv
git checkout tags /3 .0.0

2. Configure CMAKE

Inside the opencv directory created in the last step, create a build directory.

1
2
cd /full/path/to/opencv
mkdir build

Instruct CMAKE to install inside the build directory and not the default directory /usr/local so that our OpenCV 2.X installation is not messed up.

With opencv_contrib

opencv_contrib is a repository that contains cutting edge algorithms, some of which are not fully tested, and some of which are not free. It may not be suitable for production, but is excellent for learning new stuff. Please note that this step is optional.

You can download opencv_contrib source from

https://github.com/Itseez/opencv_contrib/archive/3.0.0.zip

Alternatively, you can clone it directly from github

1
2
3
git clone https: //github .com /Itseez/opencv_contrib .git
cd opencv_contrib
git checkout tags /3 .0.0

To compile with opencv_contrib you need to use the CMAKE flagOPENCV_EXTRA_MODULES_PATH to specify the location of opencv_contrib. So, if you want to include opencv_contrib, use the

1
cmake -D OPENCV_EXTRA_MODULES_PATH=full /path/to/opencv_contrib/modules < /strong >

instead of just cmake in the instructions below.

Without CUDA support

 
cmake -D WITH_CUDA=OFF -D CMAKE_INSTALL_PREFIX= /full/path/to/opencv/build -D CMAKE_BUILD_TYPE=RELEASE   ..

We are ready to build. Go to step 3.

With CUDA support

To build OpenCV CUDA library you need to make sure

  1. You have a CUDA enabled Nvidia Graphics Card. You are unlikely to have a CUDA enabled card for lower end macs.  Follow the instructions here to find the card you have, and check here to see if your card is supported.
  2. Download and install CUDA Toolkit if you have a CUDA enabled card. You may have to add the following to your .bash_profile or .profile
     
    export DYLD_FALLBACK_LIBRARY_PATH= /usr/local/cuda/lib/ :$DYLD_FALLBACK_LIBRARY_PATH

Now follow the instructions below if you have a CUDA enabled card, and you have installed CUDA Toolkit

 
cmake -D WITH_CUDA=ON -D CMAKE_INSTALL_PREFIX= /full/path/to/opencv/build -D CMAKE_BUILD_TYPE=RELEASE   ..

Proceed to step 3, but if you encounter errors look for them in the section below.

Errors you may encounter

CUDA 6.5 errors

Note that CUDA 7.0 is available and there is no reason to use CUDA 6.5. However, let’s say you have a good reason and you try to build with CUDA 6.5, you will receive the following error.

 
Linking CXX executable ../../.. /bin/opencv_test_cudev Undefined symbols for architecture x86_64:

The reason for this error is that clang++ uses libc++ by default while CUDA 6.5 Toolkit was built using libstdc++. We need to modify two files to ensure OpenCV is compiled with libstdc++

In /full/path/to/opencv/cmake/OpenCVCompilerOptions.cmake find the line (e.g. it could line 23 )

23
set (OPENCV_EXTRA_FLAGS "" )

and replace it with

23
set (OPENCV_EXTRA_FLAGS " -stdlib=libstdc++" )

Similarly find the line ( around line number 28 )

28
set (OPENCV_EXTRA_EXE_LINKER_FLAGS "" )

and replace it with

28
set (OPENCV_EXTRA_EXE_LINKER_FLAGS " -stdlib=libstdc++" )

In /full/path/to/opencv/cmake/OpenCVDetectCUDA.cmake find the line

41
set (NVCC_FLAGS_EXTRA "" )

and replace it with

41
set (NVCC_FLAGS_EXTRA "-Xcompiler -stdlib=libstdc++; -Xlinker -stdlib=libstdc++" )

Unsupported gpu architecture error

You may receive this error

1
Unsupported gpu architecture 'compute_11' CMake Error at

According to OpenCV documentation,

“NVIDIA* compiler enables generating binary code (cubin and fatbin) and intermediate code (PTX). Binary code often implies a specific GPU architecture and generation, so the compatibility with other GPUs is not guaranteed. PTX is targeted for a virtual platform that is defined entirely by the set of capabilities or features. Depending on the selected virtual platform, some of the instructions are emulated or disabled, even if the real hardware supports all the features.

At the first call, the PTX code is compiled to binary code for the particular GPU using a JIT compiler. When the target GPU has a compute capability (CC) lower than the PTX code, JIT fails.”

So you can fix this by specifying the right GPU architecture for your machine using cmake flags CUDA_ARCH_BIN and CUDA_ARCH_PTX. I used the following

1
-D CUDA_ARCH_BIN=3.2 -D CUDA_ARCH_PTX=3.2

3. Build OpenCV

Use make to build and install. Note the library will be installed inside the builddirectory.

1
2
3
make
make install
export DYLD_LIBRARY_PATH= /full/path/to/opencv/build/lib :$DYLD_LIBRARY_PATH

Copy the pkg-config file opencv.pc to /usr/local/lib/pkgconfig/opencv3.pc so that you do not mess up your OpenCV 2.x config file.

 
cp lib /pkgconfig/opencv .pc /usr/local/lib/pkgconfig/opencv3 .pc

4. Test installation

Basic tests

We can run some sample code located at opencv/samples/cpp.

1
2
3
4
5
6
7
cd /full/path/to/opencv/samples/cpp
 
# If you built it with no CUDA support or with CUDA 7 or above
g++ -ggdb `pkg-config --cflags --libs opencv3` facedetect.cpp -o /tmp/test && /tmp/test
 
# If you built it with CUDA 6.5
g++ -ggdb `pkg-config --cflags --libs opencv3` -stdlib=libstdc++ facedetect.cpp -o /tmp/test && /tmp/test

CUDA tests

To test CUDA we can try a few examples located at opencv/samples/gpu.

1
2
3
4
5
6
7
8
export DYLD_FALLBACK_LIBRARY_PATH= /usr/local/cuda/lib/ :$DYLD_FALLBACK_LIBRARY_PATH
cd /full/path/to/opencv/samples/gpu
 
# If you built it with CUDA 7 or above
g++ -ggdb `pkg-config --cflags --libs opencv3` hog.cpp -o /tmp/hog && /tmp/hog
 
# If you built it with CUDA 6.5
g++ -ggdb `pkg-config --cflags --libs opencv3` -stdlib=libstdc++ hog.cpp -o /tmp/hog && /tmp/hog

5. Setting up Python

Open a terminal and run the following commands.

1
2
export DYLD_FALLBACK_LIBRARY_PATH= /full/path/to/opencv/build/lib :$DYLD_FALLBACK_LIBRARY_PATH
export PYTHONPATH= /full/path/to/opencv/build/lib/python2 .7 /site-packages :$PYTHONPATH

This ensures the OpenCV 3 is being used on the current terminal. We can verify this by typing the following command on the terminal.

 
1
python -c "import cv2;  print cv2.__version__"
#The output should be 3.0.0

To switch back to your OpenCV 2.x, simply open a new terminal.

Subscribe

If you liked this article, please subscribe to our newsletter and receive a free
Computer Vision Resource guide. In our newsletter we share OpenCV tutorials and examples written in C++/Python, and Computer Vision and Machine Learning algorithms and news.


你可能感兴趣的:(Install OpenCV 2 on Mac OSX)