【发布时间】:2015-02-27 00:04:01
【问题描述】:
我目前正在编写一段代码,它采用中心像素 (x, y) 和半径 r,然后找到该圆形样本中每个像素的平均强度。
这是我目前的代码:
function [average] = immean(x, y, r, IMAGE)
%Initialise the variables, total and pixelcount, which will be used to
%collect the sum of the pixel intensities and the number of pixels used to
%create this total.
total = 0;
pixelcount = 0;
%Find the maximum width, nx, and maximum height, ny, of the image - so that
%it can be used to end the for-loop at the appropriate positions.
[nx ny] = size(IMAGE);
%Loop through all pixels and check whether each one falls within the range
%of the circle (specified by its x-and-y-coordinates and its radius, r).
%If a pixel does fall within that domain, its intensity is added to the
%total and the pixelcount increments.
for i = 1:nx
for j = 1:ny
if (x-i)^2 + (y-j)^2 <= r^2
total = total + IMAGE(i, j);
pixelcount = pixelcount + 1;
end
end
end
但是,问题是,当我打印出 total 时,我一直得到值 255。我认为这是因为 MatLab 知道像素的最大强度是 255,所以它阻止了 total大于那。那么如何将此像素强度转换为普通整数,使 MatLab 不会将total 限制为 255?
【问题讨论】:
-
转换为
double()? -
为什么选择双精度而不是整数?无论如何,强度值都以整数形式给出。至少,我打印出所有整数的那些......
-
是的,整数也应该没问题。
-
好的,但是什么代码可以让我将强度转换为整数?
-
@Zetland - 写了一个更详细的答案。