Tag Archives: OpenCV

Install OpenCV 2.4.2 with ffmpeg support in Mac OS X 10.8

In order to install OpenCV 2.4.2 with ffmpeg support we have to first install lamemp3, faac, faad and of course ffmpeg. I’ve added a new script in my installation-scripts repository in Github but I’m going to write down here installing one by one all the libraries.

Also remember that you need to install the “Command Line Tools” in order to compile from the command line if you are using Xcode 4.4 or above. Have a look at “Can’t configure with cmake in Mac OS X 10.8” before continuing the installation process.

mp3lame:

curl -L -o lame-3.99.5.tar.gz http://sourceforge.net/projects/lame/files/lame/3.99/lame-3.99.5.tar.gz/download
tar xzvf lame-3.99.5.tar.gz
cd lame-3.99.5
./configure --disable-dependency-tracking CFLAGS="-arch i386 -arch x86_64" LDFLAGS="-arch i386 -arch x86_64"
make
sudo make install
cd ..

faac (notice that only compiles for x86_64):

curl -L -o faac-1.28.tar.gz http://sourceforge.net/projects/faac/files/faac-src/faac-1.28/faac-1.28.tar.gz/download
tar xzvf faac-1.28.tar.gz
cd faac-1.28
./configure --disable-dependency-tracking CFLAGS="-arch x86_64" LDFLAGS="-arch x86_64"
make
sudo make install
cd ..

faad:

curl -L -o faad2-2.7.tar.gz http://sourceforge.net/projects/faac/files/faad2-src/faad2-2.7/faad2-2.7.tar.gz/download
tar xvzf faad2-2.7.tar.gz
cd faad2-2.7
./configure --disable-dependency-tracking CFLAGS="-arch i386 -arch x86_64" LDFLAGS="-arch i386 -arch x86_64"
make
sudo make install
cd ..

ffmpeg:

curl -O http://ffmpeg.org/releases/ffmpeg-0.11.1.tar.gz
tar xzvf ffmpeg-0.11.1.tar.gz
cd ffmpeg-0.11.1
./configure --enable-libmp3lame --enable-libfaac --enable-nonfree --enable-shared --enable-pic --disable-mmx --arch=x86_64
make
sudo make install
cd ..

OpenCV 2.4.2:

curl -L -o OpenCV-2.4.2.tar.bz2 http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.2/OpenCV-2.4.2.tar.bz2/download
tar xvzf OpenCV-2.4.2.tar.bz2
cd OpenCV-2.4.2

If we configure and compile OpenCV 2.4.2 as it comes we will get a linker error at about 34% of the compilation process like this:

