Updates‎ > ‎

Tutorial: Installing OpenCV on Mac OS X Mountain Lion

posted Sep 4, 2013, 3:41 AM by Yonas Stephen   [ updated Sep 5, 2013, 1:42 AM ]
I have been through a tough time installing OpenCV in my Macbook Pro with Mountain Lion OS. Finally I got it installed! So I would like to share to people out there to help you out installing OpenCV.

First of all, unfortunately there is no easier way to install OpenCV than through the Terminal. You may found few methods to install them on the internet such as:
  1. CMake
  2. MacPort
  3. Homebrew
In this tutorial we'll be using CMake. 

INSTALLING CMAKE
  1. First you need to download the .dmg file under Binary Distribution of CMake here. At the time of I installed this, the file name is cmake-2.8.11.2-Darwin64-universal.dmg
  2. Upon completion of the installation you will be prompted whether to put CMake in /usr/bin, select Yes and finish installation
  3. To ensure that CMake has been successfully installed, type cmake -version in Terminal

INSTALLING OPENCV
  1. First, download the tar file of OpenCV 2.4.3 stable version here and DO NOT use the latest version. I struggled with the installation because I used OpenCV 2.4.6.1 and it's not yet compatible.
  2. Extract the tar file in a folder and go to that folder through the terminal, e.g. cd Downloads/OpenCV-2.4.3
  3. Now, each of the following line is to be executed in Terminal and must be executed successfully (no error)
    • mkdir build
    • cd build
    • cmake -G "Unix Makefiles" ..
    • make -j8
    • sudo make install
  4. You are all set to use OpenCV!

SAMPLE OPENCV PROJECT

Now that you have installed OpenCV library, let's try making a simple OpenCV project. Basically the sample project follows Tilo Mitra's project here, so credits to him! However I will be adding few additional steps to tackle the errors when I ran his project in Xcode 4.6.3.

  1. Start a new XCode Command Line Tool project of type C++.
  2. We have to link the .dylib files provided by OpenCV into our project. To do this, right click on the project, and click “Add files to..”
  3. When Finder pops up, hit “/” to bring up the navigation panel.
  4. Type in /usr/local/lib
  5. Add in all the .dylib files that you need. To prevent linker errors, I recommend you initially add ALL the files ending in “…2.4.3.dylib”. There should be a dozen or so. If you know what you need, you can obviously pick and choose.
  6. Now, you should have a bunch of .dylib files in your project. Feel free to move them to a separate group within your project.
  7. Click on the project file and go to “Build Settings”.
  8. Search for “Header Search Paths”
  9. Change the path to /usr/local/include. This is where the header files for OpenCV were built.
  10. Search for "Library Search Paths"
  11. Change the path to /usr/local/lib
  12. Open main.cpp
  13. Go to File > Project Settings > Change the Derived Data Location to Project-Relative
  14. Copy the following code snippet. This snippet should load a .jpg image and save it as a .png image.

// Example showing how to read and write images
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv/cvaux.h>

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);
  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;
}

Please note that on the 4th line I changed cvauk.hpp (Tilo Mitra's tutorial) to cvauk.h, because apparently the header file is .h instead of .hpp for some reasons in newer version.

Also the point of instructions 13 is to make things easier for you to put the image file to load using cvLoadImage. By default, the project build files is generated in the /Library/Developer/Xcode/DerivedData folder and you must put your image in the same folder as the build files. By having project-relative setting, the build files will be stored inside our project folder; therefore you can put your "my_image.jpg" simply in your project folder, i.e. Put it in DerivedData/project_name/Build/Product

To run the project just use Cmd + R, and here you go the JPG to PNG converter!

Comments