I have been trying to build openCV libraries which would be compatible for mac catalyst (iOS app built for Mac which is a new feature on iOS 13/Mac 10.15 beta) without much success. This is the command the python build script calls while building OpenCV framework for Mac. Xcodebuild MACOSXDEPLOYMENTTARGET=10.15 ARCHS=x8664 -sdk macosx -configuration Release -parallelizeTargets -jobs 12.
In this post, we will provide step by step instructions for installing OpenCV 3.3.0 (C++ and Python) on MacOS and OSX.
If you are still not able to install OpenCV on your system, but want to get started with it, we suggest using our docker images with pre-installed OpenCV, Dlib, miniconda and jupyter notebooks along with other dependencies as described in this blog.
Step 1: Install XCode
OpenCV is a BSD-licensed library for digital image processing which implements several hundred computer vision algorithms. Unfortunately compiling it on the Mac was not straightforward—requiring Fink or MacPorts —and the one existing Mac framework was out of date and no longer maintained.
OpenCV Annotation Tool for Mac? I couldn't compile OpenCV to use the opencvannotation tool because my laptop is low on storage so I was looking for an alternative tool that could help me get the job done. Any help would be appreciated! Edit retag flag offensive close merge delete.
Install XCode from App Store.
Opencv Mac Os
If XCode available on App Store is not compatible with your OS:
Find XCode version compatible to your OS from this table https://en.wikipedia.org/w/index.php?title=Xcode#Version_comparison_table
Go to this webpage https://developer.apple.com/download/more/
Login if you have apple developer account else create your account and login.
Search for xcode and download the version compatible to your OS.
Install XCode.
After installation open XCode, and accept xcode-build license when it asks.
Step 2: Install Homebrew
Launch a terminal from Launchpad. From this step onward, all commands will be run in the terminal.
Step 3: Install Python 2 and Python 3
Python version (2.6 or 2.7, 3.5 or 3.6) installed on your machine is required to determine path of Python’s site-packages. It will be used later.
NOTE : Recently Homebrew made some changes in Python formula. Earlier homebrew used to install python2 as /usr/local/bin/python. Now it follows these rules:
Install python2 at /usr/local/bin/python2
Install python3 at /usr/local/bin/python3
python command will point to /usr/bin/python. This is the python distribution which comes with your OS and not installed by Homebrew.
We want to use Python installed by Homebrew because it makes installing/managing packages easier. To run python scripts you should run command python2 and python3 for Python 2 & 3 respectively. If you find this annoying and want to use command python to run python2, add following line to ~/.bash_profile.
This step is recommended not just for this course but in general to keep the python installation clean.
Step 4: Install Python libraries in a Virtual Environment
We will use Virtual Environment to install Python libraries. It is generally a good practice in order to separate your project environment and global environment.
Step 5: Install OpenCV
Step 5.1 : Compile & Install OpenCV
Note: Homebrew recently moved many popular formulae to homebrew-core. Earlier you can install OpenCV 2 using formula name opencv and OpenCV 3 using formula name opencv3. They have now renamed opencv3 to opencv and opencv to [email protected] Various options such as –with-qt, –with-tbb etc are also removed in the updated formula and CUDA support is also dropped. You can check these github issues 15853, 6126, 16640, 5996 to read more about it.
Homebrew has also made it compulsory to compile and install Python bindings for both Python 2 and Python 3. So if you don’t have Python 3 installed, Homebrew will install it while installing opencv.
Step 5.2 : Add OpenCV’s site-packages path to global site-packages
When brew is done compiling and installing OpenCV3, we will update path of site-packages directory which contains cv2.so file to Homebrew Python’s site-packages directory. Depending upon the Python version you have (2.6/2.7 or 3.5/3.6) these paths would be different.
Step 6: Make OpenCV3 Python symlink in our virtual environment
Path to OpenCV’s Python library will be different depending upon which Python version you have. Double check the exact path and filename on your machine. Use this command to find out the path on your machine.
Step 7: Test OpenCV3
Now you can exit from Python virtual environment.
Now whenever you are going to run Python scripts which use OpenCV you have to activate the virtual environment we created using workon command.
Subscribe & Download Code
If you liked this article and would like to download code (C++ and Python) and example images used in all posts of this blog, please subscribe to our newsletter. You will also receive a free Computer Vision Resource Guide. https://yellowmagical966.weebly.com/fantastical-for-mac.html. In our newsletter, we share OpenCV tutorials and examples written in C++/Python, and Computer Vision and Machine Learning algorithms and news.
OpenCV is a computer vision library used extensively by people in the computer vision field. Until a couple of years ago, OpenCV was a bit hacky and the usage was not very straightforward. But determined efforts by multiple companies finally standardized the process and now it is nice and clean. Computer vision algorithms are computationally intensive, requiring lot of processing power to run in real time. Before OpenCV came along, the efforts were very fragmented and repetitive, and there was no standard library as such. Hence Intel decided to do something about it and came up with OpenCV. The advantage of OpenCV is that the algorithms are highly optimized and the library is available on almost all the popular platforms. I have outlined the procedure below to get OpenCV up and running on your Mac.
Prerequisites
Make sure you have git command line tool installed on your machine. If not, I have described the procedure here.
Make sure Macports is installed on your machine. If not, you can get it here. Install it before you proceed.
If you don’t have cmake, run the following command to install it (this command uses Macports):
Downloading OpenCV
We are now ready to install OpenCV. Download the latest version of OpenCV from here. Make sure you download the version for your platform (Mac, Windows, etc). You will get a directory named opencv-2.x.x, depending on the latest version number.
If you don’t want to worry about downloading every time a new version is released, you can download the OpenCV git repo. This way, you can easily maintain and update your OpenCV version with a simple command. Open the terminal, go to a convenient location and download the OpenCV source code using the following command:
It will create a directory called “opencv” and download all the files here. When you want to update to a new version, just go to this folder and type the following command:
This will update your repo to the latest version.
Installing OpenCV
Now that we have the source code for OpenCV, we need to build it. Run the following commands:
We need to place the libraries in the correct locations so that they are accessible. Run the following command:
This will install everything in the /usr/local/ directory. That’s it! OpenCV is now installed on your machine.
OpenCV with Xcode
Now that we have installed OpenCV, we are ready to use it in our C/C++ code. Xcode is the most popular IDE on Mac OS X, so we would naturally want to see how to use OpenCV on Xcode. I have outlined the procedure below:
Open Xcode
Select Xcode -> New -> Project.
A new window will pop up. On the left sidebar, under “OS X”, select “Application” and choose “Command Line Utility”. Click “Next”
In the next window, give a name for the project and make sure the “Type” field at the bottom is set to C++. Click “Next”
In the left sidebar, you should be able to see your project name next to a small Xcode icon. Double click on the project. This should open the Build Settings tab (located on the top of the new window).
In the “Architectures” section, make sure it is set to “Native architecture of the build machine”.
Scroll down to the “Search Paths” section and set “Header Search Paths” to: /usr/local/include. You can do this by double-clicking on the blank space right next to “Header Search Paths” and clicking on the “+” symbol at the bottom left. After you add it, make sure you select “recursive” located on the end of the same row.
Close this window.
In the left sidebar, you should be able to see your project name with a folder icon next to it. If you expand it, you will see main.cpp. Now right click on this project and select “New group”. Name it “OpenCV Libraries”.
Right click on “OpeCV Libraries” and select Add Files to “Project” … (the word “Project” would have been replaced with your project name)
Press the “/” key to get the “Go to” folder prompt. Enter the following path: /usr/local/lib
Select all the files whose names are of the form libopencv_*.dylib. Note: Although you don’t have to worry about this, it’s better to know anyway. There will be aliases in that file list as well. So before selecting a file, look at the file icon. If there’s an arrow on the icon, it means that this file is an alias. You don’t have to add these files, since they are just aliases (and not real files).
Click “Add” and you are done!
Go to main.cpp and write a simple piece of OpenCV code. Check if it runs. If you see an error, read on.
Undefined symbols for architecture x86_64
When I first upgraded my Xcode to version 4.6, I got the following error:
Open Build Settings by double-clicking on the project name with the Xcode icon. In this window, under “Architectures” section, set “Base SDK” to “Current OX S”. Now scroll down to “Apple LLVM compiler 4.1 – Language” section. In “C++ Language Dialect”, change it to “Compiler Default”. In “C++ Standard Library”, change it from “libc++ (LLVM …)” to “libstdc++ (GNU C++ …)”.
That should do the trick for you. If not, there is another way to run OpenCV code. Keep reading!
Running OpenCV Code From Command Line
This is the last sure shot way of running OpenCV code, but you will miss out on the niceness of an IDE. You can use cmake to build and run OpenCV code. Open the Finder and create a folder for your project. Place your .cpp file there. Create a file called “CMakeLists.txt” and place the following lines in that file:
In the above lines, replace “PROJECT_NAME” with your project name and replace filename by your source code file name. For example, if your project name is “OBJECT_TRACKING” and your filename is “tracker.cpp”, then the contents of CMakeLists.txt should be:
Opencv For Mac Os
Once you create this file, run the following commands in the same directory:
You should now be able to see the executable with the same name as the your .cpp file. Now open the terminal and just type the following to run the code:
Opencv For Machine Vision Pdf
If you have any command line arguments that you are taking in your .cpp file, give them here:
Build Opencv For Mac
And there you go! You are all set to do some OpenCV coding.