Linking CXX shared library ../../lib/libopencv_highgui.dylib
Undefined symbols for architecture x86_64:
  "_faacEncClose", referenced from:
      _Faac_encode_close in libavcodec.a(libfaac.o)
      _Faac_encode_init in libavcodec.a(libfaac.o)
  "_faacEncEncode", referenced from:
      _Faac_encode_frame in libavcodec.a(libfaac.o)
  "_faacEncGetCurrentConfiguration", referenced from:
      _Faac_encode_init in libavcodec.a(libfaac.o)
  "_faacEncGetDecoderSpecificInfo", referenced from:
      _Faac_encode_init in libavcodec.a(libfaac.o)
  "_faacEncOpen", referenced from:
      _Faac_encode_init in libavcodec.a(libfaac.o)
  "_faacEncSetConfiguration", referenced from:
      _Faac_encode_init in libavcodec.a(libfaac.o)
  "_lame_close", referenced from:
      _mp3lame_encode_close in libavcodec.a(libmp3lame.o)
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
  "_lame_encode_buffer", referenced from:
      _mp3lame_encode_frame in libavcodec.a(libmp3lame.o)
  "_lame_encode_buffer_float", referenced from:
      _mp3lame_encode_frame in libavcodec.a(libmp3lame.o)
  "_lame_encode_buffer_int", referenced from:
      _mp3lame_encode_frame in libavcodec.a(libmp3lame.o)
  "_lame_encode_buffer_interleaved", referenced from:
      _mp3lame_encode_frame in libavcodec.a(libmp3lame.o)
  "_lame_encode_flush", referenced from:
      _mp3lame_encode_frame in libavcodec.a(libmp3lame.o)
  "_lame_get_encoder_delay", referenced from:
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
  "_lame_get_framesize", referenced from:
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
  "_lame_init", referenced from:
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
  "_lame_init_params", referenced from:
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
  "_lame_set_VBR", referenced from:
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
  "_lame_set_VBR_quality", referenced from:
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
  "_lame_set_bWriteVbrTag", referenced from:
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
  "_lame_set_brate", referenced from:
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
  "_lame_set_disable_reservoir", referenced from:
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
  "_lame_set_in_samplerate", referenced from:
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
  "_lame_set_mode", referenced from:
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
  "_lame_set_num_channels", referenced from:
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
  "_lame_set_out_samplerate", referenced from:
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
  "_lame_set_quality", referenced from:
      _mp3lame_encode_init in libavcodec.a(libmp3lame.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [lib/libopencv_highgui.2.4.2.dylib] Error 1
make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2
make: *** [all] Error 2

To solve this problem we have to change modules/highgui/CMakeLists.txt and add just before if(HAVE_FFMPEG) (on line 158) the following text:

if(APPLE)
  list(APPEND HIGHGUI_LIBRARIES ${BZIP2_LIBRARIES} -lmp3lame -lfaac -lbz2)
endif(APPLE)

Or we could apply this patch to OpenCV 2.4.2:

--- modules/highgui/CMakeLists.txt.old  2012-07-27 15:46:52.000000000 +0200
+++ modules/highgui/CMakeLists.txt      2012-07-27 15:47:05.000000000 +0200
@@ -155,6 +155,10 @@
   list(APPEND HIGHGUI_LIBRARIES m3api)
 endif(HAVE_XIMEA)
 
+if(APPLE)
+  list(APPEND HIGHGUI_LIBRARIES ${BZIP2_LIBRARIES} -lmp3lame -lfaac -lbz2)
+endif(APPLE)
+
 if(HAVE_FFMPEG)
   if(UNIX AND BZIP2_LIBRARIES)
     list(APPEND HIGHGUI_LIBRARIES ${BZIP2_LIBRARIES})

Now we can configure, compile and install it as usual. If you want to know more configuration options of cmake for OpenCV you can have a look at “Install OpenCV 2.3.1a in Mac OS X 10.6“.

mkdir build
cmake ..
make
sudo make install
cd ..

Source: http://blog.yageek.net/2012/06/compile-opencv-241-on-mac-os-x-lion.html

Use OpenCV in Xcode 4 for an Object Oriented Mac OS X Application

First of all you have to install OpenCV in Mac OS X. Then in Xcode create a new Mac OS X Application project of type “Command Line Tool”.

Set a “Product Name” and choose “Foundation” for “Type” option.

In the “Build Settings” tab (clicking on the projects blue icon on the left) add following values for those options:

Inside “Search Paths”:
Header Search Paths: /usr/local/include
Library Search Paths: /usr/local/lib
Inside “Linking”:
Other Linker Flags: -lopencv_core -lopencv_highgui -lopencv_imgproc

NOTE: You can add more libraries in “Other Linker Flags” to add more OpenCV functionality, for example:

-lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_contrib -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_ml -lopencv_objdetect -lopencv_video

If you want to also use the C functions available in OpenCV change inside “Build Options” the next parameter:

Compiler for C/C++/Objetive-C: LLVM GCC 4.2

Change the extension of “main.m” file to “main.mm“. This makes the code to be interpreted as Objetive-C++ and not only as Objetive-C.

Now (VERY IMPORTANT) inside the .pch file of the “Supporting Files” folder add BEFORE any other import the following code:

#ifdef __cplusplus
    #import "opencv2/opencv.hpp"
#endif

This is because OpenCV has a MIN macro defined that also exists in Apple framework, and if you don’t have the one in OpenCV you may have errors like “LLVM GCC 4.2 Error: Statement-expressions are allowed only inside functions” or “opencv2/core/core.hpp:433: error: statement-expressions are allowed only inside functions“. The import in surrounded by that “ifdef” so the files that don’t use Objetive-C++ code can maintain the “.m” extension and not be forced to change all them to “.mm“.

Fill “main.mm” with this example code, that is in fact the Object Oriented version code of the previous example in Use OpenCV on Xcode 4 for a Mac OS X Application:

//
// main.mm
// OpenCVOOTest
//
// Created by __MyName__ on 16/09/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import 
#import "opencv2/opencv.hpp"

using namespace cv;

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    Mat img(cv::Size(640, 480), CV_8UC3);
    
    circle(img, cv::Point(320, 240), 100, Scalar(255, 0, 0, 255), 5, 8, 0);
    
    namedWindow("OpenCV Window", CV_WINDOW_NORMAL);

    imshow("OpenCV Window", img);
    
    waitKey(0);
    
    [pool drain];
    return 0;
}

If you have this error while linking:

