Average of multiple matrices
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Karim
2022-7-9
编辑:Karim
2022-7-10
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 个评论
Walter Roberson
2022-7-11
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!