OpenCV 2.0 on SnowLeopard 64bit via MacPorts

Updated 1/17/10: The Portfile now includes a “+disable_openmp” variant. I encountered some OpenMP related segfaults in cvCalcOpticalFlowPyrLK which can be prevented with this variant.

In case you need OpenCV 2.0 on SnowLeopard in 64bit and can live without quicktime support (OpenCV 2.0 it has FFMPEG support so you should be OK in most cases) this tutorial is for you.

In case you want OpenCV 2.0 in 32 bit on 10.5 this probably works too, but I haven’t tested it. Just download the attached “Portfile” and follow the short steps underneath:

  1. Before starting execute
    sudo port selfupdate
    so that the most recent dependencies will be installed later on. Otherwise the build might fail.

  2. Make your own port directory somewhere, e.g.
    /Users/sam/ports
  3. Let macports know about it by editing sources.conf, e.g.
    > cd /opt/local/etc/macports
    > sudo vim sources.conf
  4. Add the local port directory to the sources.conf before the rsync:// at the bottom of the file, e.g.
    file:///Users/sam/ports # That is 3 slashes after file ;)
  5. Create a graphics/OpenCV subfolder so that we have in our example
    /Users/sam/ports/graphics/OpenCV
  6. Copy the downloaded Portfile into the above directory, make sure it is called Portfile not Portfile.txt
  7. Execute portindex in your local port directory, e.g.
    > cd /Users/sam/ports
    > portindex
    You should see a message telling you that the total number of parsed ports is 1
  8. Check if OpenCV 2.0 is found
    > port search OpenCV
  9. Install OpenCV via
    > sudo port install OpenCV
    or on SnowLeopard in 64 bit but without quicktime support
    > sudo port install OpenCV +sl_64bit
  10. Enjoy the time you have saved by not having to implement the algorithms in the new MachineLearning package all by yourself ;)

Download Portfile

