This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Color Replacement

This example shows you how to use sliders and buttons from Simulink Support Package for Android™ Devices to develop an interactive system for color replacement application


A color detection algorithm identifies pixels in an image that match a specified color or color range. The detected pixels in the original image are replaced by the pixels from a different image. This process is known as color replacement or Chroma Key.

This color replacement model internally uses color detection logic same as the Color Detection example

This model is configured with

  • Sliders and buttons for changing the parameters and controlling the detection logic run-time.

  • Provision for inputting a different image for replacement of pixel values in the detected region.

  • Simple low pass filter for smoothing sharp edges in the output.

In this example, you will be introduced to two practices to interactively control the algorithm at run-time:

  • Controlling from the Simulink model on host machine

  • Controlling from the deployed application on the device screen


Required Hardware

  • Android device, configured using androidhwsetup

  • USB cable to connect the device to host computer

Task 1 - Run Model with Controls from Simulink

In this task, you will configure and run the Color Replacement model on the device controlling from Simulink on the host machine by communicating with device using external mode.

1. Open the Color Replacement in External Mode model.

2. In the model double click on the camera and find resolution. Choose an image with this resolution. This image will replace the detected pixels.

3. Load the template image into matrix RGB_background by executing the following command in the MATLAB command window:

RGB_background = imread('image_640_480.jpg');

4. In your Simulink model, click Simulation > Model Configuration Parameters to open Configuration Parameters dialog.

5. Select the Hardware Implementation pane and set the Hardware board to Android Device.

6. In the model, make sure that the Simulation mode on the toolbar is set to External.

7. In the model, click the Run button on the toolbar to run the model on the device.

8. The camera images with color replacement appear on your device screen similar to the figure shown below.

9. For tuning the reference color for detection in the images, change Reference_R, Reference_G, Reference_B slider values to RGB component values of reference color respectively.

10. Modify Threshold_Hue slider value to update hue range used for detection. Smaller value narrows range detecting single color and larger value expands range detecting multiple colors with similar hue.

11. To disable color replacement logic set Enable_detection button to off.

Task 2 - Run the model with Controls on Device

In this task, you will configure color replacement model to deploy the application onto the device and run independently. You will use slider and button blocks from Simulink Support Package for Android® Devices to control the application on the device during run-time. This model uses same subsystem as the previous task.

1. Open the Color Replacement on Device model.

2. In your Simulink model, click Simulation > Model Configuration Parameters to open Configuration Parameters dialog.

3. Select the Hardware Implementation pane and set the Hardware board to Android Device.

4. In your Simulink model, click the Deploy to Hardware button on the toolbar.

5. Once the model deploys, you can observe the camera feed with replacement on the left side of your device screen. All the slider and button controls appear on the right side.

6. Modify the slider values or button on the device to see changes in the output images on device screen.

Other Things to Try

  • Inspect the model and add sliders for controlling other parameters Saturation_threshold, Value_threshold. In the subsystem, replace Constant blocks for these parameters with slider blocks and run the model.


This example demonstrated a color replacement model and showed you how to use slider and button blocks from Simulink to configure an interactive system to control algorithm run-time.In this example you learned:

  • How to use the external mode for controlling algorithm from host machine, while application is running is on device.

  • How to develop an independent application with slider and button controls on the device.