Main Content

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.

Introduction

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

Prerequisites

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 androidcolorreplacement Simulink 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.png');
Warning: PNG library warning:
iCCP: cHRM chunk does not match sRGB 

4. On the Modeling tab of the toolstrip, select Model Settings.

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

6. On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Monitor & Tune.

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

8. 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.

9. 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.

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

Task 2 - Run Simulink Model with Controls from 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 androidColorReplacementOnDevice Simulink model.

2. On the Modeling tab of the toolstrip, select Model Settings.

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

4. On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Build, Deploy & Start.

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.