Main Content

Color Detection

This example shows you how to develop a Simulink® model for an image processing application - color detection by using Simulink Support Package for Android® Devices.

Introduction

A color detection algorithm identifies pixels in an image that match a specified color or color range. The color of detected pixels can then be changed to distinguish them from the rest of the image.

This example introduces a workflow to take Simulink model of a color detection algorithm from simulation to deployment onto device:

  • Simulate the color detection model

  • Deploy the color detection model and use the Video Display block to show simulated data on the Android device

  • Modify the color detection model to use real-time images from the Camera Block

Prerequisites

We recommend completing Getting Started with Android Devices example.

Required Hardware

  • Android device, configured using androidhwsetup

  • USB cable to connect the device to host computer

Task 1 - Simulate Model on Host Computer

In this task, you will simulate color detection model and observe results for the images from workspace.

1. Open the androidColorDetectionImage Simulink model.

2. Load and display a sample color image into the MATLAB® workspace by executing the following command in the MATLAB command window:

RGB_image = imread('peppers.png');
imshow(RGB_image)

3. In the model, double-click the Reference Color block to open the block mask.

4. In the block mask, set the Constant value field to the vector [ 58, 68, 117 ] to detect the color purple in the image. The vector value represents the red, green, and blue components of the color for detection (e.g. [R, G, B] ).

5. In the model, click the Run button on the toolbar to run the simulation. When the simulation completes, you can see the resulting image where the originally purple regions are now white.

6. (Optionally) You can modify the value of the Reference Color block and run the simulation again to isolate different color regions from the image.

Task 2 - Run Model on Device

In this task, you will create a new model to use the Video Display block and deploy the model algorithm to the device

1. Open a new Simulink model and copy the contents of the first model into the new model.

2. In the new model, remove the MATLAB Function image_display block.

3. From the Simulink Library Browser, add the Android Video Display block to the model. A preconfigured Simulink model is included for your convenience.

4. Connect your configured Android device to your host computer. If you have not previously configured your device, run androidhwsetup from the MATLAB command prompt to configure your Android device first.

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

6. Select the Hardware Implementation pane and set the Hardware board to the Android Device you are using.

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

8. The image with the masked colors will appear on your device screen similar to the figure shown below.

Task 3 - Run Model on Device with Real-Time Video

In this task, you will update the previous model to use frames captured from your Android device camera allowing for color detection in real-time images.

1. Using the model created in Task 2, remove the Constant blocks labeled R_inp, G_inp, and B_inp.

2. From the Simulink Library Browser, add the Android Camera block to the model.

3. Connect the R, G, and B ports of the Camera block to the R_in, G_in, and B_in ports of the Color Detection block, respectively. A preconfigured Simulink model is included for your convenience.

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

5. The camera images with the masked colors appear on your device screen similar to the figure shown below.

Other Things to Try

  • Change the detection algorithm by modifying the threshold values used in the Color Detection subsystem.