# Documentation

### This is machine translation

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

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

## Edge Detection on Images

This example shows how to generate a standalone C library from MATLAB code that implements a simple Sobel filter that performs edge detection on images. The example also shows how to generate and test a MEX function in MATLAB prior to generating C code to verify that the MATLAB code is suitable for code generation.

### Prerequisites

There are no prerequisites for this example.

### Create a New Folder and Copy Relevant Files

The following code will create a folder in your current working folder (pwd). The new folder will only contain the files that are relevant for this example. If you do not want to affect the current folder (or if you cannot generate files in this folder), you should change your working folder.

### Run Command: Create a New Folder and Copy Relevant Files

```coderdemo_setup('coderdemo_edge_detection'); ```

### About the 'sobel' Function

The sobel.m function takes an image (represented as a double matrix) and a threshold value and returns an image with the edges detected (based on the threshold value).

```type sobel ```
```% edgeImage = sobel(originalImage, threshold) % Sobel edge detection. Given a normalized image (with double values) % return an image where the edges are detected w.r.t. threshold value. function edgeImage = sobel(originalImage, threshold) %#codegen assert(all(size(originalImage) <= [1024 1024])); assert(isa(originalImage, 'double')); assert(isa(threshold, 'double')); k = [1 2 1; 0 0 0; -1 -2 -1]; H = conv2(double(originalImage),k, 'same'); V = conv2(double(originalImage),k','same'); E = sqrt(H.*H + V.*V); edgeImage = uint8((E > threshold) * 255); ```

### Generate the MEX Function

Generate a MEX function using the 'codegen' command.

```codegen sobel ```

Before generating C code, you should first test the MEX function in MATLAB to ensure that it is functionally equivalent to the original MATLAB code and that no run-time errors occur. By default, 'codegen' generates a MEX function named 'sobel_mex' in the current folder. This allows you to test the MATLAB code and MEX function and compare the results.

### Read in the Original Image

Use the standard 'imread' command.

```im = imread('hello.jpg'); image(im); ```

### Convert Image to a Grayscale Version

Convert the color image (shown above) to an equivalent grayscale image with normalized values (0.0 for black, 1.0 for white).

```gray = (0.2989 * double(im(:,:,1)) + 0.5870 * double(im(:,:,2)) + 0.1140 * double(im(:,:,3)))/255; ```

### Run the MEX Function (The Sobel Filter)

Pass the normalized image and a threshold value.

```edgeIm = sobel_mex(gray, 0.7); ```

### Display the Result

```im3 = repmat(edgeIm, [1 1 3]); image(im3); ```

### Generate Standalone C Code

```codegen -config coder.config('lib') sobel ```

Using 'codegen' with the '-config coder.config('lib')' option produces a standalone C library. By default, the code generated for the library is in the folder codegen/lib/sobel/

### Inspect the Generated Function

```type codegen/lib/sobel/sobel.c ```
```/* * File: sobel.c * * MATLAB Coder version : 3.3 * C/C++ source code generated on : 14-Feb-2017 18:47:10 */ /* Include Files */ #include "rt_nonfinite.h" #include "sobel.h" #include "sobel_emxutil.h" #include "sqrt.h" #include "conv2.h" /* Function Definitions */ /* * Arguments : const emxArray_real_T *originalImage * double threshold * emxArray_uint8_T *edgeImage * Return Type : void */ void sobel(const emxArray_real_T *originalImage, double threshold, emxArray_uint8_T *edgeImage) { emxArray_real_T *H; emxArray_real_T *V; int b_H; int c_H; emxInit_real_T(&H, 2); emxInit_real_T(&V, 2); /* edgeImage = sobel(originalImage, threshold) */ /* Sobel edge detection. Given a normalized image (with double values) */ /* return an image where the edges are detected w.r.t. threshold value. */ conv2(originalImage, H); b_conv2(originalImage, V); b_H = H->size[0] * H->size[1]; emxEnsureCapacity((emxArray__common *)H, b_H, sizeof(double)); b_H = H->size[0]; c_H = H->size[1]; c_H *= b_H; for (b_H = 0; b_H < c_H; b_H++) { H->data[b_H] = H->data[b_H] * H->data[b_H] + V->data[b_H] * V->data[b_H]; } emxFree_real_T(&V); b_sqrt(H); b_H = edgeImage->size[0] * edgeImage->size[1]; edgeImage->size[0] = H->size[0]; edgeImage->size[1] = H->size[1]; emxEnsureCapacity((emxArray__common *)edgeImage, b_H, sizeof(unsigned char)); c_H = H->size[0] * H->size[1]; for (b_H = 0; b_H < c_H; b_H++) { edgeImage->data[b_H] = (unsigned char)((H->data[b_H] > threshold) * 255U); } emxFree_real_T(&H); } /* * File trailer for sobel.c * * [EOF] */ ```

### Cleanup

Remove files and return to original folder

### Run Command: Cleanup

```cleanup ```
Was this topic helpful?

Get trial now