I’m using OpenCV for my 4th year design project and setting it up was a huge pain. I had to look through a whole bunch of different sites to figure out what to do. There are various ways to install it – through package managers such as Homebrew or Macports, or through the tarball + cmake. Now that I’ve got it set up, I decided to write this little post to explain to others how to go about setting it up.
Note: This method does not set up the Python bindings for OpenCV (still working on that). It only sets up the C++ framework. Also, I tested this on OSX Lion, but it should apply to Snow Leopard or Leopard. Also you will need XCode installed for any of this to work (but you knew that, right?)
On that note, let’s get started.
Download a Package Manager
It’s between Macports, Fink or Homebrew. I used Macports, so I’d recommend that. Download the .dmg file, then install it. You can check to see if it installed successfully by opening your terminal and typing port.
Download the OpenCV Tarball
You can get that from here. Look for the Linux or Mac version. Unzip it after you download it into a folder.
Get cmake
In your terminal, type in the following:
sudo port install cmake
This will go fetch cmake and its dependencies and install them onto your system. You can check to see that cmake is installed by typing cmake in a new terminal window.
Build OpenCV
We are going to build OpenCV using cmake. In terminal, navigate to the folder where OpenCV was extracted to. Type in the following:
# make a separate directory for building mkdir build cd build cmake -G "Unix Makefiles" ..
Now, we can make OpenCV. Type the following in:
make -j8 sudo make install
This should now build OpenCV into your /usr/local/ directory.
Make A Sample OpenCV Project
So we now have OpenCV built but we still have to link to the framework in our project.
- Start a new XCode Command Line Tool project.
- 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..”
- When Finder pops up, hit “/” to bring up the navigation panel.
- Type in
/usr/local/lib - Add in all the .dylib files that you need. To prevent linker errors, I recommend you initially add ALL the files ending in “…2.3.1.dylib”. There should be a dozen or so. If you know what you need, you can obviously pick and choose.
- Now, you should have a bunch of .dylib files in your project. Feel free to move them to a separate group within your project.
- Click on the project file and go to “Build Settings”.
- Search for “Header Search Paths”
- Change the path to
/usr/local/include. This is where the header files for OpenCV were built. - Open main.cpp
- 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.hpp>
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;
}
And there you go. That should be working for you. If it’s not, leave a comment below with the error you get and I’ll try looking into it for you. Hopefully, this helps save you some time.
On that note, here is a good OpenCV Tutorial.
Poetry is better.
Hi,
Thanks for the tutorial.
When I try your sample, I’m getting number of errors in XCode.
c++locale.h – Semantic issue. Use of undeclared indentifier ‘va_start’
calib3d.hpp – Semantic issue. Use of undeclared indentifier ‘DBL_….
contrib.hpp – Semantic issue…
And many more.
Any idea what’s wrong in my project?
Hard to say based on those error messages. Are you compiling this project using g++? That may be one issue. What OS are you on? I tested this on Lion.
These sound like syntax issues, so at the very least, you could go into those files and use an alternate syntax.
Hi,
I’m using Mac OS X Lion (10.7.2).
I installed OpenCV before I found your guide. These are the steps I followed.
1. Installed CMake
2. tar xjf OpenCV-2.3.1a.tar.bz2
3. cd OpenCV-2.3.1
4. cmake -DCMAKE_INSTALL_PREFIX=/usr .
5. make
6. sudo make install
OpenCV is successfully installed to /usr. And I’m able to compile your cpp file using the terminal with the following command.
g++ -I/usr/include -L/usr/lib -lopencv_core.2.3.1 -lopencv_calib3d.2.3.1 -lopencv_imgproc.2.3.1 -lopencv_legacy.2.3.1 -lopencv_highgui.2.3.1 main.cpp
But I can’t build/run the same cpp file with XCode. I think it’s something wrong with linking. I have set XCode’s Header Search Path to /usr/include and included dylib files from /usr/lib. But still getting the same error.
I got it working. Apparently I have made a mistake by installing OpenCV at /usr. When I set Header Search Path to /usr/include XCode picks some unnecessary headers and fails to link my project it seems. I followed your tutorial from the beginning and the it worked.
Thanks for the help.
One addition, third include in your code is redundant i guess.
#include
I’m able to successfully run the code without this include.
Regard,
Gian
Thanks Gian! Glad I could help and thanks for posting your thoughts so other readers in the same boat as you are able to figure out the issue! Cheers.
when i type this: cmake -G “Unix Makefiles” ..
i get this: CMake Error: The source directory “/Users/karansamel” does not appear to contain CMakeLists.txt.
Specify –help for usage, or press the help button on the CMake GUI.
are we supposed to have CMakeLists.txt in there before?
actually the only problem I’m having now is at the last step when i type in sudo make install, i get this:
make: *** No rule to make target `install’. Stop.
It seems like the directory from where you are executing “sudo make install” does not have Makefiles. Can you see if Makefile exists in that directory?
Hi, how do I change the path of the Header Search File?
It should be in Build Settings. Go here for more info: http://stackoverflow.com/questions/5269029/header-search-path-in-xcode-4