Main Content

Generate HDL Code from Frame-Based Model by Using Neighborhood Processing with States

Since R2024a

You can represent an algorithm that converts Bayer pattern encoding to a true-color, RGB image by using a frame-based model and neighborhood processing methods. In this example, you use the HDL Coder™ frame-to-sample conversion optimization to generate synthesizable pixel-based HDL code from a frame-based model.

This example uses neighborhood processing operations that are needed for the demosaic algorithm. The algorithm contains persistent variables, that model states in a MATLAB® design, to track whether a pixel location is odd or even. The model uses a MATLAB Function block with the neighborhood processing function hdl.npufun.

Examine MATLAB Function Block Algorithm

In the hdlFrame_Demosaic model, there is a MATLAB Function block inside the device under test (DUT) that contains the neighborhood processing operations needed for the demosaic algorithm. To generate synthesizable HDL code from the frame-based model, HDL Coder uses hdl.npufun to create a streaming sample-based neighborhood processing algorithm. You can use this function in a frame-based model to contain the entire image processing algorithm in a single MATLAB Function block.

Open the MATLAB Function block in the hdlFrame_Demosaic/DUT subsystem to see the demosaic algorithm.

load_system("hdlFrame_Demosaic");
open_system("hdlFrame_Demosaic/DUT/MATLAB Function");
imshow("hdlFrame_demosaic.tif");

Examine the Kernel Function for Neighborhood Processing

This code shows a section of kernel function that this model uses for neighborhood processing. This code shows how the function uses persistent variables inside the kernel function demosaic_bilinear to track the row and column location in the input image. The function uses the row and column values to decide the computations to get the R, G, and B output values.

function [R,G,B] = demosaic_bilinear(height, width, im)
persistent row col
if isempty(row)
    row = uint16(1);
    col = uint16(1);
end
isRowOdd = bitget(row, 1);
isColOdd = bitget(col, 1);

Run the Model

The input data hdlFrame_demosaic.tif is an image encoded by the Bayer pattern encoded with a size of size 500-by-500 pixels. Simulate the model to see the decoded RGB color data an as output color image.

sim("hdlFrame_Demosaic");

Generate HDL Code

Generate synthesizable HDL code by using the frame-to-sample conversion. On the Inport block of the DUT, set the HDL block property ConvertToSamples to convert the input signals from frame-based to sample-based inputs.

hdlset_param('hdlFrame_Demosaic/DUT/I','ConvertToSamples','on');

If the input image source emits multiple pixels per clock cycle, use the SamplesPerCycle parameter to generate HDL code which matches the image source. For example, to handle 4 pixels per clock cycle, set the SamplesPerCycle parameter to 4.

hdlset_param('hdlFrame_Demosaic','SamplesPerCycle',4);

For the MATLAB Function block that contains the optical flow algorithm, set the HDL block property Architecture to MATLAB Datapath. Enable the frame-to-sample conversion optimization and generate HDL code using the makehdl command. For more information on the frame-to-sample conversion optimization, see HDL Code Generation from Frame-Based Algorithms.

hdlset_param('hdlFrame_Demosaic/DUT/MATLAB Function','Architecture','MATLAB Datapath');
hdlset_param('hdlFrame_Demosaic','FrameToSampleConversion','on');
makehdl('hdlFrame_Demosaic/DUT');

The frame-to-sample conversion separates the frame-based inputs into sample, valid, and ready signals for a sample-based hardware-targeted interface.

See Also

Functions

Related Examples

More About