23 Responses to this post.

  1. Posted by Franzi on 26.10.09 at 4:38 pm

    Worked like a charm :) ! Thanks so much!

  2. Posted by James on 26.10.09 at 4:38 pm

    Everything is building great now I’ve finally managed to follow the instructions properly :D

  3. Posted by Holger on 26.10.09 at 4:38 pm

    Hi, have you considered contributing this to MacPorts? It would be very useful indeed.

  4. Posted by admin on 26.10.09 at 4:38 pm

    I submit it to the current maintainer. It might not go through because of the missing qt support, let’s see …

  5. Posted by Marco on 26.10.09 at 4:38 pm

    Great stuff. However I still have a couple of problems:

    1) when compiling the C samples (find_obj.cpp) I get the following errors:

    Undefined symbols:
    “cv::flann::Index::knnSearch(cv::Mat const&, cv::Mat&, cv::Mat&, int, cv::flann::SearchParams const&)”, referenced from:
    flannFindPairs(CvSeq const*, CvSeq const*, CvSeq const*, CvSeq const*, std::vector<int, std::allocator >&)in ccL9sHQ0.o
    “vtable for cv::flann::KDTreeIndexParams”, referenced from:
    cv::flann::KDTreeIndexParams::KDTreeIndexParams(int)in ccL9sHQ0.o
    cv::flann::KDTreeIndexParams::~KDTreeIndexParams()in ccL9sHQ0.o
    “cv::flann::Index::Index(cv::Mat const&, cv::flann::IndexParams const&)”, referenced from:
    flannFindPairs(CvSeq const*, CvSeq const*, CvSeq const*, CvSeq const*, std::vector<int, std::allocator >&)in ccL9sHQ0.o
    “cv::flann::Index::~Index()”, referenced from:
    flannFindPairs(CvSeq const*, CvSeq const*, CvSeq const*, CvSeq const*, std::vector<int, std::allocator >&)in ccL9sHQ0.o
    flannFindPairs(CvSeq const*, CvSeq const*, CvSeq const*, CvSeq const*, std::vector<int, std::allocator >&)in ccL9sHQ0.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status

    2) when I try to execute some sample program that requires the highgui I get:

    Xlib: extension “RANDR” missing on display “/tmp/launch-Ykfiaj/:0″.

    Has anybody experienced this, and knows how to solve ?

  6. Posted by Matthias on 26.10.09 at 4:38 pm

    The Xlib warning is quite common and can be ignored.

  7. Posted by Marco on 26.10.09 at 4:38 pm

    Doh, right, my problem #2 is not really an issue. I’m a bit worried however by the problem with FLANN. Have you seen it ?

    I’ve also another issue with the face detection example (facedetect.cpp) that crashes (still have to see where, it just segfaults).

  8. Posted by Erik on 26.10.09 at 4:38 pm

    Thank you Matthias

    The installation of OpenCV worked really like a charm, but another piece of software, the frei0r-plugins, which depends on the OpenCV, fails with this error message:

    In file included from /opt/local/include/opencv/cxcore.hpp:46,
    from /opt/local/include/opencv/cxcore.h:2123,
    from /opt/local/include/opencv/cv.h:58,
    from filter/facedetect/facedetect.c:20:
    /opt/local/include/opencv/cxmisc.h:52:26: error: cvconfig.h: No such file or directory
    make[1]: *** [facedetect_la-facedetect.lo] Error 1
    make: *** [all-recursive] Error 1

    I am compiling on Snow Leopard in 64-bit and trying to install kdenlive. Everything except the frei0r-plugins and mlt compiles well. Does anybody have a clue on how to fix this??

    Thanks in advance,
    Erik

  9. Posted by Erik on 26.10.09 at 4:38 pm

    … or to narrow down the the question, there are three possibilities:

    1. To tell the frei0r-plugins not to use this function of OpenCV (i.e. the missing header file cvconfig.h)
    OR
    2. To patch the older version 1 of OpenCV, where the cvconfig.h is obviously existent, in order to get it compiled under Snow Leopard
    OR
    3. To tell mlt not to depend on the frei0r-plugins

    I am used to Portage under Gentoo but completely new to MacPorts, thus I’ll be very glad if someone smarter could see a good solution.

    Erik

  10. Posted by Erik on 26.10.09 at 4:38 pm

    Hi guys

    I managed to compile frei0r-plugins and mlt by simply uncommenting these lines:

    #ifdef HAVE_CONFIG_H
    #include “cvconfig.h”
    #endif

    in /opt/local/include/opencv/cxmisc.h

    However, in order to start kdenlive, I also had to run it like this:

    KDE_DEBUG=False /opt/local/bin/kdenlive.app/Contents/MacOS/kdenlive –nocrashhandler &

    following this thread: https://bugs.launchpad.net/ubuntu/+source/kdenlive/+bug/287922

    Last but not least, if your dbus refuses to run, try this:

    launchctl load -w /Library/LaunchAgents/org.freedesktop.dbus-session.plist

    I hope this might help someone else here …

    Erik

  11. Posted by Nick on 26.10.09 at 4:38 pm

    Matthias, I followed this method and installed via macports, and it seem to work mostly okay. I have two questions.

    1) cvHaarDetectObjects now gives a segfault whenever I try to call it in 64-bit mode. Do you think this is a problem with the underlying code? or something about the build method? Do you have this problem on your install?

    2) I wrote a program to replace all of the functionality of cvcap_qt.cpp to use more modern osx libraries that will compile in 64-bit mode. I’d like to talk to someone about integrating these into the official source code, but I’m not sure who would be good to contact. I tried emailing some of the maintainers on sourceforge to no avail. Do you know who is involved in currently maintaining the Mac specific portions of OpenCV and how I can contact him?

    Thanks for your help on the 64-bit mode things. This 10.6 thing is kindof a mess, but I’m glad things are finally setting down and becoming unbroken.

    –Nick

  12. Posted by Michael on 26.10.09 at 4:38 pm

    First of all, thank you very much for the Portfile, that’s really sped things up.
    @Nick: Does this mean you have a fully 64bit OpenCV with QuickTime running under SnowLeopard? If so is there anyway you could package it up and share it with us? This would be very very useful for me.

    Hopefully soon this 10.6pain will be over and we can get back to actually writing code!
    Thank you all for your pointers,
    Michael

  13. Posted by Michael Slater on 26.10.09 at 4:38 pm

    First of all, thank you very much for the Portfile, that’s really sped things up.
    @Nick: Does this mean you have a fully 64bit OpenCV with QuickTime running under SnowLeopard? If so is there anyway you could package it up and share it with us? This would be very very useful for me.

    Hopefully this 10.6 pain will be over soon and we can get back to actually writing code!
    Thank you all for your pointers,
    Michael

  14. Posted by luisrpp on 26.10.09 at 4:38 pm

    Hi Matthias,

    Thanks for sharing this. I finally got OpenCV 2.0 working in Snow Leopard with FFMpeg support.

    The only thing that is not working is the iSight Camera.

    Do you know how to fix this?

    Thanks & Regards,
    Luis

  15. Posted by szibis on 26.10.09 at 4:38 pm

    I have same problem with frei0r-plugins, and too solve the problem I get the source of OpenCV 2.0 from distfiles to /usr/src/, untar, and then run ./configure –prefix=/op/local, just like installer from port do.

    Then configure creates cvconfig.h, and then i copy this header into includes of opencv after normal port install opencv from user profile.

    remove the sources from /us/src/, and it’s clean.

    Then, when it’s done i run again frei0r-plugins install, and it’s works :)

  16. Posted by Daniel on 26.10.09 at 4:38 pm

    Anybody got cvHaarDetectObjects running in 64bit mode? I’ve the same problem as Nick with segfault in cvHaarDetect

  17. Posted by Matthias on 26.10.09 at 4:38 pm

    Where does the segfault happen? I had problems with cvCalcOpticalFlowPyrLK in OpencV 2.0 and traced it back to openmp related calls. I recompiled it without openmp support which prevents the segfault. I added a new version of the Portfile with a “+disable_openmp” variant to the post. Let me know if that solves your problem.

  18. Posted by zigazou on 26.10.09 at 4:38 pm

    When you still get error messages when building OpenCV, you should try “sudo port clean OpenCV” and start again.

  19. Posted by Christy on 26.10.09 at 4:38 pm

    Hi,

    Thank you for providing these instructions. I want to use your method to install OpenCV on Snow Leopard with the portfile you supply, but would also like to enable support for the OpenCV Python Interface. According to the following page, http://www.tsd.net.au/blog/opencv-python-bindings-macports , python support can be enabled by installing openCV with “cmake” and the -D option:

    cmake … -D BUILD_PYTHON_SUPPORT=ON …

    Is there a way to enable python support via a macports install?

    Thank you.

  20. Posted by Matthias on 26.10.09 at 4:38 pm

    Sure, download the Portfile and add this to the end of the file:

    variant python_support description {Enables python support} {
    configure.args-append –with_python
    }

    Save and install via
    sudo port install opencv +sl_64bit +python_support

    Hope this helps.

  21. Posted by Christy on 26.10.09 at 4:38 pm

    Thanks Mattias. it worked for me with a slight variation.

    I substituted
    –with-swig**
    for
    –with-python.

    So the bottom of Portfile (as it exists on my computer) reads:

    variant python_support description {Enables python support} {
    configure.args-append –with-swig
    }

    And I installed as directed with:

    sudo port install opencv +sl_64bit +python_support

    To test the installation I found and ran the sample script “morphology.py” located under:
    /Applications/OpenCV/build/OpenCV-2.0.0/samples/swig_python
    on my computer. it worked :)

    ————————-
    ** SWIG was downloaded from http://sourceforge.net/projects/swig/ and installed according to the instructions in the accompanying README file.

    I also added the following line to my .bash_profile

    export PYTHONPATH=$PYTHONPATH:/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages:/opt/local/lib/python2.6/site-packages:/Library/Python/2.6/site-packages:/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/py2app/recipes/

    … and sourced the newly edited file (i.e. > source ~/.bash_profile)

  22. Posted by Cary on 26.10.09 at 4:38 pm

    you rock

  23. Posted by Xavi on 26.10.09 at 4:38 pm

    Thanks a lot,
    after a small twick it compiled perfectly. Basically, I am using ffmpeg-deve for some projects and it does not coexists well with ffmpeg. Given that I just need opencv for some of the functions in the libraries (and not for input/output of video) I just deleted the reference to ffmpeg in the Portfile and all compiled like a charm.

Respond to this post