Question

i have 100 image that I have read and store in a matrix of the form <128x128x100 Double> and I want to apply an operation on each image. how can I do? I have another matrix of the form <40x20 cell> witch contain 800 image and I want to do the same thing, ie applying an operation on each image. I do not know how to make a loop that carries it. thank you for your help

Was it helpful?

Solution

Assuming your function is in this form function imageOut = someFunction(imageIn):

First case: (assuming I is a MxNxP matrix with P images, each MxN)

O = cell2mat(cellfun(@someFunction, num2cell(I, [1 2]), 'UniformOutput', false))

Second case: (assuming I is a MxN cell array, each cell contains an image)

O = cellfun(@someFunction, I, 'UniformOutput', false)

OTHER TIPS

The most straight forward aspproach is to use a for-loop.
For the 128-by-128-by-100 matrix (let's call it M) you can do

for imi = 1:size(M,3)
    currImg = M(:,:,imi); % take a "slice" of M = an image
    % process currImg here...
end

For the 40-by-20 cell array (let's call it C) you can do

for ri = 1:size(C,1)
    for ci = 1:size(C,2)
        currImg = C{ri,ci}; % get an image stored in cell ri,ci
        % process currImg here...
    end
end

Notice the different modes of accessing matrices (using ()) and accessing cell elements (using {}).

This really really really depends on the operation you want to apply. For example, if you want to dilate each image with a 3x3 square, all you have to do is pass the 3D matrix and the 3x3 square.

imdilate (img, true (3, 3));

This simple example will apply dilation on all the 2D images of the matrix, it's all a matter of choosing the right structuring element (the true (3, 3)). Of course, you can use a for loop too, but that's not how this language was meant to be used. And isn't the example above much more elegant than a loop?

A well designed function for the Matlab language should be handling this cases for you, and work across all images of a n-dimensional matrix. It is up to you to understand the operator and function well enough to make it do what you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top