Compute Maximum Average HSV of Images with MapReduce - MATLAB & Simulink (original) (raw)

This example shows how to use ImageDatastore and mapreduce to find images with maximum hue, saturation and brightness values in an image collection.

Prepare Data

Create a datastore using several images in the current folder. Select .jpg images only using the FileExtensions Name-Value pair.

ds = imageDatastore(pwd,'FileExtensions','.jpg');

Find Average Maximum HSV from All Images

One way to find the maximum average hue, saturation, and brightness values in the collection of images is to use readimage within a for-loop, processing the images one at a time. For an example of this method, see Read and Analyze Image Files.

This example uses mapreduce to accomplish the same task, however, the mapreduce method is highly scalable to larger collections of images. While the for-loop method is reasonable for small collections of images, it does not scale well to a large collection of images.

Scale to MapReduce

Map Function

function hueSaturationValueMapper(data, info, intermKVStore) if ~ismatrix(data) hsv = rgb2hsv(data);

% Extract Hue values
h = hsv(:,:,1);

% Extract Saturation values
s = hsv(:,:,2);

% Extract Brightness values
v = hsv(:,:,3);

% Find average of HSV values
avgH = mean(h(:));
avgS = mean(s(:));
avgV = mean(v(:));

% Add intermediate key-value pairs
add(intermKVStore, 'Average Hue', struct('Filename', info.Filename, 'Avg', avgH));
add(intermKVStore, 'Average Saturation', struct('Filename', info.Filename, 'Avg', avgS));
add(intermKVStore, 'Average Brightness', struct('Filename', info.Filename, 'Avg', avgV));

end end

Reduce Function

function hueSaturationValueReducer(key, intermValIter, outKVSTore) maxAvg = 0; maxImageFilename = '';

% Loop over values for each key while hasnext(intermValIter) value = getnext(intermValIter); % Compare values to determine maximum if value.Avg > maxAvg maxAvg = value.Avg; maxImageFilename = value.Filename; end end

% Add final key-value pair add(outKVSTore, ['Maximum ' key], maxImageFilename); end

Run MapReduce

Use mapreduce to apply the map and reduce functions to the datastore, ds.

maxHSV = mapreduce(ds, @hueSaturationValueMapper, @hueSaturationValueReducer);



Map 0% Reduce 0% Map 20% Reduce 0% Map 40% Reduce 0% Map 60% Reduce 0% Map 80% Reduce 0% Map 100% Reduce 0% Map 100% Reduce 33% Map 100% Reduce 67% Map 100% Reduce 100%

mapreduce returns a datastore, maxHSV, with files in the current folder.

Read and display the final result from the output datastore, maxHSV. Use find and strcmp to find the file index from the Files property.

tbl = readall(maxHSV); for i = 1:height(tbl) figure; idx = find(strcmp(ds.Files, tbl.Value{i})); imshow(readimage(ds, idx), 'InitialMagnification', 'fit'); title(tbl.Key{i}); end

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

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

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

See Also

mapreduce | imageDatastore | tall

Topics