Average of multiple matrices
1 view (last 30 days)
Show older comments
Latifa Bouguessaa
on 9 Jul 2022
Commented: Latifa Bouguessaa
on 12 Jul 2022
Hello dear forum members
I have a question, I have 36 images and I want to calculate this formula (M=Bi - mean value of all images)
that is, I have to do a matrix as an average of the 36 images and then subtract the image from individual images Bi and at the end I sum the results.
does someone have an idea, how I realize this
Your Latifa
0 Comments
Accepted Answer
Karim
on 9 Jul 2022
Edited: Karim
on 10 Jul 2022
This depends how your images are stored in memory, if they are stored in a 3D array you can use the mean function
% generate 36 matrices, stored in a 3D array
IM = rand(60,60,36);
% here '3' indicates the 3d dimension (i.e. the slices)
MyMean = mean( IM, 3)
% subtract the mean from each slice
for i = 1:size(IM,3)
IM(:,:,i) = IM(:,:,i) - MyMean;
end
15 Comments
Walter Roberson
on 11 Jul 2022
M = (subMat(:,:,36)- mean(subMat,3));
That line takes the mean over the third dimension, giving a 2D double result. You then try to subtract that 2D double result from a 2D uint8 matrix. However, when you do computations on uint8 matrices, the two operands for the operation must both be uint8 unless one of the operands is a scalar double.
An alternate way of writing the code would be
M = (subMat(:,:,36)- mean(subMat,3,'native'));
which would cause mean() of uint8 data to return a uint8 result.
Reminder: when you subtract uint8 from uint8, if the subtraction would return negative, zero is returned instead. So all values in slice 36 that were less than the mean value of subMat over the third dimension, will be transformed into 0 when you use uint8 calculations. If you want the possibility of negative values then you need to conver the uint8 to double before you do the subtraction.
More Answers (0)
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!