【问题标题】:Convert the intensity value of a pixel to an integer将像素的强度值转换为整数
【发布时间】: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 - 写了一个更详细的答案。

标签: matlab image-processing


【解决方案1】:

根据 Divakar 的评论,您选择 double 主要是为了提高位精度以及 MATLAB 以这种方式自然处理数组。此外,当您取事物的平均值时,您还希望具有浮点精度,因为求某事物的平均值将不可避免地得到浮点值。

我有点困惑,为什么您不想在平均值之后保留小数点,尤其是在准确性方面。但是,这不是关于意图的问题,而是您的问题中列出的您想要做什么的问题。因此,如果您不想要小数点,只需将图像转换为高于 8 位的整数精度以避免剪切。像uint32 这样你就可以得到一个 32 位无符号整数,甚至是 uint64 一个 64 位无符号整数。要尽量减少剪辑,请转换为 uint64。因此,只需执行以下操作:

IMAGE = uint64(IMAGE);

在任何处理之前在函数的开头执行此操作。

【讨论】:

  • 很高兴得到解释!
  • @Divakar - 谢谢 :) 不幸的是,这个不属于心理主义者类别,因为它很清楚。
  • uint322^24 像素以下是安全的,高于它也可能饱和。那是“仅” 16 百万像素。我会切换到uint64
  • @Daniel - 好主意!我想,既然这听起来像是一项任务,我的猜测是圆圈可能不会那么大……至少我希望如此。我一定会改变精度。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-23
  • 2022-01-06
  • 2018-03-16
  • 2017-04-05
相关资源
最近更新 更多