HDL Coder

High Dynamic Range Imaging

This example shows how to generate HDL code from a MATLAB® design that implements a high dynamic range imaging algorithm.


High Dynamic Range Imaging (HDRI or HDR) is a set of methods used in imaging and photography to allow a greater dynamic range between the lightest and darkest areas of an image than current standard digital imaging methods or photographic methods. HDR images can represent more accurately the range of intensity levels found in real scenes, from direct sunlight to faint starlight, and is often captured by way of a plurality of differently exposed pictures of the same subject matter.


design_name = 'mlhdlc_hdr';
testbench_name = 'mlhdlc_hdr_tb';

Let us take a look at the MATLAB design

function [valid_out, x_out, y_out, ...
    HDR1, HDR2, HDR3] = mlhdlc_hdr(YShort1, YShort2, YShort3, ...
    YLong1, YLong2, YLong3, ...
    plot_y_short_in, plot_y_long_in, ... 
    valid_in, x, y)
% This design implements a high dynamic range imaging algorithm.

plot_y_short = plot_y_short_in;
plot_y_long = plot_y_long_in;

%% Apply Lum(Y) channels LUTs
y_short = plot_y_short(uint8(YShort1)+1);
y_long = plot_y_long(uint8(YLong1)+1);

y_HDR = (y_short+y_long);

%% Create HDR Chorm channels
% HDR per color

HDR1 = y_HDR * 2^-8;
HDR2 = (YShort2+YLong2) * 2^-1;
HDR3 = (YShort3+YLong3) * 2^-1;

%% Pass on valid signal and pixel location

valid_out = valid_in;
x_out = x;
y_out = y;


% Clean screen and memory 
close all
clear mlhdlc_hdr

%% Read the two exposued images

short = imread('mlhdlc_hdr_short.tif');
long = imread('mlhdlc_hdr_long.tif');

% define HDR output variable
HDR = zeros(size(short));
[height, width, color] = size(HDR);

figure('Name', [mfilename, '_plot']);
imshow(short, 'InitialMagnification','fit'), title('short');

imshow(long, 'InitialMagnification','fit'), title('long');

%% Create the Lum(Y) channels LUTs
% Pre-process
% Lumminance short LUT
ShortLut.x = [0    16    45    96   255];
ShortLut.y = [0    20    38    58   115];

% Lumminance long LUT
LongLut.x = [ 0 255];
LongLut.y = [ 0  140];

% Take the same points to plot the joined Lum LUT
plot_x = 0:1:255;
plot_y_short = interp1(ShortLut.x,ShortLut.y,plot_x); %LUT short
plot_y_long = interp1(LongLut.x,LongLut.y,plot_x); %LUT long

%plot(plot_x, plot_y_short, plot_x, plot_y_long, plot_x, (plot_y_long+plot_y_short)), grid on;

%% Create the HDR Lum channel 
% The HDR algorithm
% read the Y channels 

YIQ_short = rgb2ntsc(short);
YIQ_long = rgb2ntsc(long);

%% Stream image through HDR algorithm

for x=1:width
    for y=1:height
        YShort1 = round(YIQ_short(y,x,1)*255); %input short
        YLong1 = round(YIQ_long(y,x,1)*255); %input long

        YShort2 = YIQ_short(y,x,2); %input short
        YLong2 = YIQ_long(y,x,2); %input long

        YShort3 = YIQ_short(y,x,3); %input short
        YLong3 = YIQ_long(y,x,3); %input long

        valid_in = 1;
        [valid_out, x_out, y_out, HDR1, HDR2, HDR3] = mlhdlc_hdr(YShort1, YShort2, YShort3, YLong1, YLong2, YLong3, plot_y_short, plot_y_long, valid_in, x, y);

        % use x and y to reconstruct image
        if valid_out == 1
            HDR(y_out,x_out,1) = HDR1;
            HDR(y_out,x_out,2) = HDR2;
            HDR(y_out,x_out,3) = HDR3;

%% plot HDR
HDR_rgb = ntsc2rgb(HDR);
imshow(HDR_rgb, 'InitialMagnification','fit'), title('hdr ');

Simulate the Design

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


Setup for the Example

Executing the following lines copies the necessary files into a temporary folder

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

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

% copy files to the temp dir
copyfile(fullfile(mlhdlc_demo_dir, [design_name,'.m*']), mlhdlc_temp_dir);
copyfile(fullfile(mlhdlc_demo_dir, [testbench_name,'.m*']), mlhdlc_temp_dir);
copyfile(fullfile(mlhdlc_demo_dir, 'mlhdlc_hdr_long.tif'), mlhdlc_temp_dir);
copyfile(fullfile(mlhdlc_demo_dir, 'mlhdlc_hdr_short.tif'), mlhdlc_temp_dir);

Create a New HDL Coder™ Project

coder -hdlcoder -new mlhdlc_hdr_prj

Next, add the file 'mlhdlc_hdr.m' to the project as the MATLAB Function and 'mlhdlc_hdr_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.

Creating constant parameter inputs

This example shows to use pass constant parameter inputs. In this design the input parameters 'plot_y_short_in' and 'plot_y_long_in' are constant input parameters. You can define them accordingly by modifying the input types as 'constant(double(1x256))'

plot_y_short_in and plot_y_short_in are LUT inputs. They are constant folded as double inputs to the design. You will not see port declarations for these two input parameters in the generated code.

Note that inside the design 'mlhdlc_hdr.m' these variables are reassigned so that they get properly fixed-point converted. This is not necessary if these are purely used as constants for defining sizes of variables for example and not part of the logic.

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_hdr'];
clear mex;
cd (mlhdlc_demo_dir);
rmdir(mlhdlc_temp_dir, 's');