ハイ ダイナミック レンジ イメージング
この例では、ハイ ダイナミック レンジ イメージング アルゴリズムを実装する MATLAB® 設計からの HDL コードの生成方法を示します。
アルゴリズム
ハイ ダイナミック レンジ イメージング (HDRI または HDR) は、イメージ処理や写真撮影で使用される一連の技法で、現在の標準的なデジタル イメージ処理や写真撮影の技法よりも広いダイナミック レンジ (イメージの最も明るい部分から最も暗い部分までの範囲) に対応しています。HDR イメージでは、直射日光からかすかな星明かりまで、実際の光景における強度レベルの範囲をより正確に表現でき、多くの場合は同じ対象について露出が異なる複数の画像をキャプチャします。
MATLAB 設計
design_name = 'mlhdlc_hdr'; testbench_name = 'mlhdlc_hdr_tb';
関数 dbtype を使用して 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 set(0,'DefaultFigureWindowStyle' , 'normal')
22 figure('Name', [mfilename, '_plot']);
23 subplot(1,3,1);
24 imshow(short, 'InitialMagnification','fit'), title('short');
25
26 subplot(1,3,2);
27 imshow(long, 'InitialMagnification','fit'), title('long');
28
29
30 %% Create the Lum(Y) channels LUTs
31 % Pre-process
32 % Luminance short LUT
33 ShortLut.x = [0 16 45 96 255];
34 ShortLut.y = [0 20 38 58 115];
35
36 % Luminance long LUT
37 LongLut.x = [ 0 255];
38 LongLut.y = [ 0 140];
39
40 % Take the same points to plot the joined Lum LUT
41 plot_x = 0:1:255;
42 plot_y_short = interp1(ShortLut.x,ShortLut.y,plot_x); %LUT short
43 plot_y_long = interp1(LongLut.x,LongLut.y,plot_x); %LUT long
44
45 %subplot(4,1,3);
46 %plot(plot_x, plot_y_short, plot_x, plot_y_long, plot_x, (plot_y_long+plot_y_short)), grid on;
47
48
49 %% Create the HDR Lum channel
50 % The HDR algorithm
51 % read the Y channels
52
53 YIQ_short = rgb2ntsc(short);
54 YIQ_long = rgb2ntsc(long);
55
56 %% Stream image through HDR algorithm
57
58 for x=1:width
59 for y=1:height
60 YShort1 = round(YIQ_short(y,x,1)*255); %input short
61 YLong1 = round(YIQ_long(y,x,1)*255); %input long
62
63 YShort2 = YIQ_short(y,x,2); %input short
64 YLong2 = YIQ_long(y,x,2); %input long
65
66 YShort3 = YIQ_short(y,x,3); %input short
67 YLong3 = YIQ_long(y,x,3); %input long
68
69 valid_in = 1;
70
71 [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);
72
73 % use x and y to reconstruct image
74 if valid_out == 1
75 HDR(y_out,x_out,1) = HDR1;
76 HDR(y_out,x_out,2) = HDR2;
77 HDR(y_out,x_out,3) = HDR3;
78 end
79 end
80 end
81
82 %% plot HDR
83 HDR_rgb = ntsc2rgb(HDR);
84 subplot(1,3,3);
85 imshow(HDR_rgb, 'InitialMagnification','fit'), title('hdr ');
設計のシミュレーション
コードの生成前にテスト ベンチを使用して設計のシミュレーションを実行し、実行時エラーが発生しないことを常に確認することをお勧めします。
mlhdlc_hdr_tb

HDL Coder™ プロジェクトの新規作成
coder -hdlcoder -new mlhdlc_hdr_prj
次に、'mlhdlc_hdr.m' ファイルを [MATLAB 関数] としてプロジェクトに追加し、'mlhdlc_hdr_tb.m' を [MATLAB テスト ベンチ] として追加します。
MATLAB HDL Coder プロジェクトの作成と入力に関する詳細なチュートリアルについては、MATLAB から HDL へのワークフロー入門を参照してください。
定数のパラメーター入力の作成
この例では、定数のパラメーター入力の受け渡しを使用する方法を示します。
この設計の入力パラメーターである 'plot_y_short_in' と 'plot_y_long_in' は定数の入力パラメーターです。入力の型を変更して 'constant(double(1x256))' として定義できます。
'plot_y_short_in' と 'plot_y_short_in' は LUT 入力です。設計には double の入力として定数畳み込みされます。これらの 2 つの入力パラメーターについての端子の宣言は生成される HDL コードには含まれません。
設計 'mlhdlc_hdr.m' の内部では、これらの変数が固定小数点に適切に変換されるように再度代入されることに注意してください。これは、たとえば変数のサイズを定義する場合など、ロジック以外の部分で純粋に定数として使用する場合は必要ありません。
固定小数点変換と HDL コード生成の実行
HDL アドバイザーを起動し、[コード生成] のステップを右クリックして [選択したタスクまで実行] オプションを選択し、最初から 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 コードを確認します。