Main Content

Read and Analyze Image Files

This example shows how to create a datastore for a collection of images, read the image files, and find the images with the maximum average hue, saturation, and brightness (HSV). For a similar example on image processing using the mapreduce function, see Compute Maximum Average HSV of Images with MapReduce.

Create a datastore containing images with .jpg and .tif extensions.

images = {'cloudCombined.jpg','landOcean.jpg','ngc6543a.jpg','street1.jpg',...
    'street2.jpg','corn.tif'};

ds = imageDatastore(images);

Initialize the maximum average HSV values and the corresponding image data.

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

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

For each image in the collection, read the image file and calculate the average HSV values across all of the image pixels. If an average value is larger than that of a previous image, then record it as the new maximum (maxAvgH, maxAvgS, or maxAvgV) and record the corresponding image data (dataH, dataS, or 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

View the images with the largest average hue, saturation, and brightness.

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

Figure contains an axes object. The hidden 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 hidden 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 hidden axes object with title Maximum Average Brightness contains an object of type image.

See Also

| |

Related Topics