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 varous 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 shoshould 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. DMG file, then install it. you can check to see if it installed successfully by opening your terminal and typingport
.
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 typingcmake
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 buildingmkdir buildcd buildcmake -G "Unix Makefiles" ..
Now, we can make opencv. type the following in:
make -j8sudo make install
This shoshould 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 .."
- 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 shoshould be a dozen or so. If you know what you need, you can obviously pick and choose.
- Now, you shoshould 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
/usr/local/include
. This is where the header files for opencv were built.
- Open main. cpp
- Copy the following code snippet. this snippet shocould load a. jpg image andsave 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 shoshould 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.
Please read before commenting:Hey guys-Thanks a lot for all the comments on this thread. unfortunately, it's been a long time since I looked into opencv so I don't remember the details anymore. if you have questions, skim through the comments because scenarios people have posted their solutions. feel free to comment if you don't find an answer. on the other hand, if you made some small change that made this work-Please add it The comments so others can find it useful. Thanks!
Opencv on Mac OSX: a step-by-step guide