CreativeApplications.Net reports innovation and catalogues projects, tools and platforms relevant to the intersection of art, media and technology. Read more here.

t f r

Search

Augmented Reality with Processing [Tutorial, Processing]

All of the visuals in the above video were created using NyArtoolkit for Processing. NyARToolkit is an augmented reality toolkit built with 100% pure Java. It is derived from ARToolkit-2.72.1. Like Processing itself it’s open source and free! In this tutorial you will learn how to use it to place computer generated imagery correctly onto real world footage. To do this in real-time NyArtoolkit uses markers – black and white images – to determine the three-dimensional position and orientation in the real world. Most likely you will have seen something like this before, but now you will learn how to do it yourself using freely available software.

All right so let’s start with the general setup. What do you need to get started?

1. Processing 1.5.1
A Processing tutorial wouldn’t really work without Processing, now would it? So if you haven’t already, go HERE and download Processing for your OS. These examples were written for Processing 1.5.1 (the current stable build) but also with the upcoming Processing 2.0 in mind. I have personally tested all these examples in Processing 2.0a4 (alpha 4) and they work. When the time comes, you will probably notice that the default examples from the NyArtoolkit break in the Processing 2.0, due to the changes in the internal structure of the renderers. P3D is gone and there is a new (and vastly improved) OPENGL renderer in town. Go HERE to see the major changes in Processing 2.0. Just know that I have done my utmost to make sure these augmented reality code examples should run both under the current and future Processing builds.

2. NyArtoolkit for Processing 1.1.6
What it’s all about! Go HERE and download nyar4psg-1.1.6.zip. If you don’t know how to install contributed libraries in Processing, go HERE for instructions.

3. GSVideo 1.0.0
The second and third example use GSVideo to get a webcam stream into Processing. You could use alternatives if you want. I have chosen GSVideo because it works well and also because it will be integrated into Processing 2.0 as the new default video/capture library. So future readers, you might not need to seperately download this. For all of us living in the now, go HERE and download GSVideo for your OS. Again, if you don’t know how to install contributed libraries in Processing, go HERE for instructions.

4. Patterns
To make this whole thing work you will need black and white patterns. The NyArtoolkit includes two patterns (Hiro and Kanji), but these MultiMarker code examples can handle many more. There are a couple of online services that let you make your own markers and train them. But in my opinion, this is too much hassle. Fortunately you can download an ARToolkit Patternmaker from the university of Utah, which comes with 100 patterns and the associated training files! Go HERE to download the ARToolkit Patternmaker. Unpack this file and you will find 100 images (patternMaker\examples\gif) and 100 corresponding pattern files (patternMaker\examples\ARToolKit_Patterns). Very useful and these are the ones I used in the video. Very important: rename the first 10 pattern files to two-digits so they are loaded correctly in alphanumeric order in Processing. Also, if you use for example marker number 30, make sure to increase the number of loaded markers in these code example with the numMarkers variable.

All right, time to recap. So at this point you have Processing, the NyArtoolkit and GSVideo installed. In addition, you have downloaded the Patternmaker and therefore are in possession of 100 quality markers and the corresponding .patt files. If this is correct, you are all set and ready to get started with the code examples. All code examples are fully commented and kept as straight-forward as possible in order to maximize the educational value.

Example 1: Basic
The first example is basic, but holds all of the important techniques that are necessary for more advanced uses of the NyArtoolkit. I’ve purposefully started with an example that is image based for two reasons. First, because it will allow you to focus on getting the library working without any webcam troubles. ;) Second, to show that the NyArtoolkit is in essence image-based. This makes it extremely flexible, because any input will work! Be it a static image, an image sequence, a webcam capture or a movie. This is for example how I made the movie accompanying this blog post.

If you input the following image (place it in the sketch’s data subdirectory)…

…into the first code example, you should end up with something like this…

Example 2: Dynamic
Time to get a little more dynamic. Both with regard to the input (webcam) and the output (moving graphics). Instead of a static box, this example shows a moving mountain terrain on each marker. I’ve added some suggestions in the code where you could tweak the look of this example. The mountain will grow (up to a certain height) while the marker is in sight and will flatten if the marker is no longer detected. You can see this behavior very clearly in the video above. Try it out yourself, by running the code example below in front of a webcam!

