NAR (Nghia’s Augmented Reality)

This my latest and greatest markerless/natural feature augmented reality demo. This new code is called NAR Demo, short for Nghia’s Augmented Reality Demo, for a lack of a better name 🙂 So what’s the difference between NAR Demo and MarkerlessARDemo? A bucket load, just to name a few:

  • No CUDA specific code, anyone can run it now (woohoo!)
  • Irrlicht Engine for cross platform GUI, the power of a full featured game engine!
  • Multi-threaded design for real-time processing
  • Replaced FAST feature with more robust Difference of Gaussian blob features
  • Experimental optical flow code, this improves stability a lot (less jittering)
  • Bug fixes from MarkerlessARDemo
  • LOTS of code cleaning

NAR Demo is meant to serve as an educational tool, there are no plans to make it anything beyond that.

I’ve made a PDF document that focuses on the computer vision aspect of NAR. It’s general and not code specific and might be useful if are writing your own AR system. Download NAR_Demo.pdf, also found in the doc directory of the 7z package.


Last update 05/02/2012


  • Minor bug fixes in DOG.cpp
  • Added computer vision documentation in doc (work in progress)
Download LinuxDownload Windows
NAR_Demo- (7.2MB) Use 7zip to decompress. This file includes both a Linux (CodeBlocks) and Visual Studio 2010 project. I’ve only tested it on a 64bit machine. There is a pre-compiled Windows 64bit binary in the root folder if you want to test it out without compiling. But first please read the usage section down below. IMPORTANT: The OpenCV Windows dlls I provided have been compiled on an Intel i7. If you have a CPU that doesn’t support SSE3 instructions it might crash. In which case, you will have to compile your own version of OpenCV.


The code is also available on GitHub


To compile you need the following libraries installed

If you are using the Visual Studio project you will have to edit the project and point it to the directories of the libraries mentioned above.

32 bit systems: If you are compiling for a 32 bit system you need to edit NAR/NAR_Config.h and comment out #define USE_SSE4. The POPCNT assembly instruction seems to work only with 64 bit compilers.


The executable NAR_Demo or NAR_Demo.exe can be found in the root directory. It accepts no arguments, but first you have to provide it an image of the object you want to detect at the following path:


There is no default AR_object.png provided, NAR_Demo WILL NOT run without this file.

I recommend finding some software that can view your webcam/camera, hold the object in front, take a screenshot, and crop as required. The image should be no bigger than your video resolution. The object MUST be planar/flat for it work correctly.

Once NAR_Demo is running, just hold up your object for detection.

You might notice it’s very slow (< 4 FPS) when the object is not detected. This is due to OpenCV’s cv::findHomography function, which lacks an option to specify the maximum number of RANSAC iterations, it’s hard coded to 2000. I’ve submitted a suggestion to OpenCV’s bug track system, here’s hoping they read it.


Simplified BSD. See LICENSE.txt in the root directory.

34 thoughts on “NAR (Nghia’s Augmented Reality)”

    1. NAR Demo is designed to hog the CPU and use every last bit of it 🙂 I don’t have any settings to reduce the CPU usage but maybe you can try lowering the priority of the process?

      The camera should turn off if you close the program cleanly. Hit ESCAPE to close the program, it should turn the camera off. I don’t explicitly close the camera, I assume OpenCV will.

  1. Dear doctor Nghia,

    I would like to report that NAR can be built on MacOS X Lion.
    Below is what to do to build.
    It’s easier for me (and maybe others) to contribute if you use GitHub ;).


    NAR needs OpenCV, Boost, and Irrlicht.

    Install OpenCV to $HOME/opt/opencv-2.3.1 and Boost to $HOME/opt/boost_1_48_0


    Build Irrlicht 1.7.2 (the latest 1.7.3 has build error on MacOS X):

    mkdir -p $HOME/src
    cd $HOME/src
    svn export irrlicht-1.7.2

    Open irrlicht-1.7.2/source/Irrlicht/MacOSX/MacOSX.xcodeproj with Xcode.

    Base SDK 10.6
    LLVM GCC 4.2

    Edit Scheme, select Release
    Select menu Archive, save libIrrlicht.a to $HOME/src/irrlicht-1.7.2/lib/MacOSX/libIrrlicht.a


    Edit NAR:


    #ifdef _WIN32
    #elif __linux__
    #elif __APPLE__
    #error “Platform not supported or tested”


    #ifdef _WIN32
    #elif (__linux__ || __APPLE__)
    #define __cpuid(out, infoType)\
    asm(“cpuid”: “=a” (out[0]), “=b” (out[1]), “=c” (out[2]), “=d” (out[3]): “a” (infoType));
    #error “Platform not supported or tested”


    Change to AR_Demo/NAR directory, then:

    g++ `pkg-config –cflags opencv` `pkg-config –libs opencv` -msse4.1 -msse4.2 -I$HOME/opt/boost_1_48_0/include -L$HOME/opt/boost_1_48_0/lib -lboost_system -lboost_filesystem -lboost_thread -I/$HOME/src/irrlicht-1.7.2/include $HOME/src/irrlicht-1.7.2/lib/MacOSX/libIrrlicht.a -framework Carbon -framework Cocoa -framework OpenGL -framework IOKit *.cpp ../*.cpp

    a.out will be created.

  2. Thanks,
    I’ve just commented at GitHub about version number.

    I would like to ask about homography.

    At “Pose Estimation For Planar Target” (, it seems that only model_3D and iprts parameters are enough.

    Why in NAR NAR::PoseEstimation method needs the homography matrix?
    cv::Mat image_pts = m_inv_camera_intrinsics * H * m_AR_object_image_pts

    1. image_pts will always be the 4 corner points of the AR object. I decided to use the 4 corner points, which requires the Homography matrix, instead of all the points for consistent speed. I’m not how that affects the accuracy because the homography calculation is quite good, OpenCV does a refining process after RASNAC.

  3. Hi,

    I had no idea it was even possible to run it in Python! Sadly, I don’t program in Python so am not able to help you 🙁

  4. Dr Nghia. After several configuration, I have tried to run your code several time, in both Debug and Release mode for windows 7 32bit with VS2010. I saw my camera is running after the Learning pose ended, but It could show any captured image in the NAR, the window is black and FindRADObject always 0ms, for debug mode it’s sometimes give mess buffer full. I couldn’t figure out what’s wrong. I didn’t changed anything in your code. Thanks.

    1. I find camera support in OpenCV to be rather unreliable. I’ve had some good and bad experiences with different webcam. You’re best option is to keep up to date with the latest OpenCV and hope they fix the problem, or try a different webcam. Or if you have time you can attempt to debug it. I had to do this once. If it returns a blank image it indicates some error in the webcam driver, which you can trace back into the OpenCV code. But it’s rather time consuming,

      1. Thank Dr Nghia. After updating Opencv from 2.3.1 to 2.4.1 I could run the code. I think there some problem with Microsoft Lifecam driver in Opencv2.3.1. When I run it without threads, it still working. Anyways, your NAR is very nice AR demo with very intuitive documentation.

  5. i tryed to compile your application on ubuntu i got the following errors
    i’m on a 32bits version
    i already installer boost opencv and irrlich
    i have pentium dual-core CPU
    thanks for you help

    ————– Clean: Release in NAR_Demo —————

    Cleaned “NAR_Demo – Release”

    ————– Build: Release in NAR_Demo —————

    Compiling: NAR/AlphaBetaTracker.cpp
    Compiling: NAR/BaseThread.cpp
    Compiling: NAR/CpuID.cpp
    Compiling: NAR/DoG.cpp
    Compiling: NAR/ExtractFeatureThread.cpp
    In file included from /home/khayati/Travail/realiteAugmente/Nar/NAR_Demo/NAR/ExtractFeatureThread.cpp:7:0:
    /usr/lib/gcc/i686-linux-gnu/4.6/include/smmintrin.h:32:3: erreur: #error “SSE4.1 instruction set not enabled”
    Process terminated with status 1 (0 minutes, 5 seconds)
    1 errors, 0 warnings

    1. You probably don’t have SSE4.1 support on your CPU. Edit NAR_Demo/NAR/NAR_Config.h, comment out the line #define USE_SSE4.

      1. i already done that

        now i have strange more errors messages

        ————– Clean: Debug in NAR_Demo —————

        Cleaned “NAR_Demo – Debug”

        ————– Build: Debug in NAR_Demo —————

        Compiling: NAR/AlphaBetaTracker.cpp
        Compiling: NAR/BaseThread.cpp
        Compiling: NAR/CpuID.cpp
        In file included from /home/khayati/Travail/realiteAugmente/Nar/NAR_Demo/NAR/CpuID.cpp:1:0:
        /home/khayati/Travail/realiteAugmente/Nar/NAR_Demo/NAR/CpuID.h:21:3: attention : ISO C++ interdit les structures anonymes [-pedantic]
        /home/khayati/Travail/realiteAugmente/Nar/NAR_Demo/NAR/CpuID.h:108:3: attention : ISO C++ interdit les structures anonymes [-pedantic]
        Compiling: NAR/DoG.cpp
        Compiling: NAR/ExtractFeatureThread.cpp
        Compiling: NAR/KTree.cpp
        In file included from /home/khayati/Travail/realiteAugmente/Nar/NAR_Demo/NAR/KTree.cpp:1:0:
        /home/khayati/Travail/realiteAugmente/Nar/NAR_Demo/NAR/KTree.h: In member function ‘float KTree::DistanceSq(const float*, const float*) const’:
        /home/khayati/Travail/realiteAugmente/Nar/NAR_Demo/NAR/KTree.h:77:5: erreur: ‘__m128’ was not declared in this scope
        /home/khayati/Travail/realiteAugmente/Nar/NAR_Demo/NAR/KTree.h:77:12: erreur: expected ‘;’ before ‘sum’
        /home/khayati/Travail/realiteAugmente/Nar/NAR_Demo/NAR/KTree.h:81:16: erreur: expected ‘;’ before ‘aa’
        /home/khayati/Travail/realiteAugmente/Nar/NAR_Demo/NAR/KTree.h:82:16: erreur: expected ‘;’ before ‘bb’
        /home/khayati/Travail/realiteAugmente/Nar/NAR_Demo/NAR/KTree.h:83:16: erreur: expected ‘;’ before ‘cc’
        /home/khayati/Travail/realiteAugmente/Nar/NAR_Demo/NAR/KTree.h:84:9: erreur: ‘cc’ was not declared in this scope
        /home/khayati/Travail/realiteAugmente/Nar/NAR_Demo/NAR/KTree.h:84:31: erreur: ‘_mm_mul_ps’ was not declared in this scope
        /home/khayati/Travail/realiteAugmente/Nar/NAR_Demo/NAR/KTree.h:85:9: erreur: ‘sum’ was not declared in this scope
        /home/khayati/Travail/realiteAugmente/Nar/NAR_Demo/NAR/KTree.h:85:9: note: suggested alternative:
        /usr/include/opencv2/core/core.hpp:2009:32: note: ‘cv::sum’
        /home/khayati/Travail/realiteAugmente/Nar/NAR_Demo/NAR/KTree.h:85:33: erreur: ‘_mm_add_ps’ was not declared in this scope
        /home/khayati/Travail/realiteAugmente/Nar/NAR_Demo/NAR/KTree.h:90:24: erreur: ‘sum’ was not declared in this scope
        /home/khayati/Travail/realiteAugmente/Nar/NAR_Demo/NAR/KTree.h:90:24: note: suggested alternative:
        /usr/include/opencv2/core/core.hpp:2009:32: note: ‘cv::sum’
        /home/khayati/Travail/realiteAugmente/Nar/NAR_Demo/NAR/KTree.h:90:27: erreur: ‘_mm_storeu_ps’ was not declared in this scope
        /home/khayati/Travail/realiteAugmente/Nar/NAR_Demo/NAR/KTree.h: At global scope:
        /home/khayati/Travail/realiteAugmente/Nar/NAR_Demo/NAR/KTree.h:75:14: attention : unused parameter ‘a’ [-Wunused-parameter]
        /home/khayati/Travail/realiteAugmente/Nar/NAR_Demo/NAR/KTree.h:75:14: attention : unused parameter ‘b’ [-Wunused-parameter]
        Process terminated with status 1 (0 minutes, 7 seconds)
        19 errors, 0 warnings

  6. i tryed to compile your application on VS2010 and i got the following errors
    i’m on a 32bits version
    i already installer boost opencv and irrlich
    i have pentium dual-core CPU
    thanks for you help

    BaseThread.obj : error LNK2022: metadata operation failed (8013119F): Existing TypeRef should have a corresponding TypeDef(dummy),but it doesn’t: (0x01000019)。
    1>LINK : fatal error LNK1215: metadata operation failed (8013119F):

          1. Thanks for you reply. I have found the problem.

            In the option: Common Language Runtime support, I chose the /clr which is false.

          2. Hello. There is another question for the project. I run the source code in a 64 bit computer. However, it has the LNK 2019 error.

            error LNK2019: Unresolved external symbol __imp__createDevice.

          3. Not sure on this one. Maybe you’re not linking to the correct 64bit version of Irrlicht?

      1. I am sorry to bother you that here is another question. The processing of compiling and linking are both succuess in 64bit system. But when the project is runing at the code “gui::IGUIFont* font = device->getGUIEnvironment()->getFont(“media/bitstream_font.xml”);” a dialog is pop up:” NAR_Demo.exe 0x… the most possible abnormal 0xc0xC0000005 …” What can I do for it? Thanks a lot

        1. Hmmm, that file is a relative path so if you run in Visual Studio make sure the path is relative to the exe. Else hard code the full path to make sure.

  7. it’s great to open such technology. if I use haarcascade to detecte front side of hand than I can use this or I have to develope code for this by using contour detection???your help is really greatful for me

Leave a Reply

Your email address will not be published. Required fields are marked *