Main Content

이미지 파일 읽기와 분석하기

이 예제에서는 이미지 모음을 위한 데이터저장소를 만들고, 이미지 파일을 읽어오고, 최대 평균 HSV(색상, 채도, 밝기)를 사용하여 이미지를 찾는 방법을 보여줍니다. mapreduce 함수를 사용하여 이미지를 처리하는 유사한 예제는 Compute Maximum Average HSV of Images with MapReduce 항목을 참조하십시오.

두 개의 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 값을 계산합니다. 평균값이 이전 이미지의 평균값보다 크면, 그 값을 새 최댓값(maxAvgH, maxAvgS 또는 maxAvgV)으로 기록하고 해당 이미지 데이터(dataH, dataS 또는 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.

참고 항목

| |

관련 항목