Category Archives: Computer Vision - Page 2

osgART 2.0 RC3 with OpenSceneGraph 2.9.7 or later (including 3.0.0)

Long time ago I had problems using osgART 2.0 RC3 with OpenSceneGraph 2.9.7 or later, I couldn’t get the video feed from the webcam on the background so I only used OpenSceneGraph 2.9.6. Today I found out that my osgART program could work with OpenSceneGraph 2.9.7 or later (current version 3.0.0 works too!), but as I always used 2.9.6 I didn’t know I have founded the solution to this problem!

NOTE: If you want to install OpenSceneGraph 3.0.0 in Ubuntu 11.10 or Ubuntu 12.04 you’ll have to use the patches I’ve created and uploaded to my github repository.

The key of the problem is that when you configure the video backgrounds osgART::VideoGeode you can use TEXTURE_2D or TEXTURE_RECTANGLE. If you use TEXTURE_RECTANGLE it will work on newer versions of OpenSceneGraph. You can look at line 40 of “examples/osgartsimple/osgartsimple.cpp” in osgART 2.0 RC3 source code folder:

osg::Group* createImageBackground(osg::Image* video) {
        osgART::VideoLayer* _layer = new osgART::VideoLayer();
        _layer->setSize(*video);
        osgART::VideoGeode* _geode = new osgART::VideoGeode(osgART::VideoGeode::USE_TEXTURE_2D, video);
        addTexturedQuad(*_geode,video->s(),video->t());
        _layer->addChild(_geode);
        return _layer;
}

If you installed OpenSceneGraph 2.9.7 or later and compiled osgART 2.0 RC3 as it comes in the original code, when you execute this example from “/usr/local/bin/osgartsimple” you will get this as you show the “Hiro” pattern to the camera:

If you change previous code this way:

osg::Group* createImageBackground(osg::Image* video) {
        osgART::VideoLayer* _layer = new osgART::VideoLayer();
        _layer->setSize(*video);
        osgART::VideoGeode* _geode = new osgART::VideoGeode(osgART::VideoGeode::USE_TEXTURE_RECTANGLE, video);
        addTexturedQuad(*_geode,video->s(),video->t());
        _layer->addChild(_geode);
        return _layer;
}

Then compile and install osgART 2.0 RC3 again, “/usr/local/bin/osgartsimple” will work OK.

ARToolKit with OpenKinect using the IR camera

More than one year ago I started adding Kinect support to ARToolKit using libfreenect. I had some results but didn’t have enough time to publish a fully tested version of it. Now I know it ins’t very useful because Ubuntu already detects Kinect as a webcam (creating a /dev/videoX device for it, or at least that happend to me with Ubuntu 12.04 UPDATE: I figured out that this happens because of the gspca_kinect module introduced in Linux 3.0 kernel), so you can use it with ARToolKit via the GStreamer video input. Still I wanted to learn more on how ARToolKit works on its inside. Now I’ve added some more features and support not only for the RGB camera but also for the IR camera, tilt and LED.

The nature of the IR camera makes detecting markers very difficult as they are very blurry when not very close to the Kinect, but hey it’s been fun hacking with libfreenect. I’ll try to publish the code ASAP. I still want to test it more.

Install osgART 2.0 RC3 with OpenSceneGraph 2.8.3 or 2.9.6 with Collada support in Ubuntu 12.04

Before installing osgART we need to install ARToolKit, collada-dom and OpenSceneGraph so look at those post before continuing with this:

Once you have installed the dependencies for osgART download osgART 2.0 RC3 code from here and uncompress it. Run the following commands:

mkdir build
cd build
cmake .. -DCMAKE_CXX_FLAGS=-fpermissive -DCMAKE_MODULE_LINKER_FLAGS=-lgstreamer-0.10 -DCMAKE_SHARED_LINKER_FLAGS=-lgstreamer-0.10
make
sudo make install

