Design Article

Background subtraction, part 2: MATLAB to C using MCS

Seth Benton

8/17/2008 12:00 PM EDT

Part 1 details three background subtraction algorithms implemented in MATLAB. Also see MATLAB to C using MCS: Advanced topics, where we highlight advanced topics using the mixture-of-Gaussians background subtraction method.

Introduction
MATLAB is a highly flexible interpreted language. This makes automatic translation to C a pain. To convert MATLAB to C, a tool must guess data types (MATLAB doesn't require specification of data types), convert MATLAB's vector semantics into loops, deal with polymorphism (which isn't supported in C). The list goes on.

In this article we'll examine how Agility's MATLAB to C tool MCS approaches the problem. First, we'll give an overview the tool and its capabilities. Then, we'll explore the conversion process by converting the background subtraction algorithms we implemented in Part 1 (as MATLAB models), into C. We'll then verify the generated C using mex files (all m-files, generated C files, and the test video clip used—can be downloaded here).

What is MCS exactly?
MCS converts MATLAB to ANSI-C. It can also compile generated C into a standalone executable. If the generated C is to be integrated into a larger model (for instance, integrated with legacy C code), MCS eases this task by doing some of the integration grunt work.

MCS supports most MATLAB language features, including the most common operators, data types, flow control constructs, persistent and global variables, etc… These features should suffice for most use cases. A bigger issue for most developers will be function support. If a function isn't supported, the developer must find and integrate an equivalent C function, or write one from scratch in MATLAB or C. This can be a time-consuming and potentially bug introducing process.

At the time of this writing, MCS supports C code generation for hundreds of functions. Many more functions are supported via callbacks to MATLAB (i.e. the C code calls the MATLAB engine. This means MATLAB has to be running or the MATLAB engine has to be embedded in the application).

MCS focuses on image/video processing, but also supports a wide range of non-image/video related functions, making it suitable for a wide range of DSP applications.

Background subtraction models
Recall that in Part 1, we implemented three background subtraction algorithms in MATLAB (project files here.); a low-complexity method (frame difference), a medium-complexity method (approximate median), and a high-complexity method (mixture of gaussians). For the sake of brevity and clarity, we'll use the low-complexity (frame difference) method to illustrate the conversion process. With understanding of the process for frame difference, the MATLAB code for the other methods should be relatively straightforward to interpret.

MATLAB to C conversion
Step 1: Separate the algorithm from the testbench
In our MATLAB model for frame difference, we combined code implementing the algorithm with testbench code (i.e. code that reads in the test video, checks for errors, visualizes output, etc.) in the same m-file. MCS requires we separate the algorithm from the testbench and put it into a separate function. This makes it easier to define algorithm inputs. It also makes it easier to integrate the algorithm into a larger application. Figure 1 shows the algorithm code we wish to convert.


Figure 1. Algorithm portion of the frame difference algorithm

The algorithm is relatively simple.

  1. Convert the incoming frame 'fr' to greyscale (here we assume a color RGB sensor)
  2. Subtract the current frame from the background model 'bg_bw' (in this case it's just the previous frame)
  3. For each pixel, if the difference between the current frame and backround 'fr_diff (j,k)' is greater than a threshold 'thresh', the pixel is considered part of the foreground.
Figure 2 shows the frame difference method at work on the test video.

Figure 2. Frame difference output




Please sign in to post comment

Navigate to related information

Datasheets.com Parts Search

185 million searchable parts
(please enter a part number or hit search to begin)

Feedback Form