Example 3: OOP
The third example serves two purposes. First, you should realise that every (3D) sketch you can run in Processing, can be used in an augmented reality context. The only thing that AR changes is the perspective/camera. So every sketch which holds 3D shapes could work. To show this, I’ve taken an example that comes with Processing, the RGBCube, and integrated it – quite easily – into in AR context. The second purpose of this example, is to show the use of AR detection within an object-oriented (OOP) sketch. As sketches get a little more advanced, many people will turn to classes. It’s very easy to combine AR with classes. In this example it’s done through an ID, which is the link between the object (here a spinning RGBCube) and the corresponding marker. See the code how it works. In this example there are two part / tabs. The main sketch and the ARObject class. You will need both to run the example.

Main Sketch

ARObject

Troubleshooting & General Tips
- If you get an “Exception occurred at Multimarker.Multimarker” you probably haven’t changed the camPara String. In all the examples above it points to the location of the file on MY computer. You have to change it to the location of the file on YOUR computer. Also make sure you have (the latest) NyArtoolkit for Processing 1.1.6 installed.
- NullPointerException with regard to the patterns. Most likely the path to the patterns is incorrect. Use forward slashes in the pattern path. Don’t put a slash at the end.
- You get no error messages, but the sketch window remains grey. This means there is no incoming webcam imagery or perhaps no webcam at all. Make sure your webcam is working correctly.
- You get no error messages, the marker is clearly on the screen, but not detected. Most likely you haven’t loaded it’s pattern file. In the code examples you load the first ‘numMarkers’ from the list. Your marker apparantly is alphanumerically not within the range of loaded patterns. Make sure it is and it’s loaded.
- For bigger issues (errors, questions), please use the Processing forum.

I hope you have learned something and this tutorial has been useful to get you off to a running start with augmented reality. Of course this is only the tip of the iceberg. There is a whole world of possibilities. Good luck with these code examples and more!

