LoginSignup
9
3

More than 3 years have passed since last update.

2.1.1 各種画像データフォーマットの読込み・書込み

Posted at

目次へのリンク

MATLABによる画像処理・コンピュータービジョン入門目次

概要

MATLABは様々な規格・フォーマットの画像の読み込みおよび書き込みに対応しています。

いくつかの特殊なフォーマットの読み込みについて紹介します。

  • マルチページTIFF ファイル の読込み
  • ハイ ダイナミック レンジ イメージの読込み
  • ローダイナミックレンジ画像ファイル群を使った合成
  • RAW画像の読み込み

対応ファイル

一般的な画像ファイルの読み込み

BMP/JPEG/PNGなどの一般的な画像ファイルを読み込むことができます。

code
A = imread('ngc6543a.jpg');
figure;
image(A)

figure_0.png

マルチページTIFF ファイル の読込み

画像ファイルの情報の取得

code
info = imfinfo('corn.tif')
フィールド Filename FileModDate FileSize Format FormatVersion Width Height BitDepth ColorType FormatSignature ByteOrder NewSubFileType BitsPerSample Compression PhotometricInterpretation StripOffsets SamplesPerPixel RowsPerStrip StripByteCounts XResolution YResolution ResolutionUnit Colormap PlanarConfiguration TileWidth TileLength TileOffsets TileByteCounts Orientation FillOrder
1 'C:\Program Files\MATLAB\R2020a\toolbox\matlab\imagesci\corn.tif' '11-3-2013 09:53:42' 230537 'tif' [ ] 312 415 8 'indexed' [73,73,42,0] 'little-endian' 0 8 'PackBits' 'RGB Palette' 1x16 double 1 26 1x16 double 72 72 'Inch' 256x3 double 'Chunky' [ ] [ ] [ ] [ ] 1 1
2 'C:\Program Files\MATLAB\R2020a\toolbox\matlab\imagesci\corn.tif' '11-3-2013 09:53:42' 230537 'tif' [ ] 312 415 24 'truecolor' [73,73,42,0] 'little-endian' 0 [8,8,8] 'JPEG' 'RGB' 1x52 double 3 8 1x52 double 72 72 'Inch' [ ] 'Chunky' [ ] [ ] [ ] [ ] 1 1
3 'C:\Program Files\MATLAB\R2020a\toolbox\matlab\imagesci\corn.tif' '11-3-2013 09:53:42' 230537 'tif' [ ] 312 415 8 'grayscale' [73,73,42,0] 'little-endian' 0 8 'JPEG' 'BlackIsZero' 1x52 double 1 8 1x52 double 72 72 'Inch' [ ] 'Chunky' [ ] [ ] [ ] [ ] 1 1

1番目の画像の読込みと表示

code
[I, map] = imread('corn.tif', 1);

figure;
imshow(I, map)

figure_1.png

2番目の画像の読込み

code
[I, map] = imread('corn.tif', 2);
figure;
imshow(I, map)

figure_2.png

全ての画像読み込み

code
figure;
tiledlayout(1,3,'Padding','none');
for k = 1:numel(info)
    nexttile
    [I, map] = imread('corn.tif',k);
    imshow(I, map)
end

figure_3.png

ハイ ダイナミック レンジ イメージの読込み

code
hdr_image = hdrread('office.hdr');   % m x n x 3 single

max(hdr_image(:))
output
ans = 3.2813
code
min(hdr_image(:))
output
ans = 0
code
figure;
imshow(hdr_image);      % 1.0 以上は飽和

figure_4.png

code
hdr_image(66,637,3)     % 3.2813
output
ans = 3.2813
code
hdr_image(66,634,:)     % 0.4492, 0.5898, 0.7656
output
ans = 
ans(:,:,1) =

    0.4492

ans(:,:,2) =

    0.5898

ans(:,:,3) =

    0.7656

トーンマッピング (uint8へ)

code
rgb = tonemap(hdr_image);

結果の表示

code
figure;
imshow(rgb);

figure_5.png

code
rgb(66,637,3)          % 最大値は255へマッピングされています
output
ans = 255

ローダイナミックレンジ画像ファイル群を使った合成

code
files = {'office_1.jpg', 'office_2.jpg', 'office_3.jpg', ...
         'office_4.jpg', 'office_5.jpg', 'office_6.jpg'};

% 各画像の相対露光値
expTimes = [0.0333, 0.1000, 0.3333, 0.6250, 1.3000, 4.0000];

% 最も明るいイメージと最も暗いイメージ間の中間の露光値を、
% ハイ ダイナミック レンジの計算に対して、基本露光として使用
hdr  = makehdr(files, 'RelativeExposure', expTimes ./ expTimes(1));

rgb = tonemap(hdr);
imshow(rgb)

figure_6.png

RAWデータの読込み

code
fid = fopen('I2_01_1_onion.raw', 'r', 'b');    % ファイルを開く モード:読取り、ビックエンディアン
G = (fread(fid, [198, 135], '*uint16', 'b'))'; % ファイルの読込み・転置。*があると出力も同じ型。b:ビックエンディアン
figure;
imshow(G);                              % 表示

figure_7.png

code
fclose(fid);                                   % ファイルを閉じる

メモリマッピングを使った読込み

メモリ マッピングは、ディスク上のファイルの一部または全体をアプリケーションのアドレス空間内の一定のアドレス範囲にマッピングする方法です。

これによってアプリケーションでは、動的メモリへのアクセスと同様にディスク上のファイルにアクセスできるようになります。

freadfwrite などのIO関数を使用する場合に比べファイルの読み取りと書き込みが高速化します。

大規模ファイルにランダムアクセスする場合や小さなファイルに頻繁にアクセスする場合等にも ファイルの読み取りと書き込みが高速化します。

code
m = memmapfile('I2_01_1_onion.raw')
output
m = 
    Filename: 'C:\OneDrive\OneDrive - MathWorks\Shared with Everyone\Internal\Community\Qiita\20200330_IPCV_Eval_Kit_Series\IPCV_Eval_Kit\demo_files\I2_01_1_onion.raw'
    Writable: false
      Offset: 0
      Format: 'uint8'
      Repeat: Inf
        Data: 53460x1 uint8 array

code
m.Format =  'uint16'               % Endianは、OS固有のものが使われる (Windows:Little)
output
m = 
    Filename: 'C:\OneDrive\OneDrive - MathWorks\Shared with Everyone\Internal\Community\Qiita\20200330_IPCV_Eval_Kit_Series\IPCV_Eval_Kit\demo_files\I2_01_1_onion.raw'
    Writable: false
      Offset: 0
      Format: 'uint16'
      Repeat: Inf
        Data: 26730x1 uint16 array

code

% Endianの変更
I1 = mod(m.Data, 256) * 256 + (m.Data/256);   % m.Repeat = Infなので、全データを取込み

figure
imshow(reshape(I1, 198, 135)'); % 行列に整形、MATLABの画像形式の列優先に変換

figure_8.png

code

% memmapfileのクリア
clear m;

まとめ

MATLABで様々なフォーマットの画像を読み込み方法について紹介しました。

参考

謝辞

本記事は @eigs さんのlivescript2markdownを使わせていただいてます。

目次へのリンク

MATLABによる画像処理・コンピュータービジョン入門目次

9
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
3