【问题标题】:How do I obtain the count of corner points in a matrix as well as store their positions in an array?如何获取矩阵中角点的计数并将它们的位置存储在数组中?
【发布时间】:2025-12-06 08:50:02
【问题描述】:

有人告诉我,矩阵的坐标不一定要用中心点表示,也可以用角点表示。然而,我真的不知道该怎么做。尽管如此,我有这些信息与中心点和角点的矩阵符号相关,如下所示。

[Nr, Nc] = size(Img);

还有:

Nr = ny+1; % which is total no. of centre rows Nc = nx+1; % which is total no. of center columns

还有,

ny = Nr – 1; % which is total no. of line rows nx = Nc – 1; % which is total no. of line columns

T = ny * nx; % where T = Total no. of internal nodes nodeCount = nx(iy – 1) + jx;

计数顺序是逐行的,即从节点开始:(1,1) to (1,nx),然后是(2,1) to (2, nx),等等。请注意,在给定的图像(6x6 矩阵)中,nodeCount 有 25 个数组元素。

现在,我需要获取 nodeCount 以便它包含从 1 到 T 按指定计数顺序计数的所有内部节点的数组。然后,我将从 nodeCount 中的每个索引中获取 x 和 y 坐标。

请,我需要有关如何解决此问题的帮助/建议/建议。非常感谢。

【问题讨论】:

  • 你的描述有点模糊,是[x,y] = meshgrid(1:nx,1:ny); coords = [x(:),y(:)]你需要的吗?
  • 非常感谢@Florian。抱歉这么晚才回复。是的,您的代码让我得到x and y coords,这是问题的第二部分。然而,我仍然需要找到nodeCount。感谢您的帮助。
  • 如果我理解正确的话,nodeCount 应该是nodeCount = nx*(coords(:,2)-1) + coords(:,1)
  • 是的,没错。我确实从中得到了 nodeCount,但是从整个代码的工作方式来看,我需要拥有 nodeCounts,然后从 nodeCount 中提取 x 和 y 坐标以进行进一步的计算。我希望你明白我的意思?
  • 我希望我明白你的意思:我做了一个回复,显示了所有 4 个变体:从头开始创建 nodeCountcoords 并将一个转换为另一个。您问的最后一个是数字 4):将 nodeCount 转换为坐标。

标签: matlab image-processing


【解决方案1】:

由于在cmets的讨论有点长,我决定回答一下:

1) 创建坐标(从头开始)

[x,y] = meshgrid(1:nx,1:ny); 
coords = [x(:),y(:)];

2) 创建nodeCount(从头开始)

nodeCount = reshape(reshape((1:nx*ny),nx,ny)',nx*ny,[]);

3) 将坐标转换为 nodeCount

nodeCount = nx*(coords(:,2)-1) + coords(:,1);

4) 将 nodeCount 转换为坐标

coords_x = mod(nodeCount-1,nx) + 1;
coords_y = (nodeCount - coords_x)/nx + 1;
coords = [coords_x, coords_y];

edit:当然,3-D 也可以。取决于你如何计算你的节点。一种方法是这样(未经测试,但我想你明白了):

[x,y,z] = meshgrid(1:nx,1:ny,1:nz);
coords = [x(:),y(:),z(:)];
nodeCount = ny*nx*(coords(:,3)-1) + nx*(coords(:,2)-1) + coords(:,1)
coords_x = mod(nodeCount-1,nx) + 1;
coords_y = mod((nodeCount - coords_x)/nx, ny) + 1;
coords_z = (nodeCount - coords_x - nx*(coords_y-1))/nx/ny + 1;

【讨论】:

  • 完美!。非常感谢!
  • 只是一个想法...我想知道是否以及是否有可能将第三维度纳入其中。有什么想法可以让我获得 nodeCount 及其坐标吗?
  • 哦!非常感谢。将立即检查并通知您。干杯!
  • 嗨@Florian。请问可以私聊你吗?
  • 嗯,如果你保证不会把你的代码发给我检查它,那好吧。 ;-)