Main Content

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

イメージ ファイルの読み取りおよび解析

この例では、イメージのコレクション用のデータストアの作成、イメージ ファイルの読み取り、ならびに色相、彩度および明度 (HSV) の最大平均値をもつイメージの検出を行う方法を説明します。関数 mapreduce を使用したイメージ処理の類似例については、MapReduce によるイメージの最大平均 HSV の計算を参照してください。

2 つの MATLAB® ディレクトリを特定し、これらのディレクトリ内で拡張子 .jpg.tif および .png をもつイメージを含むデータストアを作成します。

location1 = fullfile(matlabroot,'toolbox','matlab','demos');
location2 = fullfile(matlabroot,'toolbox','matlab','imagesci');

ds = imageDatastore({location1,location2},'FileExtensions',{'.jpg','.tif','.png'});

最大平均 HSV 値と対応するイメージ データを初期化します。

maxAvgH = 0;
maxAvgS = 0;
maxAvgV = 0;

dataH = 0;
dataS = 0;
dataV = 0;

コレクション内のイメージごとに、イメージ ファイルを読み取り、全イメージ ピクセルの平均 HSV 値を計算します。平均値が以前のイメージの平均値より大きい場合、それを新しい最大値 (maxAvgHmaxAvgS または maxAvgV) として記録し、対応するイメージ データ (dataHdataS または dataV) を記録します。

for i = 1:length(ds.Files)
    data = readimage(ds,i);     % Read the ith image    
    if ~ismatrix(data)          % Only process 3-dimensional color data        
        hsv = rgb2hsv(data);    % Compute the HSV values from the RGB data 
        
        h = hsv(:,:,1);         % Extract the HSV values
        s = hsv(:,:,2);            
        v = hsv(:,:,3);            

        avgH = mean(h(:));      % Find the average HSV values across the image
        avgS = mean(s(:));
        avgV = mean(v(:));
        
        if avgH > maxAvgH       % Check for new maximum average hue
           maxAvgH = avgH;
           dataH = data;
        end

        if avgS > maxAvgS       % Check for new maximum average saturation
           maxAvgS = avgS;
           dataS = data;
        end

        if avgV > maxAvgV       % Check for new maximum average brightness
           maxAvgV = avgV;
           dataV = data;
        end
    end
end

色相、彩度、明度の最大平均値をもつイメージを表示します。

imshow(dataH,'InitialMagnification','fit');
title('Maximum Average Hue')

Figure contains an axes object. The axes object with title Maximum Average Hue contains an object of type image.

figure
imshow(dataS,'InitialMagnification','fit');
title('Maximum Average Saturation');

Figure contains an axes object. The axes object with title Maximum Average Saturation contains an object of type image.

figure
imshow(dataV,'InitialMagnification','fit');
title('Maximum Average Brightness');

Figure contains an axes object. The axes object with title Maximum Average Brightness contains an object of type image.

参考

| |

関連するトピック