I’m trying to write a function in matlab for averaging images (all of the same size and format (.png)). But it’s not really working at the moment. The error i get is: “Error using +, Integers can only be combined with integers of the same class, or scalar doubles.“
The code is below. The images are all resized to the same size, and have padding applied if the proportions aren’t equal (padding is done with an alpha channel, to make the padding transparent). The images are all proper png’s, and can be opened in photoshop etc. without errors.
If i understand correctly, you don’t have to initialize or declare variables in matlab. Which is probably part of my problem. My normal (processing/java) approach would be to initialize containerAv to be of size (x, y, 3), add all the color values of all images to it, and then divide all values by the number of images. Giving you the average image.
I don’t really know how to do this in matlab. What am i doing wrong? How do i rewrite this code so it gets me the rgb average image?
function imageCentroid(imageList, resizeFolder, outputFolder, x, y)
% local variable
centroidImage = zeros([x y 3]);
% loop through all images in the resizeFolder
for i=1:length(imageList)
% get filename and load image
fname = imageList(i).name;
container = imread([resizeFolder fname]);
% add container values to centroidImage
centroidImage = centroidImage + container;
end
% calculate mean image (divide by number of images)
centroidImage = centroidImage / length(imageList);
% save mean image
imwrite(centroidImage, [outputFolder 'centroid.png']);
end
Probably change:
to