Edge Detection on Images

Open Script

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.

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


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');

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]);

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.2
* C/C++ source code generated on : 23-Jul-2016 04:36:49

/* 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, (int)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];

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, (int)sizeof(unsigned
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);


* File trailer for sobel.c
* [EOF]


Remove files and return to original folder
Run Command: Cleanup