MATLAB Answers

0

How to convert the mean value of an integer matrix to a double

Jonathan Wells さんによって質問されました 2016 年 5 月 25 日
最新アクティビティ Chris Turnes
さんによって 編集されました 2016 年 5 月 26 日
Hello,
I am importing an image as a 2D matrix using
importdata('myImage').
I am then trying to search 4x4 pixel regions of that image and calculate the mean of each image to determine which region has the highest mean value. However, MATLAB will only return the mean values as integer numbers. How do I make it return values as type double?
My code for the mean is as follows.
for ii = 1:((frameHeight/2)-3)
for jj = 1:(frameWidth-3)
regionA(ii,jj) = mean(mean(frame1(ii:ii+3, jj:jj+3), 'double'));
end
end
for ii = (frameHeight/2):(frameHeight-3)
for jj = 1:(frameWidth-3)
regionB(ii,jj) = mean(mean(frame1(ii:ii+3, jj:jj+3), 'double'));
end
end
(I'm splitting the image into a top and bottom half to separate objects in the image and trying to find the max region in each half.)
Also, sorry for the poor formatting. It's my first time posting here and I'm not sure why part of the code isn't showing up correctly. But basically, what can I do to make the mean output as type double? Any help would be greatly appreciated.
-Jon

  0 件のコメント

サインイン to comment.

1 件の回答

Chris Turnes
回答者: Chris Turnes
2016 年 5 月 25 日
編集済み: Chris Turnes
2016 年 5 月 26 日
 採用された回答

Are regionA and regionB allocated as integers? It seems likely he final assignment that is casting them back to integers. Since the inner call to mean uses the 'double' option, the nested mean call is indeed returning a double output. But this is getting cast to an integer because the array in which you are storing it is of integer type. Compare the following:
>> A = zeros(1,1,'int32');
>> B = zeros(1,1,'double');
>> x = randi(10, 10, 1, 'int32');
>> A(1) = mean(x, 'double')
A =
7
>> B(1) = mean(x, 'double')
B =
6.6000

  2 件のコメント

Jonathan Wells 2016 年 5 月 25 日
Ah, that did the trick. I didn't even notice that my outer mean function wasn't casting as double. I just assumed that if I used it in the nested function, it would apply to the outer one as well.
Thank you!
Chris Turnes
2016 年 5 月 26 日
Glad to help! However, just to be clear, the code that you originally posted is doing the outer calculation in double -- it is just the subscripted assignment that changes the type because of the type of the output array. Doing
mean(mean(x,'double'))
is equivalent to
y = mean(x, 'double');
z = mean(y);
"y", in this case, is double, and mean of a double is a double. The integer conversion is happening during the subscripted assignment to "regionA" and "regionB" because they are allocated as integers.
>> x = eye(10, 'uint8');
>> class(mean(mean(x, 'double')))
ans =
double
>> R = int32(0);
>> R(1) = mean(mean(x,'double'));
>> class(R)
ans =
int32
Note also that it's the fact that it's a subscripted assignment. If you do a full assignment, then your variable is just overwritten with the result (which is then a double):
>> R = mean(mean(x,'double'));
>> class(R)
ans =
double
This is a subtle distinction, but important for realizing what's going on.

サインイン to comment.



Translated by