Documentation Center

  • Trial Software
  • Product Updates

Sobel Edge Detection

This example shows how to generate HDL code from a MATLAB® design implementing the Sobel edge detection algorithm.

Introduction

design_name = 'mlhdlc_sobelfilter.m';
testbench_name = 'mlhdlc_sobelfilter_tb.m';

Let us take a look at the MATLAB design

type(design_name);
function [x_out y_out data_out] = ...
            mlhdlc_sobelfilter(x_in, y_in, data_in)
        
persistent lineBuffer1 lineBuffer2 k 

WIDTH = 752;
HEIGHT = 480;
CMAX = 2000;
Kx = [ 1  0 -1; ...
       2  0 -2; ...
       1  0 -1];
Ky = [ 1  2  1; ...
       0  0  0; ...
      -1 -2 -1];

if isempty(k)
    k = zeros(3);
end
if isempty(lineBuffer1)
    lineBuffer1 = zeros(1,WIDTH);
    lineBuffer2 = zeros(1,WIDTH);
end


xTemp = x_in-1;
yTemp = y_in-1;
if xTemp < 0
    xOutTemp = CMAX;
else
    xOutTemp = xTemp;
end
if yTemp < 0
    yOutTemp = CMAX;
else
    yOutTemp = yTemp;
end
if x_in >= 0 && x_in < WIDTH
    dataValid = 1;
else
    dataValid = 0;
end
if dataValid == 1
    lbIndex = x_in+1;
else
    lbIndex = 1;
end
l1 = lineBuffer1(lbIndex);
l2 = lineBuffer2(lbIndex);
if dataValid == 1
    lb1WriteValue = l2;
    lb2WriteValue = data_in;
    l = [l1 l2 data_in]';
else
    lb1WriteValue = l1;
    lb2WriteValue = l2;
    l = zeros(3,1);
end
lineBuffer1(lbIndex) = lb1WriteValue;
lineBuffer2(lbIndex) = lb2WriteValue;
k = [k(:,2:3) l];


if yOutTemp == 0
    k(1,:) = k(2,:);
elseif yOutTemp == HEIGHT-1
    k(3,:) = k(2,:);
end
if xOutTemp == 0
    k(:,1) = k(:,2);
elseif xOutTemp == WIDTH-1
    k(:,3) = k(:,2);
end  
%Gx = conv2(k,Kx,'valid');
%Gy = conv2(k,Ky,'valid');
Gx = 0;
Gy = 0;
for yi = 1:3,
    for xi = 1:3,
        Gx = Gx+k(yi,xi)*Kx(yi,xi);
        Gy = Gy+k(yi,xi)*Ky(yi,xi);
    end
end
G = abs(Gx) + abs(Gy);
Gd = floor(G/4);
Gdm = min(Gd,255);



x_out = xOutTemp;
y_out = yOutTemp;
if yOutTemp < HEIGHT && xOutTemp < WIDTH
    data_out = Gdm;
else
    data_out = 0;
end



type(testbench_name);

FRAMES = 1;
WIDTH = 752;
HEIGHT = 480;
HBLANK = 10;%748;
VBLANK = 10;%120;

vidData = double(imread('mlhdlc_img_yuv.tif'));


for f = 1:FRAMES
    xOut = 1;
    yOut = 1;
    vidOut = zeros(HEIGHT, WIDTH, 3);
    
    for y = 0:HEIGHT+VBLANK-1
        for x = 0:WIDTH+HBLANK-1
            if y >= 0 && y < HEIGHT && x >= 0 && x < WIDTH
                pixData = vidData(y+1,x+1,1);
            else
                pixData = 0;
            end
            

            [xOut yOut dataOut] = ...
                mlhdlc_sobelfilter(x, y, pixData);


            if yOut >= 0 && yOut < HEIGHT && xOut >= 0 && xOut < WIDTH
                vidOut(yOut+1,xOut+1,:) = dataOut;
            end  
        end  
    end
    
    figure(1);
    subplot(1,2,1); 
    imshow(uint8(vidData(:,:,1)));
    subplot(1,2,2);
    imshow(uint8(vidOut));
    drawnow;            
            
end

    

Setup for the Example

Executing the following lines of code copies the necessary example files into a temporary folder

mlhdlc_demo_dir = fullfile(matlabroot, 'toolbox', 'hdlcoder', 'hdlcoderdemos', 'matlabhdlcoderdemos');
mlhdlc_temp_dir = [tempdir 'mlhdlc_sobel'];

% create a temporary folder and copy the MATLAB files
cd(tempdir);
[~, ~, ~] = rmdir(mlhdlc_temp_dir, 's');
mkdir(mlhdlc_temp_dir);
cd(mlhdlc_temp_dir);

% copy the design files to the temporary directory
copyfile(fullfile(mlhdlc_demo_dir, design_name), mlhdlc_temp_dir);
copyfile(fullfile(mlhdlc_demo_dir, testbench_name), mlhdlc_temp_dir);
copyfile(fullfile(mlhdlc_demo_dir, 'mlhdlc_img_stop_sign.gif'), mlhdlc_temp_dir);

Simulate the Design

It is a good practice to simulate the design with the testbench prior to code generation to make sure there are no runtime errors.

mlhdlc_sobelfilter_tb

Create a New HDL Coder™ Project

coder -hdlcoder -new mlhdlc_sobel_prj

Next, add the file 'mlhdlc_sobelfilter.m' to the project as the MATLAB Function and 'mlhdlc_sobelfilter_tb.m' as the MATLAB Test Bench.

You can refer to Getting Started with MATLAB to HDL Workflow tutorial for a more complete tutorial on creating and populating MATLAB HDL Coder projects.

Run Fixed-Point Conversion and HDL Code Generation

Launch HDL Advisor and right click on the 'Code Generation' step and choose the option 'Run to selected task' to run all the steps from the beginning through the HDL code generation.

Examine the generated HDL code by clicking on the hyperlinks in the Code Generation Log window.

Clean up the Generated Files

You can run the following commands to clean up the temporary project folder.

mlhdlc_demo_dir = fullfile(matlabroot, 'toolbox', 'hdlcoder', 'hdlcoderdemos', 'matlabhdlcoderdemos');
mlhdlc_temp_dir = [tempdir 'mlhdlc_sobel'];
clear mex;
cd (mlhdlc_demo_dir);
rmdir(mlhdlc_temp_dir, 's');
Was this topic helpful?