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

2 次查看(过去 30 天)
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

采纳的回答

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
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
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Numeric Types 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by