В этом примере показано, как сгенерировать HDL-код из проекта MATLAB®, который реализует расширенный динамический диапазон алгоритм обработки изображений.
Обработка изображений Расширенного динамического диапазона (HDRI или HDR) является набором методов, используемых в обработке изображений и фотографии, чтобы позволить больший динамический диапазон между самыми легкими и самыми темными областями изображения, чем текущие стандартные цифровые методы обработки изображений или фотографические методы. Изображения HDR могут представлять более точно область значений уровней яркости, найденных в действительных сценах, от прямого солнечного света, чтобы ослабеть звездный свет, и часто получаются посредством множества по-другому отсоединенных изображений той же темы.
design_name = 'mlhdlc_hdr'; testbench_name = 'mlhdlc_hdr_tb';
Давайте смотреть на проект MATLAB
dbtype(design_name);
1 function [valid_out, x_out, y_out, ... 2 HDR1, HDR2, HDR3] = mlhdlc_hdr(YShort1, YShort2, YShort3, ... 3 YLong1, YLong2, YLong3, ... 4 plot_y_short_in, plot_y_long_in, ... 5 valid_in, x, y) 6 % 7 8 % Copyright 2013-2015 The MathWorks, Inc. 9 10 % This design implements a high dynamic range imaging algorithm. 11 12 plot_y_short = plot_y_short_in; 13 plot_y_long = plot_y_long_in; 14 15 %% Apply Lum(Y) channels LUTs 16 y_short = plot_y_short(uint8(YShort1)+1); 17 y_long = plot_y_long(uint8(YLong1)+1); 18 19 y_HDR = (y_short+y_long); 20 21 %% Create HDR Chorm channels 22 % HDR per color 23 24 HDR1 = y_HDR * 2^-8; 25 HDR2 = (YShort2+YLong2) * 2^-1; 26 HDR3 = (YShort3+YLong3) * 2^-1; 27 28 %% Pass on valid signal and pixel location 29 30 valid_out = valid_in; 31 x_out = x; 32 y_out = y; 33 34 end
dbtype(testbench_name);
1
2 %
3
4 % Copyright 2013-2015 The MathWorks, Inc.
5
6 % Clean screen and memory
7 close all
8 clear mlhdlc_hdr
9 set(0,'DefaultFigureWindowStyle','docked')
10
11
12 %% Read the two exposed images
13
14 short = imread('mlhdlc_hdr_short.tif');
15 long = imread('mlhdlc_hdr_long.tif');
16
17 % define HDR output variable
18 HDR = zeros(size(short));
19 [height, width, color] = size(HDR);
20
21 figure('Name', [mfilename, '_plot']);
22 subplot(1,3,1);
23 imshow(short, 'InitialMagnification','fit'), title('short');
24
25 subplot(1,3,2);
26 imshow(long, 'InitialMagnification','fit'), title('long');
27
28
29 %% Create the Lum(Y) channels LUTs
30 % Pre-process
31 % Luminance short LUT
32 ShortLut.x = [0 16 45 96 255];
33 ShortLut.y = [0 20 38 58 115];
34
35 % Luminance long LUT
36 LongLut.x = [ 0 255];
37 LongLut.y = [ 0 140];
38
39 % Take the same points to plot the joined Lum LUT
40 plot_x = 0:1:255;
41 plot_y_short = interp1(ShortLut.x,ShortLut.y,plot_x); %LUT short
42 plot_y_long = interp1(LongLut.x,LongLut.y,plot_x); %LUT long
43
44 %subplot(4,1,3);
45 %plot(plot_x, plot_y_short, plot_x, plot_y_long, plot_x, (plot_y_long+plot_y_short)), grid on;
46
47
48 %% Create the HDR Lum channel
49 % The HDR algorithm
50 % read the Y channels
51
52 YIQ_short = rgb2ntsc(short);
53 YIQ_long = rgb2ntsc(long);
54
55 %% Stream image through HDR algorithm
56
57 for x=1:width
58 for y=1:height
59 YShort1 = round(YIQ_short(y,x,1)*255); %input short
60 YLong1 = round(YIQ_long(y,x,1)*255); %input long
61
62 YShort2 = YIQ_short(y,x,2); %input short
63 YLong2 = YIQ_long(y,x,2); %input long
64
65 YShort3 = YIQ_short(y,x,3); %input short
66 YLong3 = YIQ_long(y,x,3); %input long
67
68 valid_in = 1;
69
70 [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);
71
72 % use x and y to reconstruct image
73 if valid_out == 1
74 HDR(y_out,x_out,1) = HDR1;
75 HDR(y_out,x_out,2) = HDR2;
76 HDR(y_out,x_out,3) = HDR3;
77 end
78 end
79 end
80
81 %% plot HDR
82 HDR_rgb = ntsc2rgb(HDR);
83 subplot(1,3,3);
84 imshow(HDR_rgb, 'InitialMagnification','fit'), title('hdr ');
Это всегда - хорошая практика, чтобы симулировать проект с испытательным стендом до генерации кода, чтобы убедиться, что нет никаких ошибок периода выполнения.
mlhdlc_hdr_tb

Выполнение следующих линий копирует необходимые файлы во временную папку
mlhdlc_demo_dir = fullfile(matlabroot, 'toolbox', 'hdlcoder', 'hdlcoderdemos', 'matlabhdlcoderdemos'); mlhdlc_temp_dir = [tempdir 'mlhdlc_hdr']; % 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 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);
coder -hdlcoder -new mlhdlc_hdr_prj
Затем добавьте файл 'mlhdlc_hdr.m' в проект как функция MATLAB и 'mlhdlc_hdr_tb.m' как Испытательный стенд MATLAB.
Можно обратиться к Началу работы с MATLAB к примеру по Рабочему процессу HDL для более полного примера при создании и заполнении проектов HDL Coder MATLAB.
Этот пример показывает, чтобы использовать передачу постоянные входные параметры параметра.
В этом проекте входные параметры 'plot_y_short_in' и 'plot_y_long_in' являются постоянными входными параметрами. Можно задать их соответственно путем изменения входных типов как 'постоянных (дважды (1x256))'
'plot_y_short_in' и 'plot_y_short_in' являются входными параметрами LUT. Они постоянные свернутый как двойные входные параметры к проекту. Вы не будете видеть объявления порта для этих двух входных параметров в сгенерированном HDL-коде.
Обратите внимание на то, что в проекте 'mlhdlc_hdr.m' эти переменные повторно присвоены так, чтобы они получили правильно преобразованную фиксированную точку. Это не необходимо, если они просто используются в качестве констант для определения размеров переменных, например, и не части логики.
Запустите Советника по вопросам HDL и щелкните правой кнопкой мыши по 'Генерации кода', продвигаются и выбирают опцию, 'Запущенную к выбранной задаче', чтобы запустить все шаги с начала через генерацию HDL-кода.
Следующий скрипт преобразует проект в фиксированную точку, и сгенерируйте HDL-код с испытательным стендом.
exArgs = {0,0,0,0,0,0,coder.Constant(ones(1,256)),coder.Constant(ones(1,256)),0,0,0};
fc = coder.config('fixpt');
fc.TestBenchName = 'mlhdlc_hdr_tb';
hc = coder.config('hdl');
hc.GenerateHDLTestBench = true;
hc.SimulationIterationLimit = 1000; % Limit number of testbench points
codegen -float2fixed fc -config hc -args exArgs mlhdlc_hdrИсследуйте сгенерированный HDL-код путем нажатия на гиперссылки в окне Code Generation Log.
Можно запустить следующие команды, чтобы очистить временную папку проекта.
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');