如何提取像素边/角的坐标

时间:2017-01-27 15:29:29

标签: matlab image-processing

我理解像素元素存储在单元格的中心。但是,我需要处理边缘和角落处的坐标而不是中心坐标。

enter image description here

该代码可用于提取下图中显示的所有内部节点。

enter image description here

%obtain the pixel width
pixelsPerInch = get(0, 'ScreenPixelsPerInch');
mmPerInch     = 25.4;
mmPerPixel    = mmPerInch / pixelsPerInch; 

[nrow, ncol] = size(Img);

for ii = 1: nrow - 1
  for jj = 1: ncol - 1
    % determine the distance of each pixel from the origin
    x_mm(jj) = jj * mmPerPixel;
    y_mm(ii) = ii * mmPerPixel;

    % determine the nodal coordinates
    node_x(jj) = (x_mm(jj) + (mmPerPixel/2))/mmPerPixel;
    node_y(ii) = (y_mm(ii) + (mmPerPixel/2))/mmPerPixel;

  end
end

%%%%% merge the x and y-coords
[node_y, node_x] = meshgrid(node_y, node_x);
my_nodes         = [node_y(:), node_x(:)];

为了找到任何节点上方,下方或两侧的边缘中心,所需要的只是从node_x,node_y中加上或减去1/2,具体取决于想要去的方向。例如,要查找任何节点上方的边缘,请执行以下操作:

jx = node_x;
iy = node_y - 0.5;

通过决定是否从感兴趣点的x和y坐标加上/减去1/2,也可以从每个感兴趣的节点找到像素中心。

1 个答案:

答案 0 :(得分:0)

由于您正在处理图片及其像素坐标,因此,此答案假定您可以访问MATLAB's Image Processing Toolbox

来自MATLAB的Image Coordinate Systems documentation

  

任何像素的中心点的固有坐标(x,y)与该像素的列和行索引相同。例如,第5行第3列中像素的中心点具有空间坐标x = 3.0,y = 5.0。

     

Intrinsic Pixel Coordinate System

首先,找到每个像素的宽度(以您感兴趣的单位为准)。 Another answer on SO已经说明了如何做到这一点:

  

[T]根对象的ScreenPixelsPerInch属性。

pixelsPerInch = get(0, 'ScreenPixelsPerInch');
mmPerInch     = 25.4;
mmPerPixel    = mmPerInch / pixelsPerInch;

然后,转换像素在图像矩阵中的位置(让我们使用MATLAB的第5行和第3列的文档中的示例 - > x = 3.0y = 5.0)做类似{{1}的操作}和x_mm = 3.0 * mmPerPixel

如果您想要角落的任何位置,只需将y = 5.0 * mmPerPixel添加/减去mmPerPixel / 2x_mm变量,并确定角落的空间位置(或边缘,选择你的图像矩阵中的像素。

修改

提取您感兴趣的所有像素的右手角的空间坐标:

y_mm