Setting up and testing OpenCV in MacOS – tested on Mountain Lion v. 10.8.3
(*) based on the blog notes of Tilo Mitra
Installation:
- Download the latest version of OpenCV from SourceForge: http://sourceforge.net/projects/opencvlibrary/files/latest/download and unzip it. A folder is extracted with various files and folders
- Download cmake which is required in order to build and install OpenCV for MacOS. There are 2 alternatives to follow:
- Based on MacPorts
- Download MacPorts for Mountain Lion: https://distfiles.macports.org/MacPorts/MacPorts-2.1.3-10.8-MountainLion.pkg and install it
- Open a terminal window and type: sudo port install cmake. This will download and install cmake.
- Direct download
- Get cmake from http://www.cmake.org/files/v2.8/cmake-2.8.10.2-Darwin64-universal.dmg and install it.
- Open a terminal window, change directory to where the OpenCV was extracted, create a folder named build and move into it. Then type the following commands:
cmake -G “Unix Makefiles” ..
make -j8
sudo make installAfter successful installation everything should be installed in /usr/local/lib and /usr/local/include
- There is a high probability that step 3.b will result in 2 errors! In this case an additional step is needed:
- Download the latest JUnit from SourceForge: http://sourceforge.net/projects/junit/files/latest/download
- While in the terminal window and inside the “build” directory create a folder named lib:
mkdir modules/java/test/.build/lib
and copy the downloaded junit file in this folder with:
cp ~/Downloads/junit-4.10.jar modules/java/test/.build/lib
- Then retry the make process, which will most probably result in 100% success. It is suggested to execute a: “make clean” command before rerunning step 3.b
Testing the installation:
- Start Xcode and create a Command Line Tool project
- Right-click the project name after created and select “Add files to…”. When finder pops-up type “/” to get the navigation panel and type /usr/local/lib and hit Enter. Select all *opencv*.dylib files and include them in the project. It is suggested to group then into one group i.e. named “OpenCV” for easier management
- Select the project name again and go to “Build Settings”. Search for “Header Search Paths” and once found input the path: /usr/local/include
- Open main.cpp file and type in the typical open-save image example shown below
// Example showing how to read and write images
#include
#include
#includeint main(int argc, char** argv)
{
IplImage * pInpImg = 0;// Load an image from file – change this based on your image name
pInpImg = cvLoadImage(“my_image.jpg”, CV_LOAD_IMAGE_UNCHANGED);
if(!pInpImg)
{
fprintf(stderr, “failed to load input image\n”);
return -1;
}// Write the image to a file with a different name,
// using a different image format — .png instead of .jpg
if( !cvSaveImage(“my_image_copy.png”, pInpImg) )
{
fprintf(stderr, “failed to write image file\n”);
}// Remember to free image memory after using it!
cvReleaseImage(&pInpImg);return 0;
}