You can remove the “-DCMAKE_CXX_FLAGS=-fpermissive” option if you add the following includes in the “include/osgART/Utils” file after “#include “:

#include 
#include 

Configure the ARTOOLKIT_CONFIG variable (you should have this already configured when installing ARToolKit):

export ARTOOLKIT_CONFIG="v4l2src device=/dev/video0 use-fixed-fps=false ! ffmpegcolorspace ! capsfilter caps=video/x-raw-rgb,bpp=24 ! identity name=artoolkit ! fakesink"

And run the example program while showing the “Hiro” pattern to the camera:

cd /usr/local/bin/
./osgartsimple

If you get this error:

Plugin '-1' unknown!
Could not initialize video plugin!

It is caused because the osgART libraries weren’t correctly copied to the library folder (“/usr/local/lib“) and/or the OSG plugins folder (“/usr/local/lib/osgPlugins-X.X.X“). Verify that a file called “libosgART.so” is in “/usr/local/lib” and that the files “osgdb_osgart.so“, “osgart_tracker_artoolkit2.so” and “osgart_video_artoolkit2.so” are in the OSG plugins folder (that should be “/usr/local/lib/osgPlugins-2.8.3” or “/usr/local/lib/osgPlugins-2.9.6” depending on the version of OSG you installed). Be aware that if you install different versions of OSG at the same time the osgART plugin may no be copied to the plugin folder of the OSG version you want. Also you may run this just in case:

sudo ldconfig /etc/ld.so.conf

If you see a black background it is caused because of a small bug in ARToolKit. It can be fixed in ARToolKit changing “ar2VideoCapNext” function on “lib/SRC/VideoGStreamer/video.c line 378 to return 0 instead of TRUE OR by changing osgART file “src/osgART/Video/ARToolKit/ARToolKitVideo.cpp” on line 310 from this:

if (0 == ar2VideoCapNext(video))

To this:

if (ar2VideoCapNext(video))

If you want to have a smoother marker detection on osgART you can change the source code on file “src/osgART/Traker/ARToolKit/SingleMarker.cpp” and uncomment the line 76 and comment the line 77. I mean changing this:

//arGetTransMatCont(markerInfo, patt_trans, patt_center, patt_width, patt_trans);
arGetTransMat(markerInfo, patt_center, patt_width, patt_trans);

To this:

arGetTransMatCont(markerInfo, patt_trans, patt_center, patt_width, patt_trans);
//arGetTransMat(markerInfo, patt_center, patt_width, patt_trans);

Sources: http://projects.springlobby.info/issues/1575
https://github.com/bitcoin/bitcoin/pull/456
https://code.ros.org/trac/opencv/ticket/1020
http://forum.openscenegraph.org/viewtopic.php?t=7755

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

Use OpenCV in Xcode 4 for a 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

It’s also necessary to change inside “Build Options” the next parameter or the C functions of OpenCV will not work:

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

In main.m write this example code, compile (command+B) and execute (comnand+R).

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

#import 
#import "opencv/cv.h"
#import "opencv/highgui.h"

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    IplImage* img = cvCreateImage( cvSize( 640, 480 ), IPL_DEPTH_8U, 3 );
    
    cvCircle( img, cvPoint( 320, 240 ), 100, cvScalar( 255, 0, 0 , 255 ), 5, 8, 0 );
    
    cvNamedWindow( "OpenCV Window", CV_WINDOW_NORMAL );
    cvShowImage( "OpenCV Window", img );
    
    cvWaitKey(0);
    
    cvDestroyWindow( "OpenCV Window" );
    cvReleaseImage( &img );
    
    [pool drain];
    return 0;
}

If it fails compiling it’s posible that you need to add the “opencv/cv.h” import first of all, but in order to be really the first one you have to put it inside the .pch file (the one with the same name as your project) in the “Supporting Files” folder:

#ifdef __OBJC__
    #import "opencv/cv.h"
    #import 
#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”.

  • Page 2 of 4
  • <
  • 1
  • 2
  • 3
  • 4
  • >