Skip to content

Instantly share code, notes, and snippets.

@mlhales
Last active March 8, 2023 16:34
Show Gist options
  • Save mlhales/5785725 to your computer and use it in GitHub Desktop.
Save mlhales/5785725 to your computer and use it in GitHub Desktop.
Install OpenCV with Nvidia CUDA, and Homebrew Python support on the Mac.

#OpenCV on the Mac with Nvidia CUDA and Python support using Homebrew

##Prerequisites

  • XCode
  • CUDA
  • Homebrew
    • CMake
    • GCC 4.5+
    • Python

##Installing

###Install XCode from the Mac App Store.

Once installed, from the preferences, install the command line tools. This will give you the basic compilers that are needed for doing anything.

###Install CUDA

Download and run the .pkg file from Nvidia

Make sure to remember and add the CUDA libraries to your path. The best way is too add a couple of lines to your .bashrc

export PATH=/usr/local/cuda/bin:$PATH
export DYLD_LIBRARY_PATH=/usr/local/cuda/lib:$DYLD_LIBRARY_PATH

One note about .bashrc. I usually put my environment variables in my .bashrc, then source that into my .bash_profile. that way I get them both in interactive shell and login shell environments. My .bash_profile looks like this:

if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi

###Install Homebrew

http://mxcl.github.io/homebrew/

In your terminal paste the following:

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

You should probably make sure that your /usr/local/bin comes before your /usr/bin in your $PATH so that your homebrew executables are found first. An easy way is to edit your /etc/paths file. Mine looks like this:

/usr/local/bin
/usr/local/sbin
/usr/bin
/bin
/usr/sbin
/sbin

###Install Cmake

brew install cmake

###Install Python

brew install python

Make sure that the Homebrew Python is available in your terminal.

which python

Update your $PYTHONPATH variable for your brewed site packages. Add the following to your .bashrc

export PYTHONPATH="/usr/local/lib/python2.7/site-packages:$PYTHONPATH"

Install numpy

pip install numpy

###Install GCC 4.5

OpenCV CUDA modules will not build with the GCC 4.2 available with XCode on the Mac. Apparently GCC 4.5 works the best.

brew tap homebrew/versions
brew install gcc45 --enable-cxx --enable-objc --enable-objcxx --enable-multilib

On my machine this takes about 20 minutes.

###Install OpenCV

Download it from here

Untar it and cd into it. There are 5 compiler options that are going to give you a million warnings, so we're going to go into the cmake file and comment them out now.

Use your favorite editor to open cmake/OpenCVCompilerOptions.cmake. Find the following lines, and comment them out by putting a # in front.

#add_extra_compiler_option(-Wmissing-prototypes)
#add_extra_compiler_option(-Wstrict-prototypes)

Then just a little further down:

#add_extra_compiler_option(-Wno-narrowing)
#add_extra_compiler_option(-Wno-delete-non-virtual-dtor)
#add_extra_compiler_option(-Wno-unnamed-type-template-args)

Make a build directory, cd into it and configure cmake.

mkdir build
cd build
ccmake ..

In ccmake, change following settings:

CUDA_HOST_COMPILER=/usr/local/bin/gcc-4.5

This will compile CUDA with GCC, instead of Clang.

PYTHON_INCLUDE_DIR=/usr/local/Frameworks/Python.framework/Headers
PYTHON_LIBRARY=/usr/local/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib
PYTHON_PACKAGES_PATH=lib/python2.7/site-packages

This will use your Homebrew Python, instead of the system Python.

I turned off building the documentation, since I just use the online docs, and I don't want to mess with integrating LaTex with the process.

BUILD_DOCS=OFF

I had to add a compiler option to avoid a linker error when linking the opencv_gpu module.

CMAKE_CXX_FLAGS=-fno-inline

I also set my install director location to /usr/local/opencv, so Homebrew won't complain about un-brewed software in the /usr/local/bin directory.

CMAKE_INSTALL_PREFIX=/usr/local/opencv

Reconfigure and generate, then build OpenCV. Make sure you use the -j flag to build with multiple cpu's, otherwise this will take hours and hours.

make -j8

Finally install it.

make install

You'll want to update your python path again to be able to use the OpenCV Python classes, and update your path to find the installed opencv executables.

export PYTHONPATH=/usr/local/lib/python2.7/site-packages:/usr/local/opencv/lib/python2.7/site-packages:$PYTHONPATH
export PATH=/usr/local/cuda/bin:/usr/local/opencv/bin:$PATH
export DYLD_LIBRARY_PATH=/usr/local/cuda/lib:/usr/local/opencv/lib:$DYLD_LIBRARY_PATH

##Test it

Restart your terminal window to make sure your update paths work.

Start the python interpreter, and import cv2. If it works, you have a working OpenCv installation.

@stayHF
Copy link

stayHF commented Jul 11, 2019

I removed this

-D CMAKE_CXX_FLAGS=-fno-inline

but now the error is

[  3%] Building C object 3rdparty/libjasper/CMakeFiles/libjasper.dir/jp2_cod.c.o
nvcc fatal   : GNU C/C++ compiler is no longer supported as a host compiler on Mac OS X.
CMake Error at cuda_compile_generated_gpu_mat.cu.o.cmake:208 (message):
  Error generating
  /Users/stefankaros/Downloads/opencv-master/build/modules/core/CMakeFiles/cuda_compile.dir/src/cuda/./cuda_compile_generated_gpu_mat.cu.o


make[2]: *** [modules/core/CMakeFiles/cuda_compile.dir/src/cuda/cuda_compile_generated_gpu_mat.cu.o] Error 1
make[1]: *** [modules/core/CMakeFiles/opencv_core.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....

and compiling with

-D CUDA_HOST_COMPILER=/usr/local/cuda/bin/nvcc

gives this

nvcc fatal   : Unknown option 'feigned-char'
CMake Error at cuda_compile_generated_gpu_mat.cu.o.cmake:208 (message):
  Error generating
  /Users/stefankaros/Downloads/opencv-master/build/modules/core/CMakeFiles/cuda_compile.dir/src/cuda/./cuda_compile_generated_gpu_mat.cu.o


make[2]: *** [modules/core/CMakeFiles/cuda_compile.dir/src/cuda/cuda_compile_generated_gpu_mat.cu.o] Error 1
make[1]: *** [modules/core/CMakeFiles/opencv_core.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....

Which version of Mac OS are you using? After you cmake, what does your opencv-master/build/CMakeFiles/CMakeError.log say?

!!! use /usr/bin/gcc instead of gnu gcc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment