Sum of a cell of arrays

My code consists of scanning the current directory for all .bmp image files, creating a cell array (called "C") with the names of these files, and then imread-ing them. This creates a cell of the array images.
My question is how to take the sum of each image array read to create a single new array? The sum function obviously doesn't work since it produces a single value output. Manually typing C{1} + C{2} (and so on) will get me the correct results, but I need this to be automated and to work for any number of image files. I have tried to do a recursive function as well, but apparently it doesn't like adding arrays together. My code is shown below:
files = dir(fullfile(pwd, '*.bmp'));
C = cell(length(files), 1);
for k = 1:length(files);
names = files(k).name;
C{k} = imread(names);
end
le = length(files);
Final = recursive(le);
function r = recursive(x)
if (x == 0)
total = 0;
else
files = dir(fullfile(pwd, '*.bmp'));
C = cell(length(files), 1);
for k = 1:length(files);
names = files(k).name;
C{k} = imread(names);
total = C{x} + recursive(x-1);
end
end
r = total;
I of course have the first set of scripts under another function, but have not pasted it here. Any help would be appreciated.

回答 (2 件)

José-Luis
José-Luis 2014 年 6 月 23 日

0 投票

%dummy data
C = cell(10,1);
m = 15; %image size
n = 18;
numImages = 20;
for ii = 1:numImages
C(ii) = {rand(m,n)};
end
%The sum:
your_sum = sum ( reshape (cell2mat(C),m,n,numImages), 3 );

4 件のコメント

Michael
Michael 2014 年 6 月 23 日
Thanks for replying José-Luis!
I've tried adapting your code towards mine and came up with this:
files = dir(fullfile(pwd, '*.bmp'));
C = cell(length(files), 1);
for k = 1:length(files);
names = files(k).name;
C{k} = imread(names);
si = size(C{1});
m = si(1);
n = si(2);
le = length(files);
your_sum = sum( reshape (cell2mat(C),m,n, le), 3);
end
However, I realized that my images files have a 3rd dimension for some reason and are of type uint8. With my sample data, my cell array is of this format:
C =
[541x541x3 uint8]
[541x541x3 uint8]
[541x541x3 uint8]
[541x541x3 uint8]
Running the code while disregarding the third dimension just gave me
??? Error using ==> cell2mat at 46
All contents of the input cell array must be of the same data type.
Do I need to account for the 3rd dimension in some way?
José-Luis
José-Luis 2014 年 6 月 23 日
編集済み: José-Luis 2014 年 6 月 23 日
What does the sum of the images represent? Say you have two rgb vectors [0 255 127] and [255 0 255]. What should you get for the sum?
EDIT Also, I assumed that all your images are the same size. If not, you have to rescale them before you can add them.
Michael
Michael 2014 年 6 月 24 日
Yes all the images will be the same size. The images I am combining are black and white images. Each image has different areas of white (or gray), and I am trying to see the total area of "whiteness" that occurs in the final composite picture. So for each pixel in the image, I am trying to find the total sum of its "whiteness" from the other pictures. A spot that is lit up in every picture should produce a bright white pixel, while other pixels that do not light up in every image should be gray in the final composite.
Therefore, the maximum sum of a pixel should result in a bright white pixel. I hope this helped answer your question.
José-Luis
José-Luis 2014 年 6 月 24 日
And how would you define "whiteness" mathematically. Maybe you need to make your image grayscale first and pick the ones with the lowest sum.

サインインしてコメントする。

Michael sch
Michael sch 2014 年 6 月 23 日

0 投票

so what you can do get length(x) and then loop for all x and then sum them k=1:length(x) y=y+x{k} end
x{1}=[1 2; 3 4]
x{2}=[ 5 6 ;7 8]
x{1}+x{2}
ans =
6 8
10 12
length(x)
ans =
2

カテゴリ

ヘルプ センター および File ExchangeImages についてさらに検索

タグ

質問済み:

2014 年 6 月 23 日

コメント済み:

2014 年 6 月 24 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by