Detect Boundaries of Objects Within Video Using MATLAB Function Block on Android Device
This example shows you how to identify the boundaries of objects in a live video stream on Android™ device by using a MATLAB® Function block with the Simulink® Support Package for Android Devices. The process of identifying boundaries of objects is known as edge detection. This example implements the Sobel edge detection algorithm to identify the boundaries of the objects.
In this example, you will learn how to:
Acquire live video stream from the camera on the Android device.
Implement the Sobel edge detection algorithm on the acquired video using a MATLAB Function block.
Display the edge-emphasized objects on the Android device.
To run the model, download and install Simulink® Support Package for Android™ Devices.
Android device with camera
Step 1: Connect Android Device for Edge Detection
Before you start this example, we recommend you complete the Getting Started with Android Devices example.
1. Connect the micro-USB end of the USB cable to the Android device and the regular USB end of the USB cable to the computer.
2. Using the Hardware Setup screen, configure the Android network.
Step 2: Open and Configure Edge Detection Simulink Model
Open the Edge Detection model.
The model mainly consists of these blocks:
Camera: This block captures live images from the camera of the Android device. The block outputs red, green, and blue components of the image pixel as a matrix of uint8 values.
Slider: This block adds a slider on the user interface of the generated application. You can drag the slider to the left to reduce the threshold and drag the slider to the right to increase the threshold value.
MATLAB Function: This block is a custom block that implements the Sobel edge detection algorithm. The algorithm finds the edges in the image by approximating the gradient magnitude of the image. When you double-click the block, you can see that the algorithm is comprised of two two-dimensional filters, one to calculate the gradient in the column direction and the other to calculate the gradient in the row direction. Both filters use a 3-by-3 kernel.
Video Display: This block displays the edge emphasized objects on the generated application.
Step 3: Change Model Configuration Parameters Settings
1. On the Modeling tab of the toolstrip, select Model Settings.
2. In the Configuration Parameters dialog box, select Hardware Implementation.
3. Set the Hardware board parameter to
Android Device. This selection automatically populates the parameters in the Hardware board settings with the default values for the device.
4. From the Groups list under Target hardware resources, select Device options.
5. From the Device list, select your Android device. If your device is not listed, click Refresh.
Note: If your device is not listed even after clicking Refresh, ensure that you have enabled the USB debugging option on your device. To enable USB debugging, enter
androidhwsetup in the MATLAB® Command Window and follow the onscreen instructions.
5. Click Apply. Click OK to save your changes.
Step 4: Deploy Edge Detection Simulink Model on Android Device
On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Build, Deploy & Start. The lower-left corner of the model window displays status while the support package generates the code. After successfully generating the code, the support package loads and runs the code on the device. The generated application starts acquiring live video from the camera, runs the Sobel algorithm on the acquired images, and then displays the edge-emphasized objects on the device. Change the threshold value by dragging the Threshold slider as per the requirement.
Other Things to Try
Replace the edge detection algorithm with any other algorithm that has better performance as per your requirements.