"Undefined symbols for architecture x86_64:"

Followed by multiple function names, you have to change the programs architecture to i386. Inside “Build Settings” of the project change the option “Arquitectures” to “32-bit Intel”. This by itself may not solve the problem, as OpenCV must be also compiled for a i386 32 bits architecture.

Sources: http://stackoverflow.com/questions/3810800/how-to-include-opencv-in-cocoa-application
http://computer-vision-talks.com/2011/03/opencv-build-script-with-xcode-4-support-is-here

Install OpenCV 2.3.1a in Mac OS X 10.6

Download OpenCV 2.3.1a and unpack it:

curl -L -O http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.3.1/OpenCV-2.3.1a.tar.bz2
tar xzvpf OpenCV-2.3.1a.tar.bz2

Then configure and compile it:

cd OpenCV-2.3.1
mkdir build
cd build
cmake -G "Unix Makefiles" ..
make
sudo make install

You can use different options when executing cmake:

  • -D BUILD_EXAMPLES=ON -> Compile example programs.
  • -D BUILD_TESTS=OFF -> Don’t compile tests.
  • -D BUILD_NEW_PYTHON_SUPPORT=OFF -> Don’t compile new Python support.
  • -D WITH_CARBON=ON -> (mostly for Leopard and below) To use Carbon instead of Cocoa (since version r2909).
  • -D WITH_QUICKTIME=ON -> (mostly for Leopard and below) To use QuickTime for the I/O of Video instead of QTKit (since version r2924). If you use Snow Leopard and need Carbon and QuickTime, you must also specify -D CMAKE_OSX_ARCHITECTURES=i386, -D CMAKE_C_FLAGS=-m32 and -D CMAKE_CXX_FLAGS=-m32.

If you don’t have cmake installed download a “binary distribution” from cmake.org. For example http://www.cmake.org/files/v2.8/cmake-2.8.7-Darwin64-universal.dmg.

You can compile OpenCV for an specific architecture using CMAKE_OSX_ARCHITECTURES with those values:

i386
x86_64
ppc
ppc64

For example:

cmake -G "Unix Makefiles" -D CMAKE_OSX_ARCHITECTURES=i386 ..

To create a “universal build” use both i386 and ppc:

cmake -G "Unix Makefiles" -D CMAKE_OSX_ARCHITECTURES=i386 ppc ..

If you want to compile for a 32 bit architecture is mandatory to add -m32 to C and CXX flags. If you don’t add it you could end up with this warning:

created and used with differing settings of '-m32'

To solve this use -m32 option in CMAKE_C_FLAGS and CMAKE_CXX_FLAGS:

cmake -G "Unix Makefiles" -D CMAKE_OSX_ARCHITECTURES=i386 -D CMAKE_C_FLAGS=-m32 -D CMAKE_CXX_FLAGS=-m32 ..

Use OpenCV in Eclipse CDT

Install OpenCV (see previous posts for Ubuntu 10.10, Ubuntu 11.10, Ubuntu 12.04 and OpenCV 2.4 in Ubuntu 12.04).

Install Eclipse CDT:

sudo apt-get install eclipse-cdt

Open Eclipse CDT and select a workspace (it’s just a path to store projects together). Create a new “C++ Project”, give it a name and select “Empty Project” on “Project type” and “Linux GCC” on “Toolchains”.

Right click on the project folder and select “Properties”. Go to “C/C++ Build->Settings”. Then, inside “Tool Settings” tab, go to “GCC C++ Compiler->Includes” and fill “Include paths (-l)” with the path of your OpenCV include installation. If you installed OpenCV from the source code it will possibly be “/usr/local/include/opencv2” (or the path you entered in CMAKE_INSTALL_PREFIX when configuring with CMake), but if you installed from the repositories it should be “/usr/include/opencv2“.

Now go to “GCC C++ Linker->Libraries” and fill “Libraries (-l)” with (at least) those OpenCV libraries:

opencv_core
opencv_highgui

And “Library search paths (-L)” with the path location of your OpenCV installation libraries. As before, if you installed from the source it should be “/usr/local/lib” (same here with CMAKE_INSTALL_PREFIX) and if you installed with repositories “/usr/lib“.

Now it’s time to code. Go to “File->New->Source file” and create a new file called “main.cpp“. Fill it with:

#include "opencv.hpp"

