MATLAB Examples

Image Inversion

This example shows how to use the V4L2 Video Capture and the SDL Video Display blocks from the BeagleBone Black block library to implement an image inversion algorithm with a Simulink® model, and to run the model on BeagleBone Black hardware.

Contents

Introduction

Black and white image inversion refers to an image processing technique where light areas are mapped to dark, and dark areas are mapped to light. In other words, after image inversion black becomes white and white becomes black. An inverted black and white image can be thought of as a digital negative of the original image.

In this example, the inversion algorithm will be applied to the red (R), green (G) and blue (B) components of an image captured from a USB camera creating a color digital negative effect. The R, G and B components of the image are represented as uint8 values. That is, the range of the values that any of the color components can take is from 0 to 255. Assuming that the intensity value at position $(x, y)$ of the image is $I(x,y)$, the inversion mapping is defined by $I_{inverted}(x,y) = 255-I(x,y)$.

Prerequisites

We recommend completing Getting Started with Embedded Coder Support Package for BeagleBone Black Hardware example.

Required Hardware

To run this example you will need the following hardware:

  • BeagleBone Black hardware
  • A compatible USB camera

(Optional) To display the output video on an external monitor you will need the following:

  • HDMI television, or a monitor with DVI or HDMI input, and appropriate cables.
  • USB mouse and keyboard

Task 1 - Configure and Run the Image Inversion Model in External Mode

In this task, you will connect a USB camera to your BeagleBone Black hardware, and run the image inversion model on BeagleBone Black hardware in External mode. When you run the model in External mode, the image is captured from the USB camera connected to the BeagleBone Black hardware and the results of the image inversion is sent back to the host computer to be displayed by the on SDL Video Display block.

1. Connect a compatible USB camera to the USB host port of your BeagleBone Black hardware. Note that some USB cameras draw too much current from the USB port of BeagleBone Black hardware and may not work properly. Use a powered USB hub in such cases.

2. Open the Image Inversion model. In the Simulink model, click on the V4L2 Video Capture block and note the 'Device name' parameter. Execute the following commands on the MATLAB command window to verify the 'Device name' parameter:

bbone = beaglebone_black;
output = system(bbone, 'ls -al /dev/video*')

Typical output will be:

msg =
crw-rw---T+ 1 root video 81, 0 Dec 16 14:43 /dev/video0

Make sure that the video device file name, '/dev/video0' in the example above, matches to the 'Device name' parameter specified on the V4L2 Video Capture block mask. If you do not see a video device file, your USB camera is not recognized properly. Try rebooting your BeagleBone Black with the camera attached:

system(bbone, 'sudo shutdown -r now')

3. In the model, click the Run button on the toolbar to run the model on BeagleBone Black hardware.

4. Observe the display of inverted images on the host computer with the SDL Video Display block.

5. While the model is running, double click on the Constant block and change the Constant value from 255 to 120, for example. Observe the resulting image effect.

6. Press the Stop button on the model to stop model execution.

Task 2 - (Optional) Configure and Run the Image Inversion Model as a Standalone Application

In this task, you will connect a monitor to BeagleBone Black hardware. The video captured from the USB camera will be inverted by the model running on BeagleBone Black hardware and displayed on the monitor connected to BeagleBone Black hardware.

1. Using appropriate cables, connect a monitor or television to the BeagleBone Black hardware. The monitor, once connected, will display the Linux desktop running on BeagleBone Black hardware.

2. Connect a USB mouse and a keyboard to the BeagleBone Black hardware's USB ports. Once connected, login to Linux desktop as you would to any Linux® computer.

3. Open the Image Inversion model. In the model, click the Deploy To Hardware button on the toolbar to run the model on the BeagleBone Black hardware. On the monitor connected to BeagleBone Black hardware, you will see a display of inverted images.

4. Stop the model running on the BeagleBone Black hardware by executing the following on the MATLAB® command line:

bbone = beaglebone_black;
stopModel(bbone, 'beaglebone_inversion');

Other Things to Try

  • Change the model to use a different constant value for each color component. Then, independently change the value of each of the constants to get a wider range of imaging effects.

Summary

This example showed a Simulink model implementing an image inversion algorithm. V4L2 Video Capture and the SDL Video Display blocks from the BeagleBone Black library were used to capture and display video data. The example showed how the model may be run in External mode, and as a standalone application on BeagleBone Black hardware.