Related Posts with Thumbnails


  • http://twitter.com/gPanevino G

    I think there’s something missing in example n.2, function drawMountains().
    for is bad formatted
    for (int i=0; i 0) { mountainGrowth[i] -= 0.05; }
    continue;

  • http://twitter.com/dGoligorsky ʎʞsɹoƃıןoƃ pıʌɐp

    Thanks for creating this tool and for documenting everything!  I’m trying to get the examples running and I’m getting an exception error in Example 1 in the “nya” definition for creating a new MultiMarker.  Error is “Exception occurred at Multimarker.Multimarker”

  • Amnon P5

    Yeah, you were right. WordPress ate part of the code ;-) due to some character interpretation issue or something. I’ve corrected the post above, should be all right now. Thanks for pointing it out.

  • Amnon P5

    The error “Exception occurred at Multimarker.Multimarker” means you are using an older version of the NyARToolkit. The MultiMarker class was introduced in the latest version. Download NyARToolkit 1.1.6.

  • Haris N

    I agree with the “G”.

    Same problem.

  • http://twitter.com/gPanevino G

    Ok now example 2 is working right, so cool! I think wordpress also cut some piece in example 3 in setup() function. 
    p.s. I was also getting Multimaker error before I customized paths definition in the code properly as for my Processing directory. 

  • Amnon P5

    Great to hear you’ve gotten example 2 to work! Indeed, after you pointed out the issue with that example, I noticed a large part of example 3 main sketch was missing as well. I’m working with Filip to get the correct code for that example up as well. So a little patience, but should be corrected asap. ;-D

  • http://www.creativeapplications.net Filip

    Just installed highlighter that should make reading code easier. We’ll get there eventually =]

  • Amnon P5

    All right everyone, to conclude the above discussion. The new highligher is awesome. It makes the code look good, but more importantly it keeps the code intact! I’ve corrected all the code in this blog post and personally tested it (by copying it from this page for a trial run) and everything worked. Good to see some interest in this topic so quickly ;D now you can get started with the correct code examples. Good luck!

  • http://twitter.com/ghostpressbed Derek J. Kinsman

    GStreamer is giving me nothing but trouble. Attempts at reinstalling it are failing. Need lunch. Derp. Otherwise, it’s a fantastic tutorial. The first example worked great.

  • Amnon P5

    Thanks Derek. If you have an alternative and/or preferred method of getting a webcam image into Processing (instead of GSVideo) you could of course use that. The changes to the examples would be negligable. As I wrote, NyArtoolkit is image-based, so it doesn’t really matter which route you take to get the image there. :D

  • http://twitter.com/MickD MickD

    Really well put together tutorial. Thanks.

  • InkRunner

    Hi! I’m getting the same  MultiMarker error but I got the latest NyARTtoolkit 1.1.6 :S can’t find the problem :(

  • Amnon P5

    InkRunner and others. I have a suggestions with regard to the MultiMarker error you could try that may fix it. Change the path of the String camPara to where the file is on your computer. I see on your (InkRunner) screenshot that both the camPara
    path and the pattern file path are exactly like I posted it here. But
    that is where they are located on my computer. You have to check where
    they are on YOUR computer and adapt the paths in the sketch accordingly.

    Some general suggestions to make sure the library is working:
    1. Make sure you have removed/deleted all older versions of the library from your libraries directory.
    2. Rename the main library directory to nyar4psg (when you unpack it the directory is named nyar4psg-1.1.6 so remove the last part of the name to make the directory name equal to the library’s jar filename).

  • Haris N

    As Amnon P5 mention you have to change the path for camPara and markers.
    Everything works fine :)
    http://www.screenr.com/1MQs

  • InkRunner

    you were totally right! :)

    Thank you very much! this gave me so much ideas! :) I’ll be sharing.

  • Adrianmb

    wow!! excellent tutorial ;)

  • Aart Rost

    Hi Amnon! Cool tutorial, thanks for sharing!

    To the guys with exceptions: It’s probably in the file paths. Just drop the files and folders in the data folder of the sketch and use dataPath if you need the full file path (only the patterns need that).

    This is the first example: http://aartrost.nl/meuk/augumented_reality1.zip rewritten to work without absolute paths. Should work everywhere if you’ve installed the libraries :)

  • Aart Rost

    Try renaming the “gstreamer-0.10″ folder to just “gstreamer”. Not sure if that works but that’s the first thing I would do. Numbers and dots in folder names can be troublesome

  • k.naresh89

    hello amnon p5…..but i would like to bring to your notice that even after making sure the nyartoolkit is at 1.1.6 i am still getting the error similar to 
    ʎʞsɹoƃıןoƃ pıʌɐp…”
    Exception occurred at Multimarker.Multimarker”
    any help would be appreciated
    and i sincerely thank you for your efforts to bring AR tuts to programmers….;)

  • Amnon P5

    To solve the
    MultiMarker error make sure to change the path of the
    String camPara to where the camera_para.dat file is on YOUR computer (because in the example code it points to the location where it is on my computer). Also see the earlier comments on this page about it.

  • http://twitter.com/tecnotic tecnotic

    Hi, thanks for the examples.

    Unfortunatly I obtain that error:”GLException: Method “glBlendEquation” not available”

    I am using Processing 2.0 and I think that I have installed correctly the libraries. Could you suggest me something to follow?

  • Amnon P5

    @ tecnotic: Processing 2.0 is not out yet. Only early alpha versions, you don’t mention which one you use. I have sucessfully run the examples under alpha 1 and alpha 4 (the latest at this time). In general, the alpha’s have proven to be fairly unstable (after all they are alpha and are spread with this fair warning). Wait until the release of the actual stable release of 2.0 before you rely on it. For now, use 1.5.1.

    Having said that, I am puzzled by the blend error, since blending is not used in any of the above examples. For general support or issues with the 2.0 alpha’s please use the Processing forum and the bug tracker at google code.

  • Pavlov1234

    nice! i was wondering: which camera you used for the webcam example? the image is pretty, with nice depth of field. was it made by a SLR photo camera?!

  • Amnon P5

    @ Pavlov1234: Yes it was. All of the footage in the movie was shot with a Canon EOS 600D (Rebel T3i).

  • Pavlov1234

    ah cool. but that means you haven’t really used the canon as a webcam, right? you shot all the footage of the markers previously and later used it for the AR magic.

  • Amnon P5

    That’s right. While testing different code I used my webcam in realtime. But for the purpose of the showcase I rewrote all the sketches to work with existing high quality footage.

  • http://www.jumbocortex.com/think mkedave

    bitchin skill to add to the resume.

  • http://twitter.com/ghostpressbed Derek J. Kinsman

    Ah, thanks Amnon. I figured I could get rid of GStreamer. Not sure when I broke it though. I really like GSVideo. Not sure what broke it, so I guess I’ll be debugging the computer before getting any work done. :/

  • Roberto233

    Hi and thanks so much for posting this! I’m very excited to start trying this, but am having the exact same issue on the first example (as ʎʞsɹoƃıןoƃ pıʌɐp posted) and I do indeed have the NyARTToolkit 1.1.6 library installed.  I’m using Processing 2.0a4 (alpha 4).  I was wondering if you think this issue could be caused by something else?  Thanks again.

  • tomh

    Eventually I renamed the folder to nyar4psg The code example 1 on the page has windows formatted directory paths, so I adjusted them to match mine – e.g.String camPara = “/Users/yourusernamehere/Documents/Processing/libraries/nyar4psg/data/camera_para.dat”;// the full path to the .patt pattern filesString patternPath = “/Users/yourusernamehere/Documents/Processing/libraries/nyar4psg/patternMaker/examples/ARToolKit_Patterns”;Then I had to copy the folder called patternMaker into the nyar4psg folder, which isn’t mentioned in the post…

    Quick test of the ARToolkit inside Processing: 
    http://vimeo.com/34605217

  • Amnon P5

    Roberto233: I have updated the blog post to include some more details on the multimarker exception and also updated my first reply to this problem in the comments. Once again, the solution is to change the camPara String to point to the file on YOUR computer. Similarly, the patternPath needs to reflect the location of the files on YOUR computer.

    tomh: You are correct. As mentioned in the comments below and the updated blog post, the paths need to point to the files on YOUR computer.

    In hindsight, seeing the number of questions about it, I guess I should
    have added this more explicitly to the post in the first place. Live and learn! Happy coding everybody! :P

  • http://www.arminunruh.de/ Armin Unruh

    Thanks! Your examples helped me make an application for drawing a simple sound sculpture: 
    http://vimeo.com/34752161

  • Simmonsstummer

    awesome! i managed to work the static one but in the second i get: “the function start() does not exists” referred to the cam.start()
    any suggestion?

  • Mark Hester

    Great Tutorial and a fantastic library.
    Thanks, I’m enjoying playing around with it!
    One quick question – would it be possible to link the AR markers with a 3D model from a 3D CAD package.
    For example if I design something in CAD and save it as an STL or similar 3D file format could I play the file using procesing and link it to an AR marker?

  • Amnon P5

    Yes. You can use for example Toxiclibs’s STLReader to load the STL file into Processing. Once it is in Processing, you can use it. This should be fairly easy.

  • Juan Patino

    Great tutorial! Nice accident happened when the webcam thought a frame in the background was a pattern! Thanks!!!

  • crg

    what is that “import java.io*”? i successfully installed the 2 libraries you mentioned, also got the generator but when ir run the code i get this : 
    java.lang.NullPointerException at processing.app.Library.getClassPath(Library.java:298) at processing.mode.java.JavaBuild.preprocess(JavaBuild.java:390) at processing.mode.java.JavaBuild.preprocess(JavaBuild.java:197) at processing.mode.java.JavaBuild.build(JavaBuild.java:156) at processing.mode.java.JavaBuild.build(JavaBuild.java:135) at processing.mode.java.JavaMode.handleRun(JavaMode.java:176) at processing.mode.java.JavaEditor$20.run(JavaEditor.java:481) at java.lang.Thread.run(Thread.java:680)i would preciate it very much if you could explain me what’s happening

  • Amnon P5

    java.io is part of the default java and contains some classes used in the loadPatternFilenames() function such as File and FilenameFilter. Some Processing versions already automatically include these, so an explicit import statement is not required, but for some it is required. So that’s why I added it explicitly. Your error points to a deeper issue that I suspect is either caused by the Processing version you use (very old version or very new alpha?) or the java installation on your computer. Try downloading Processing 1.5.1 STANDARD version (which includes java) for your operating system. If this does not work, please pinpoint the problem (remove all non-relevant code) and post to the Processing forum with more details (which line gives the error, which OS, which Processing version). Good luck.

  • crg

    my processing version is the 1.5.1 and java is correctly updated as i double checked it. don’t know why or how, but reinstalled the IDE and first example now works perfectly.
    thanks a lot for your time

  • Kristian_tapia

    Hi everyone i tried to run the second program using processing and I get a strange error Unsatisfied LinkError: Unable to load library ‘ gstreamer-0.10′: dlopen (lib streamer-0.10.dylib,9): image not found any ideas? I’ve been searching for a solution but nothing….hope you can help I’m using Mac OSX 10.7.2 

  • ggh

    This was a lot of fun. In the end, everything worked perfect for me. I ran into a few issues, but nothing a Google search couldn’t solve. I would love to keep experimenting with this and I was wondering if anyone knew of some more resources I could use to learn about AR in processing. I guess my ultimate goal would be to combine AR in processing and my arduino.

  • lukas valiauga

    how did you get nyARToolkit installed into libraries. I got 1.5.1processing, and 1.1.6nyartoolkit. When I add it to contributed libreries folder, after restarting program it just does not show up. Please help me !

  • Stkr

    On example 1…. I keep getting a “NullPointerException” error with the following,

    “The file “input.jpg” is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.
    Exception in thread “Animation Thread” java.lang.NullPointerException
        at sketch_jan26a.setup(sketch_jan26a.java:52)
        at processing.core.PApplet.handleDraw(Unknown Source)
        at processing.core.PApplet.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:662)”

    I’m new to this whole coding/AR thing, what files/code do I need to fix/add/change to get the example to work for me? Help would be appreciated, thanks!

  • Amnon P5

    Hi Kristian. Since this is a specific issue on your system in relation to GSVideo, I suggest you use the Processing forum to look for a solution.

  • Amnon P5

    Hi ggh. You could check out the early release of Making Things See by Greg Borenstein.

  • Amnon P5

    Hi lukas. Change the directory name from “nyar4psg-1.1.6″ into “nyar4psg” (to match the jar file name in the library folder). Then it should get picked up by Processing.

  • Amnon P5

    Hi stkr. As the text in example 1 explains you need to place the image that I provided (the one above with only the black and white markers) in the sketch’s data subdirectory. The file needs to be named “input.jpg”.

  • Stkr

    Hello Amnon, for some reason it still gives me the same error message, I renamed the picture posted above (http://www.creativeapplications.net/wp-content/uploads/2011/12/Amnon-Owed-ARtut-01.jpg) (this is the image needed, right?) to ‘input.jpg’ like you said and put it in the data folder. 

    (path was libraries->documents->processing->sketch_jan26a->data  (and the input.jpg file is in the data folder) that was the correct location right?

  • maria

    Thanks for this!! It’s great. However, I can’t make it work on my computer. I don’t really understand how the webcam works. I have a mac, I don’t know if the code changes at all. My sketch window is grey and I get the following message:

    This version of Processing only supports libraries and JAR files compiled for Java 1.5.A library used by this sketch was compiled for Java 1.6 or later, and needs to be recompiled to be compatible with Java 1.5.Exception in thread “Animation Thread” java.lang.UnsupportedClassVersionError: Bad version number in .class file at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:676) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124) at java.net.URLClassLoader.defineClass(URLClassLoader.java:260) at java.net.URLClassLoader.access$100(URLClassLoader.java:56) at java.net.URLClassLoader$1.run(URLClassLoader.java:195) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:317) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:280) at java.lang.ClassLoader.loadClass(ClassLoader.java:252) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:375) at nyart.setup(nyart.java:59) at processing.core.PApplet.handleDraw(PApplet.java:1608) at processing.core.PApplet.run(PApplet.java:1530) at java.lang.Thread.run(Thread.java:655)