main content

convert matlab vision algorithm to hardware-凯发k8网页登录

this example shows how to create a hardware-targeted design in simulink® that implements the same behavior as a matlab® reference design.

workflow

image processing toolbox™ and computer vision toolbox™ functions operate on framed, floating-point and integer data and provide excellent behavioral references. hardware designs must use streaming boolean or fixed-point data.

this example shows how to perform a framed image processing operation in matlab, and then implement the same operation in a simulink model using streaming data. the simulink model converts the input video to a pixel stream for hardware-friendly design. the same data is applied to both the hardware algorithm in simulink and the behavioral algorithm in matlab. the simulink model converts the output pixel stream to frames and exports those frames to matlab for comparison against the behavioral results.

the matlab portion of this example loads the input video, runs the behavioral code, runs the simulink model to import video frames and export modified video frames, and compares the matlab behavioral results with the simulink output frames.

video source

create a video reader object to import a video file into the matlab workspace. the video source file is 240p format. create a video player object to display the input frame, simulink filtered frame, and matlab reference frame.

videoin = videoreader('rhinos.avi');
numfrm = 10;
% active frame dimensions
actpixelsperline = 320;
actlines = 240;
% dimensions including blanking
totalpixelsperline = 402;
totallines = 324;
% viewer for results
viewer = vision.deployablevideoplayer(...
        'size','custom',...
        'customsize',[3*actpixelsperline actlines]);

edge detection and overlay

detect edges in the video frames, and then overlay those edges onto the original frame. the overlay computation uses an alpha value to mix the two pixel values. the simulink model also uses the edgethreshold and alpha parameters specified here.

the matlab edge function interprets the threshold as a double-precision value from 0 to 1. therefore, express the threshold as a fraction of the range of the uint8 data type, from 0 to 255. the pixel values returned by the edge function are logical data type. to convert these pixel values to uint8 type for overlay, multiply by 255. this scaling operation converts logical ones to 255 and logical zeros stay 0.

edgethreshold = 8;
alpha = 0.75;
frmfull = uint8(zeros(actlines,actpixelsperline,numfrm));
frmref = frmfull;
for f = 1:numfrm
    frmfull(:,:,f) = rgb2gray(readframe(videoin));
    edges = edge(frmfull(:,:,f),'sobel',edgethreshold/255,'nothinning');
    edges8 = 255*uint8(edges)*(1-alpha);
    frmref(:,:,f) = alpha*frmfull(:,:,f)   edges8;
    viewer([edges edges8 frmref(:,:,f)]);
end

set up for simulink simulation

the simulink model loads the input video into the model using a video source block. configure the sample time of the model using the totpixperframe variable. this value includes the inactive pixel regions around the 240-by-320 frames. the video source sample time is 1 time step per frame, and the rate in the streaming pixel sections of the model is 1/ totpixperframe . set the length of the simulation with the simtime variable.

totpixperframe = totalpixelsperline*totallines;
simtime = (numfrm 1)*totpixperframe;
modelname = 'verifysldesignagainstmlreference';
open_system(modelname);
set_param(modelname,'sampletimecolors','on');
set_param(modelname,'simulationcommand','update');
set_param(modelname,'open','on');

hardware-targeted algorithm

the hdl algorithm subsystem is designed to support hdl code generation.

the subsystem uses the edge detector block to find edges. the output of the block is a stream of boolean pixel values. the model scales these values to uint8 data type values for overlay.

the block returns the pixel stream of detected edges after several lines of latency, due to internal line buffers and filter logic. before performing overlay, the model must delay the input stream to match the edge stream. the pixel stream aligner block performs this alignment using the control signals of the output edge stream as a reference. this block stores the input stream in a fifo until the detected edges are available.

the image overlay subsystem scales both streams by the alpha ratio and adds them together. with hardware implementation in mind, the image overlay subsystem includes pipeline stages around each multiplier and after the adder.

for more details of this edge detector design, see the example.

open_system([modelname '/hdl algorithm']);

run simulink model

run the simulink model to return ten frames overlaid with the detected edges.

sim('verifysldesignagainstmlreference');

compare simulink results with matlab results

compare each video frame returned from simulink with the result returned by the matlab behavioral code. the images look very similar but have small pixel value differences due to overlay mixing. the matlab overlay mixing is done using floating-point values, and the simulink overlay mixing is done using fixed-point values. this comparison counts pixels in each frame whose values differ by more than 2 and calculates the peak-signal-to-noise ratio (psnr) between the frames. to view the detailed differences at each frame, uncomment the last two lines in the loop.

 for f = 1:numfrm
    frmresult = frmout.signals.values(:,:,f);
    viewer([frmfull(:,:,f) frmresult frmref(:,:,f)]);
    diff = frmref(:,:,f) - frmresult;
    errcnt = sum(diff(:) > 2);
    noisecheck = psnr(frmref(:,:,f),frmresult);
    fprintf(['\nframe #%d has %d pixels that differ from behavioral result (by more than 2). psnr = %2.2f\n'],f,errcnt,noisecheck);
    �r3(diff);
    %viewer([frmresult frmref(:,:,f) diff]);
 end
frame #1 has 2 pixels that differ from behavioral result (by more than 2). psnr = 48.33
frame #2 has 1 pixels that differ from behavioral result (by more than 2). psnr = 48.72
frame #3 has 1 pixels that differ from behavioral result (by more than 2). psnr = 48.80
frame #4 has 2 pixels that differ from behavioral result (by more than 2). psnr = 48.66
frame #5 has 2 pixels that differ from behavioral result (by more than 2). psnr = 48.70
frame #6 has 4 pixels that differ from behavioral result (by more than 2). psnr = 48.27
frame #7 has 2 pixels that differ from behavioral result (by more than 2). psnr = 48.88
frame #8 has 3 pixels that differ from behavioral result (by more than 2). psnr = 48.58
frame #9 has 3 pixels that differ from behavioral result (by more than 2). psnr = 48.55
frame #10 has 3 pixels that differ from behavioral result (by more than 2). psnr = 48.53

generate hdl code and verify its behavior

once your design is working in simulation, you can use hdl coder™ to generate hdl code and a test bench for the hdl algorithm subsystem.

makehdl([modelname '/hdl algorithm'])   % generate hdl code
makehdltb([modelname '/hdl algorithm']) % generate hdl test bench

see also

|

related topics

网站地图