//
you're reading...
Computer vision, leJOS Features

Webcam support

One of the really cool things about the EV3 is that it runs on top of a fairly standard Linux system. This means that it is reasonably easy to hack the system to do new things and support new devices. One of our users Gabriel Ferrer has been doing just that. In this thread and in a series of web posts on his blog Gabe describes the process of enabling video support in the Linux kernel and providing access to it from a leJOS program. Gabe was happy to contribute this great work to leJOS and I’ve now incorporated the code into leJOS and the leJOS sd card (at the moment this support is only available in the git master version). Using this updated version I was able to use the EV3 and my Logitech pro 9000 to take a “selfie”
ev3selfieThe above image is actually a screen grab of the EV3 LCD display using the leJOS EV3Control program. The program running on the EV3 basically grabs frames from the webcam and converts them into a mono image for display on the EV3. The code to do this is actually pretty simple (and is based on a test program written by Gabe).

import java.io.IOException;
import lejos.hardware.video.Video;
import lejos.hardware.video.YUYVImage;
import lejos.hardware.BrickFinder;
import lejos.hardware.Button;
import lejos.hardware.lcd.GraphicsLCD;

 public class CameraDemo {
     public static void main(String[] args) {
         try {
             Video wc = BrickFinder.getDefault().getVideo();
             wc.open(160,120);
             byte[] frame = wc.createFrame();
             YUYVImage img = new YUYVImage(frame, wc.getWidth(), wc.getHeight());
             GraphicsLCD g = BrickFinder.getDefault().getGraphicsLCD();
             int threshold = 128;
             boolean auto = true;
             while (!Button.ESCAPE.isDown()) {
                 wc.grabFrame(frame);
                 if (auto)
                     threshold = img.getMeanY();
                 img.display(g, 0, 0, threshold);
                 if (Button.UP.isDown()) {
                     threshold++;
                     if (threshold > 255)
                         threshold = 255;
                     auto = false;
                 }
                 if (Button.DOWN.isDown()) {
                     threshold--;
                     if (threshold < 0)
                         threshold = 0;
                     auto = false;
                 }
                 if (Button.ENTER.isDown()) {
                     auto = true;
                 }
             }
             wc.close();
             g.clear();
         } catch (IOException ioe) {
             ioe.printStackTrace();
             System.out.println("Driver exception: " + ioe.getMessage());
         }
     }
 }  

The conversion to monochrome is pretty simplistic simply turning any pixel that has a luminance less than a defined threshold to be black and other values to be white. The program uses the UP/DOWN keys to change the threshold settings. ENTER can be used to enable an auto mode which simply uses the mean luminance as the threshold.

You may be wondering why we chose this rather odd YUYV format (also known as YUV422 and YUY2) rather than simple RGB pixel values. The above program shows how YUYV can be easier to work with. This format separates the colour information from the luminance which in this case makes it very easy to perform the threshold operation. Often it is useful to perform operations on the luminance of an image (edge detection etc.) and so we use this format. We may extend the video classes in the future to support other formats but for now YUYV is what you get! If you want to know more including how to convert to RGB take a look at this Wikipedia entry.

Advertisements

Discussion

2 thoughts on “Webcam support

  1. Hi i’m a newbie
    my java is giving me some errors on the line 45, 33 and 27.
    Do you know how to fix this?

    Posted by Johnh | 2015/02/18, 17:42
    • Hi,
      Unfortunately the wordpress editor sometimes mangles code posted on this blog. I’ve fixed it now, but it may happen again. If you have further questions please post them on the leJOS forum, it is much easier to post code etc. there.

      Posted by gloomyandy | 2015/02/18, 18:03

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

About leJOS News

leJOS News keeps you up-to-date with leJOS. It features latest news, explains cool features, shows advanced techniques and highlights amazing projects. Be sure to subscribe to leJOS News and never miss an article again. Best of all, subscription is free!
Follow leJOS News on WordPress.com
%d bloggers like this: