Average of multiple matrices

조회 수: 3 (최근 30일)
Latifa Bouguessaa
Latifa Bouguessaa 2022년 7월 9일
댓글: Latifa Bouguessaa 2022년 7월 12일
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

채택된 답변

Karim
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)
MyMean = 60×60
0.5423 0.5302 0.4499 0.6049 0.5529 0.4937 0.4482 0.5046 0.4249 0.5116 0.5654 0.5206 0.5490 0.4849 0.4883 0.4870 0.4785 0.5159 0.4727 0.4874 0.4605 0.5692 0.5124 0.5299 0.3905 0.5232 0.5547 0.4935 0.5231 0.4855 0.5468 0.4794 0.4764 0.5231 0.5032 0.5078 0.4658 0.4594 0.4497 0.5204 0.5166 0.4808 0.5565 0.4713 0.4848 0.4805 0.4335 0.5317 0.5321 0.4965 0.4896 0.5081 0.4681 0.3973 0.4686 0.4634 0.5425 0.5439 0.5559 0.4928 0.5300 0.5006 0.5265 0.4788 0.5069 0.5307 0.4282 0.5432 0.4681 0.4531 0.5471 0.5047 0.5714 0.4917 0.5110 0.5373 0.5117 0.4247 0.4278 0.4275 0.5413 0.5046 0.5123 0.4798 0.4597 0.5371 0.5881 0.4942 0.5025 0.5276 0.4752 0.4677 0.4788 0.4446 0.5567 0.4630 0.4647 0.5636 0.5216 0.5469 0.5187 0.4974 0.5457 0.5155 0.4299 0.5183 0.3784 0.4394 0.4890 0.4367 0.5318 0.4033 0.4858 0.5446 0.4996 0.4741 0.5789 0.4643 0.4695 0.5074 0.4532 0.3975 0.6091 0.4424 0.5648 0.4693 0.4671 0.4325 0.5529 0.4172 0.5023 0.4736 0.5148 0.5017 0.5227 0.5115 0.5303 0.4667 0.5141 0.5075 0.4537 0.4380 0.4438 0.5337 0.4947 0.5352 0.4409 0.4874 0.5363 0.5250 0.4288 0.5157 0.4929 0.4885 0.4616 0.5684 0.5134 0.5041 0.4447 0.4677 0.4846 0.4331 0.4827 0.5668 0.4760 0.4696 0.4597 0.5153 0.4943 0.5200 0.5452 0.5485 0.5282 0.5239 0.5404 0.4633 0.4966 0.4686 0.5248 0.4939 0.4564 0.4663 0.5359 0.5494 0.4919 0.5289 0.6235 0.4576 0.4335 0.5506 0.5256 0.4485 0.5047 0.4727 0.5368 0.4898 0.3592 0.4526 0.4088 0.5358 0.4045 0.3875 0.4460 0.4536 0.5408 0.4599 0.4638 0.5069 0.5380 0.5099 0.6138 0.3802 0.5928 0.4648 0.5081 0.4707 0.4212 0.4195 0.5440 0.4571 0.5042 0.5226 0.5296 0.5771 0.3977 0.5137 0.5115 0.4058 0.5647 0.4902 0.5190 0.5141 0.4769 0.5969 0.4781 0.4203 0.5499 0.4942 0.4371 0.4919 0.6080 0.5764 0.4546 0.4690 0.4312 0.5565 0.4930 0.4178 0.4755 0.4580 0.4496 0.4649 0.4300 0.4207 0.5368 0.4766 0.5311 0.4218 0.5017 0.4895 0.5588 0.5115 0.4546 0.5221 0.5602 0.5118 0.4663 0.5374 0.4802 0.4736 0.4812 0.5693 0.4882 0.5565 0.4741 0.5420 0.5190 0.5387 0.4813 0.5916 0.5528 0.5074 0.4795 0.5309 0.4927 0.5546 0.4946 0.5187 0.5807 0.5787 0.5524 0.5082 0.5658 0.5423 0.4987 0.6166 0.4134 0.5001 0.4329 0.4975
% subtract the mean from each slice
for i = 1:size(IM,3)
IM(:,:,i) = IM(:,:,i) - MyMean;
end
  댓글 수: 15
Walter Roberson
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.
Latifa Bouguessaa
Latifa Bouguessaa 2022년 7월 12일
thank you all very much for your help

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by