int main(int argc, char* argv[])  {

        IplImage* img = cvCreateImage( cvSize( 640, 480 ), IPL_DEPTH_8U, 3 );

        cvCircle( img, cvPoint( 320, 240 ), 100, cvScalar( 255, 0, 0 ), 5 );

        cvNamedWindow( "OpenCV Window", CV_WINDOW_NORMAL );
        cvShowImage( "OpenCV Window", img );

        cvWaitKey(0);

        cvDestroyWindow( "OpenCV Window" );
        cvReleaseImage( &img );

        return 0;
}

This code will just show a black window with a blue circle in it and end when you press any key. Not very fancy but we just want to check if all runs OK.

Eclipse CDT comes with an option to build automatically, but I always disable it deselecting “Project->Build Automatically”. Build all by pressing Control+B or going to “Project->Build All”. Before debugging/running our application we need to configure the debugging/running environment. Go to “Run->Debug Configurations…” and do a double click on “C/C++ Application”. It will create a new debug configuration, but “C/C++ Application” parameter may not be automatically configured (this happend to me), so use “Browse…” button to select a “Project”, select your current project, and now (if your application binary program does exist) all should be OK to push “Debug” button.

You should now be able to run and debug your OpenCV application. Using any other library with Eclipse CDT is pretty much the same.

Install OpenCV 2.4 in Ubuntu 12.04

Installing OpenCV 2.4 from source code as I did with OpenCV 2.3.1a in Ubuntu 10.10 went flawlessly, didn’t need changing any of the preexisting ffmpeg libraries as I had to in Ubuntu 11.10.

You can download the source code for it from here.

But be aware of the changes in new version. If you are updating from a previous version of OpenCV be careful because they have introduced multiple changes and your code might not work. This are some of the biggest changes:

  • OpenCV now provides pretty complete build information via (surprise) cv::getBuildInformation().
  • reading/writing video via ffmpeg finally works and it’s now available on MacOSX too.
  • MOG2 background subtraction by Zoran Zivkovic was optimized using TBB.
  • >20 more bugs in our bug tracker have been closed (http://code.opencv.org/projects/opencv/roadmap).
  • Asus Xtion is now properly supported for HighGUI. For now, you have to manually specify this device by using VideoCapture(CV_CAP_OPENNI_ASUS) instead of VideoCapture(CV_CAP_OPENNI).
  • Some of the old functionality from the modules imgproc, video, calib3d, features2d, objdetect has been moved to legacy.
  • CMake scripts have been substantially modified. Now it’s very easy to add new modules – just put the directory with include, src, doc and test sub-directories to the modules directory, create a very simple CMakeLists.txt and your module will be built as a part of OpenCV. Also, it’s possible to exclude certain modules from build (the CMake variables “BUILD_opencv_” control that).
  • The new very base cv::Algorithm class has been introduced. It’s planned to be the base of all the “non-trivial” OpenCV functionality.
  • features2d API has been cleaned up. There are no more numerous classes with duplicated functionality. The base classes FeatureDetector and DescriptorExtractor are now derivatives of cv::Algorithm. There is also the base Feature2D, using which you can detect keypoints and compute the descriptors in a single call. This is also more efficient.
  • SIFT and SURF have been moved to a separate module named nonfree to indicate possible legal issues of using those algorithms in user applications. Also, SIFT performance has been substantially improved (by factor of 3-4x).
  • The current state-of-art textureless detection algorithm, Line-Mod by S. Hinterstoisser, has been contributed by Patrick Mihelich. See objdetect/objdetect.hpp, class Detector.
  • 3 face recognition algorithms have been contributed by Philipp Wagner. Please, check opencv/contrib/contrib.hpp, FaceRecognizer class, and opencv/samples/cpp/facerec_demo.cpp.
  • 2 algorithms for solving PnP problem have been added. Please, check flags parameter in solvePnP and solvePnPRansac functions.
  • Enhanced LogPolar implementation (that uses Blind-Spot model) has been contributed by Fabio Solari and Manuela Chessa, see opencv/contrib/contrib.hpp, LogPolar_* classes and opencv/samples/cpp/logpolar_bsm.cpp sample.
  • A stub module photo has been created to support a quickly growing “computational photography” area. Currently, it only contains inpainting algorithm, moved from imgproc, but it’s planned to add much more functionality.
  • Another module videostab (beta version) has been added that solves a specific yet very important task of video stabilization. The module is under active development. Please, check opencv/samples/cpp/videostab.cpp sample.
  • findContours can now find contours on a 32-bit integer image of labels (not only on a black-and-white 8-bit image). This is a step towards more convenient connected component analysis.
  • Canny edge detector can now be run on color images, which results in better edge maps.

Source of changelog: Computer vision talks.

  • Page 1 of 2
  • 1
  • 2
  • >