Setting up and testing OpenCV in MacOS – tested on Mountain Lion v. 10.8.3
(*) based on the blog notes of Tilo Mitra
- 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-126.96.36.199-Darwin64-universal.dmg and install it.
cmake -G “Unix Makefiles” ..
sudo make install
After successful installation everything should be installed in /usr/local/lib and /usr/local/include
- 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:
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
int 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);
fprintf(stderr, “failed to load input image\n”);
// 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!