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
brew tap homebrew/science
brew install opencv
Set up Python by creating a couple of symlinks.
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!
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
# 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
Troubleshooting
- ld: library not found for -lippicv : This just means that ippicv is not found inside /usr/local/lib. Follow the instructions below to fix it.
# Find ippicv find /usr/local -name "libippicv.a" # For me it is /usr/local/Cellar/opencv3/3.1.0_3/share/OpenCV/3rdparty/lib/libippicv.a # Make a symlink to /usr/local/lib ln -s /some/path/OpenCV/3rdparty/lib/libippicv.a /usr/local/lib/ # I used ln -s /usr/local/Cellar/opencv3/3.1.0_3/share/OpenCV/3rdparty/lib/libippicv.a /usr/local/lib/
OpenCV3 brew install options
--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.
- Not mess up OpenCV 2.4 installation because I still need it for my other projects.
- Include opencv_contrib . This repository of new and non-free algorithms is a hidden gem in OpenCV.
- Build with CUDA support. This applies only if you have a CUDA enabled GPU.
- Be able to use pkg-config for compiling code from the command line. E.g.
g++ -ggdb facedetect.cpp -o facedetect `pkg-config --cflags --libs opencv`
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.
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.
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
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 flag OPENCV_EXTRA_MODULES_PATH to specify the location of opencv_contrib. So, if you want to include opencv_contrib, use the
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
- 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.
- 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 )
set(OPENCV_EXTRA_FLAGS "")
and replace it with
set(OPENCV_EXTRA_FLAGS " -stdlib=libstdc++")
Similarly find the line ( around line number 28 )
set(OPENCV_EXTRA_EXE_LINKER_FLAGS "")
and replace it with
set(OPENCV_EXTRA_EXE_LINKER_FLAGS " -stdlib=libstdc++")
In /full/path/to/opencv/cmake/OpenCVDetectCUDA.cmake find the line
set(NVCC_FLAGS_EXTRA "")
and replace it with
set(NVCC_FLAGS_EXTRA "-Xcompiler -stdlib=libstdc++; -Xlinker -stdlib=libstdc++")
Unsupported gpu architecture error
You may receive this error
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
-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 build directory.
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.
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.
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.
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.
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.
Fantastic! I’ll give this a try ASAP. Thanks!
You are welcome. Let me know how it goes.
Thanks for the instructions. However, when I tried following the steps above (without CUDA), python libraries were not compiled. Don’t we have to specify python bindings initially, like cmake -D PYTHON_EXECUTABLE… etc.?
I am building OpenCV3 and I have python 2.7 already installed. I don’t care about preserving OpenCV2. thanks.
It automatically detects if you have python installed on your system. If you do, then python libraries are installed by default. Are you sure there is nothing inside /full/path/to/opencv/build/lib/python2.7/site-packages ?
Its not doing it for some reason. In the CMakeCache.txt file I can see the python paths detected properly.
For what it’s worth, I have the same issue – it’s not building python bindings.
Hi thanks! I followed your instruction and cmake, make seem to go well. but as tried to run the sample test step 4, i got the following error.
Undefined symbols for architecture x86_64:
“cv::CascadeClassifier::detectMultiScale(cv::_InputArray const&, std::vector<cv::Rect_, std::allocator<cv::Rect_ > >&, double, int, int, cv::Size_, cv::Size_)”, referenced from:
detectAndDraw(cv::Mat&, cv::CascadeClassifier&, cv::CascadeClassifier&, double, bool) in facedetect-d34432.o
“cv::_InputArray::getMatVector(std::vector<cv::Mat, std::allocator >&) const”, referenced from:
vtable for cv::_InputOutputArray in facedetect-d34432.o
“cv::_InputArray::getUMatVector(std::vector<cv::UMat, std::allocator >&) const”, referenced from:
vtable for cv::_InputOutputArray in facedetect-d34432.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Looks like the wrong c++ library is being linked to. If you built the OpenCV using -stdlib=libstdc++ , you have to build your code using that flag. If you didn’t use that flag, you shouldn’t use it while compiling your code. Please let me know if this helped.
I have the same issue now. If i remove the flag it says library is not found. I’m a beginner, and I’m completely lost here.
Hi, very good instructions – thanks!
But using the zipped source distribution still failed to build on my Macbook pro running Mavericks:
755 warnings generated.
Linking CXX shared library ../../lib/libopencv_viz.dylib
Undefined symbols for architecture x86_64:
“vtkObjectBase::PrintHeader(std::ostream&, vtkIndent)”, referenced from:
vtable for cv::viz::Viz3d::VizImpl::TimerCallback in vizimpl.cpp.o
vtable for cv::viz::Viz3d::VizImpl::ExitCallback in vizimpl.cpp.o
vtable for cv::viz::vtkCloudMatSink in vtkCloudMatSink.cpp.o
vtable for cv::viz::vtkCloudMatSource in vtkCloudMatSource.cpp.o
vtable for cv::viz::vtkImageMatSource in vtkImageMatSource.cpp.o
vtable for cv::viz::vtkOBJWriter in vtkOBJWriter.cpp.o
vtable for cv::viz::vtkTrajectorySource in vtkTrajectorySource.cpp.o
…
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [lib/libopencv_viz.3.0.0.dylib] Error 1
make[1]: *** [modules/viz/CMakeFiles/opencv_viz.dir/all] Error 2
make: *** [all] Error 2
Any clues?
BR /Anders
Are you sure you have used the compiler flag -stdlib=libstdc++ when you built the library and when you built the app ? You can choose to build the opencv library without the flag, and build your app without the flag, but if you do use the flag while building the opencv library you have to use the flag while building your app as well.
Please let me know if it worked.
I got this error
-bash: syntax error near unexpected token `OPENCV_EXTRA_FLAGS’
Difficult to say with just this information. Could you please share the OpenCVCompilerOptions.cmake file.
where could I share it?
I run it using terminal and I got this :
Last login: Sat Apr 25 22:07:40 on ttys000
Omamas-MacBook-Pro:~ omama$ /Library/Python/2.7/site-packages/cv/cmake/OpenCVCompilerOptions.cmake ; exit;
/Library/Python/2.7/site-packages/cv/cmake/OpenCVCompilerOptions.cmake: line 1: syntax error near unexpected token `(‘
/Library/Python/2.7/site-packages/cv/cmake/OpenCVCompilerOptions.cmake: line 1: `if(MINGW OR (X86 AND UNIX AND NOT APPLE))’
logout
[Process completed]
I also have a problem on the pip, when I am trying to download it from the preference from Pycharm. I executed this command:
pip install –user pyopencv
then, I got
DEPRECATION: –no-install, –no-download, –build, and –no-clean are deprecated. See https://github.com/pypa/pip/issues/906.
Downloading/unpacking pyopencv
Could not find a version that satisfies the requirement pyopencv (from versions: 2.0.wr1.0.1-demo, 2.0.wr1.0.1, 2.0.wr1.1.0, 2.1.0.wr1.0.0, 2.1.0.wr1.0.1, 2.1.0.wr1.0.2, 2.1.0.wr1.1.0, 2.1.0.wr1.2.0)
Some externally hosted files were ignored (use –allow-external to allow).
Cleaning up…
No distributions matching the version for pyopencv
Storing debug log for failure in /Users/omama/.pip/pip.log
The suggested solution from Pycharm is
Try to run this command from the system terminal. Make sure that you use the correct version of ‘pip’ installed for your Python interpreter located at ‘/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7’.
Thanks a lot
PyOpenCV does not support OpenCV 3 yet. If you are just trying to install opencv 2, you can do so easily using brew. You can install brew using
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
and then install opencv using the following on the command line.
brew tap homebrew/science
brew install opencv
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
I was able to follow your post for installing OpenCV 3.0.0-rc1 on OS Yosemite. However, I am not sure how to configure CMAKE to run the examples. I set “export OPENCV_DIR=/path/to/opencv/build” and ran “cmake .” in the samples/cpp/example_cmake directory, but I got the following error :
“Found package configuration file : (path/to/opencv/build)/OpenCVConfig.cmake but it set OpenCV_FOUND to FALSE so package OpenCV is considered to be not found”
How can one run opencv 3 projects with cmake – how do you do it? Thanks!
I compile and run the examples using g++ and pkg-config . Check this link
https://learnopencv.com/how-to-compile-opencv-sample-code/
This is awesome, the CUDA tips saved me hours of trying to fix it myself. Thanks! 🙂
Glad that it was helpful.
Thank you for posting this! As a long-time Linux developer setting up an OS X dev environment for the first time, getting OpenCV to compile under Yosemite was a huge problem. I had seen some discussion about the C++ libraries being the problem, but your step-by-step instructions made it super easy. Great stuff!
Thanks a bunch!
I have got this error,
make[2]: *** [modules/core/CMakeFiles/opencv_core_pch_dephelp.dir/opencv_core_pch_dephelp.cxx.o] Error 1
make[1]: *** [modules/core/CMakeFiles/opencv_core_pch_dephelp.dir/all] Error 2
make: *** [all] Error 2
Do you have any idea which part i could fix it? Thank you very much….
Thats a tough one man. I don’t have an answer.
Works like a charm with latest CUDA 7 and latest opencv on latest release Yosemite. Had to start with fresh clone of opencv. Sample build option “-stdlib=libstdc++” didn’t worked and proved unneeded. Thanks!
Thanks! You are right about CUDA 7. I will update the post. Thanks for pointing this out.
Hi Satya, thank you very much for this blog post.
I’m trying to install openCV 3.0.0 on OS X 10.10.3 with CUDA support after cloning the github account and checking out the 3.0.0 branch as suggested above. Anyway, I’m getting linking errors once I get to about 75% (with make -j8). Any idea about what’s going on and how to fix it would be very helpful.
Scanning dependencies of target opencv_cudaarithm
[ 75%] [ 75%] [ 75%] [ 75%] Building CXX object modules/cudaarithm/CMakeFiles/opencv_cudaarithm.dir/src/element_operations.cpp.o
Building CXX object modules/cudaarithm/CMakeFiles/opencv_cudaarithm.dir/src/core.cpp.o
Building CXX object modules/cudaarithm/CMakeFiles/opencv_cudaarithm.dir/src/arithm.cpp.o
Building CXX object modules/cudaarithm/CMakeFiles/opencv_cudaarithm.dir/src/reductions.cpp.o
Linking CXX shared library ../../lib/libopencv_cudaarithm.dylib
Undefined symbols for architecture x86_64:
“std::__throw_length_error(char const*)”, referenced from:
std::vector<cv::cuda::GpuMat, std::allocator >::_M_fill_insert(__gnu_cxx::__normal_iterator<cv::cuda::GpuMat*, std::vector<cv::cuda::GpuMat, std::allocator > >, unsigned long, cv::cuda::GpuMat const&) in cuda_compile_generated_split_merge.cu.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [lib/libopencv_cudaarithm.3.0.0.dylib] Error 1
make[1]: *** [modules/cudaarithm/CMakeFiles/opencv_cudaarithm.dir/all] Error 2
My bad, I realised I was using The CUDA Toolkit 5.5. After upgrading the CUDA Toolkit here https://developer.nvidia.com/cuda-downloads?sid=859491 to CUDA 7.0 it all compiles fine with the given instructions.
Cheers.
Glad it worked out! I am very interested in knowing what kind of performance boost you got with CUDA. If you have the time, please share your experience.
Cool. It’s hard to quantify right now. I feel I need to refactor the code and compare it with OpenCL performance.
Hey, thanks for this post, it’s a really helpful tutorial!
I’m having some problem with python though. I built everything from checking out 3.0.0 from github. But there doesn’t seem to be a lib/python2.7 directory (or any other python – lib is there and populated though). Is there a way to enable python support explicitly? I have python 2 and 3 installed with homebrew.
Thanks for any suggestion!
Hi, Satya, and thanks for the helpful tutorial.
However, after I followed the steps to install both opencv2 and opencv3 (on mac 10.10, without CUDA), I tried to run the test, but gave me this error message:
g++ -ggdb `pkg-config –cflags –libs opencv3` facedetect.cpp -o /tmp/test && /tmp/test
-bash: pkg-config: command not found
facedetect.cpp:1:10: fatal error: ‘opencv2/objdetect.hpp’ file not found
#include “opencv2/objdetect.hpp”
^
1 error generated.
And when I looked in my opencv3folder/include/opencv2/ directory, there is indeed no objdetect.hpp but only a single opencv.cpp file there.
There is a opencv3folder/build/include/opencv2/objectdetect.hpp file.
Am I looking at the right place? Any ideas on what went wrong?
Thanks!
Yi
You are my new hero, Satya! After several days of unsuccessfully trying to get opencv 3.0.0 with contribs to work for Python on Yosemite, your blog (with a little modification) finally led to the solution! Full description here: http://wp.me/P3ALAE-82
I followed this tutorial and it was successful though to the end. However, when I tried to use OpenCV3.0.0 with the java bindings, I got the following error message during loading the native library:
Exception in thread “main” java.lang.UnsatisfiedLinkError: /Users/Michael/Development/DevelopmentTools/opencv-3.0-2.0/build/share/OpenCV/java/libopencv_java300.dylib: dlopen(/Users/Michael/Development/DevelopmentTools/opencv-3.0-2.0/build/share/OpenCV/java/libopencv_java300.dylib, 1): Library not loaded: lib/libopencv_photo.3.0.dylib
Referenced from: /Users/Michael/Development/DevelopmentTools/opencv-3.0-2.0/build/share/OpenCV/java/libopencv_java300.dylib
Reason: image not found
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1938)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1854)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at Hello.main(Hello.java:9)
Could anybody help me with this?
I realize this is late to the party, but it appears that the system integrity protection changes in OS X El Capitan are breaking much of the install process. Specifically, brew can’t install version 2 (symlinks can’t be created, among other things), and then when it’s time to build a sample app pkg-config is nowhere to be found. Any ideas how to address these new issues?
Hello!
Thank you for the interesting post. But I can not compile opencv3. After install mac os x 10.11 the not compile opencv3.
. . .
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [lib/libopencv_videoio.3.0.0.dylib] Error 1
make[1]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2
make: *** [all] Error 2
What do you think, in what could be the problem?
Thank you!
Actually now you can simply do
brew install opencv3
If you do not have brew installed, I highly recommend it. Get it from.
http://brew.sh/
thank!!!! )))))
it has helped install opencv3 – /usr/local/Cellar/opencv3/3.0.0/
but there was another problem
in Python 2.7.10 print
>>> import cv2
Traceback (most recent call last):
File “”, line 1, in
ImportError: dlopen(/usr/local/lib/python2.7/site-packages/cv2.so, 2): Library not loaded: lib/libopencv_shape.3.0.dylib
Referenced from: /usr/local/lib/python2.7/site-packages/cv2.so
Reason: unsafe use of relative rpath lib/libopencv_shape.3.0.dylib in /usr/local/lib/python2.7/site-packages/cv2.so with restricted binary
and in Python 3.5.0 print
>>> import cv2
Traceback (most recent call last):
File “”, line 1, in
ImportError: No module named ‘cv2’
how to fix this probdemu?
thank )
add these to your .bash_profile
export DYLD_LIBRARY_PATH=/usr/local/Cellar/opencv3/3.1.0_3/lib:$DYLD_LIBRARY_PATH
export DYLD_FALLBACK_LIBRARY_PATH=/usr/local/Cellar/opencv3/3.1.0_3/lib:$DYLD_FALLBACK_LIBRARY_PATH
export PYTHONPATH=/usr/local/Cellar/opencv3/3.1.0_3/lib/python2.7/site-packages:$PYTHONPATH
Thanks for the guide. I wanted to add my notes in case they help others who may have encountered the same problem I had.
“””
This has only been tested on my computer running OSX 10.11.1. YMMV
This guide assumes you have already installed homebrew and have followed instructions for installing OpenCV 3 with brew in this post
“””
#After installing opencv3 using homebrew, I ran into this error when trying to apply a background subtraction algorithm (cv2.createBackgroundSubtractorMOG2()):
OpenCV Error: Assertion failed (The data should normally be NULL!) in allocate, file /tmp/opencv320151224-28113-1l68278/opencv-3.1.0/modules/python/src2/cv2.cpp, line 163
Traceback (most recent call last):
File “backgroundsubtractorMOG.py”, line 11, in
fgmask = fgbg.apply(frame)
cv2.error: /tmp/opencv320151224-28113-1l68278/opencv-3.1.0/modules/python/src2/cv2.cpp:163: error: (-215) The data should normally be NULL! in function allocate
#I was unable to locate the cv2.cpp file to follow the instructions I found at http://answers.opencv.org/question/76952/regarding-the-error-message-the-data-should-normally-be-null/, which were to comment out the offending line in cv2.cpp
#It turns out that when installing with brew and building from source, cv2.cpp is bound up in a tarball and is thus not searchable from the command line. The tarball is found in:
/Library/Caches/Homebrew/
#To edit cv2.cpp and get homebrew to recognize it as legitimate source code from which to build the package, there are a few steps:
1. Navigate to the homebrew cache folder (above) untar the file
$ cd /Library/Caches/Homebrew/
$ tar -xf opencv3-3.1.0.tar.gz
2. open cv2.cpp with your text editor of choice (I’m using sublime text 2 aliased to “subl”)
$ subl opencv-3.1.0/modules/python/src2/cv2.cpp
3. commment out this line by prepending it with “//” and save the file
“CV_Error(Error::StsAssert, “The data should normally be NULL!”);”
4. delete the old tarball and make a new one with the same name with your edited source code
$ rm opencv3-3.1.0.tar.gz
$ tar -cf opencv3-3.1.0.tar.gz opencv-3.1.0
5. Uninstall your previous installation (if applicable, assuming you already tried to install with brew)
$ brew uninstall opencv3
6. Try to reinstall with your new source (this should give you an error, but it is necessary to run as you will see below). The optional flags after “opencv3” are not necessary if you don’t want them.
$ brew install opencv3 –with-contrib –with-ffmpeg –with-tbb –with-qt5 –with-gstreamer –with-python3
==> Installing opencv3 from homebrew/science
==> Using Homebrew-provided fortran compiler.
This may be changed by setting the FC environment variable.
==> Downloading https://github.com/Itseez/opencv/archive/3.1.0.tar.gz
Already downloaded: /Library/Caches/Homebrew/opencv3-3.1.0.tar.gz
Error: SHA256 mismatch
Expected: f00b3c4f42acda07d89031a2ebb5ebe390764a133502c03a511f67b78bbd4fbf
Actual: a2e705b40c15ddeaec8e724bb7d2747ef2ce438e987a79219af144fa7b38bf48
Archive: /Library/Caches/Homebrew/opencv3-3.1.0.tar.gz
To retry an incomplete download, remove the file above.
#Homebrew is expecting a certain hash for the tarball, and since you’ve changed the target file, there’s a mismatch. Luckily, we can change the expected signature
7. Edit the config file for the opencv3 package to replace the expected hash and save it
$ subl /usr/local/Library/Taps/homebrew/homebrew-science/opencv3.rb
edit line 7, replacing the hash in double quotes, whose value you saw in the previous step on the line “Expected: …” with the hash on the following line, beginning “Actual: …”
8. Now when you reinstall with homebrew, everything should work
$ brew install opencv3 –with-contrib –with-ffmpeg –with-tbb –with-qt5 –with-gstreamer –with-python3
Hello, I have a quick question about your opencv install instruction
I had following message:
If you need Python to find bindings for this keg-only formula, run:
echo /usr/local/opt/opencv3/lib/python2.7/site-packages >> /usr/local/lib/python2.7/site-packages/opencv3.pth
mkdir -p /Users/ArnoldChung/.local/lib/python3.5/site-packages
echo ‘import site; site.addsitedir(“/usr/local/lib/python2.7/site-packages”)’ >> /Users/ArnoldChung/.local/lib/python3.5/site-packages/homebrew.pth
Here, I tried,
echo /usr/local/opt/opencv3/lib/python2.7/site-packages >> /usr/local/lib/python2.7/site-packages/opencv3.pth
but permission was denied. Any idea about this happening?
Thank you a lot in advance
I’m getting an ImportError: numpy.core.multiarray failed to import .. how can i fix this ???
I’ve gotten stuck, both with brew and with trying to manually compile on my own.
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
HDF5_sz_LIBRARY
linked by target “opencv_hdf” in directory /tmp/opencv3-20160702-17368-1myi1jw/opencv-3.1.0/opencv_contrib/modules/hdf
No idea what HDF5_sz_LIBRARY is or should be or what to do about. Unexpectedly, Google isn’t even any help. Any ideas? Thanks so much!
Note that opencv3 is now installed by default, and you need to run “brew install [email protected]” to install the older version
Thanks for the post, finally got it working and it only took 5 hours 😀
Had to make some minor changes which I documented here: https://stackoverflow.com/a/48042674/1602316
Hi! I got a error like:
/Users/admin/opencv_contrib/modules/ccalib/src/multicalib.cpp:134:23: error:
no member named ‘imread’ in namespace ‘cv’
Mat pattern = cv::imread(file_list[0]);
~~~~^
/Users/admin/opencv_contrib/modules/ccalib/src/multicalib.cpp:169:65: error:
use of undeclared identifier ‘IMREAD_GRAYSCALE’
image = imread(filesEachCameraFull[camera][imgIdx], IMREAD_G…
what’s wrong with this?