Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 给定一个像素坐标,我如何提取他的所有邻居?_Matlab_Image Processing_Matrix - Fatal编程技术网

Matlab 给定一个像素坐标,我如何提取他的所有邻居?

Matlab 给定一个像素坐标,我如何提取他的所有邻居?,matlab,image-processing,matrix,Matlab,Image Processing,Matrix,我想为一个给定的矩阵点构建一个3x3块,该点位于块的中心。这是我的代码: function frmBlock = fetchNeighbors(frame, row, column) %Create a 3x3 matrix contains the neighbors of the point(x, y) %[n, m] = size(frame); frmBlock = zeros(3, 3); x = floor(row); y = floor(column); frmBlock(

我想为一个给定的矩阵点构建一个3x3块,该点位于块的中心。这是我的代码:

function frmBlock = fetchNeighbors(frame, row, column)
%Create a 3x3 matrix contains the neighbors of the point(x, y)
%[n, m] = size(frame);
frmBlock = zeros(3, 3);
x = floor(row);
y = floor(column);
    frmBlock(1) = frame(x-1, y-1);
    frmBlock(2) = frame(x, y-1);
    frmBlock(3) = frame(x+1, y+1);
    frmBlock(4) = frame(x-1, y);
    frmBlock(5) = frame(x, y);
    frmBlock(6) = frame(x+1, y);
    frmBlock(7) = frame(x-1, y+1);
    frmBlock(8) = frame(x, y+1);
    frmBlock(9) = frame(x+1, y-1);
end
如您所见,我创建了一个由0初始化的3x3矩阵。我想做的是用输入坐标(行、列)的所有邻域填充该矩阵。如果由于某种原因我无法找到邻居,我什么也不做(即,将3x3块中的位置设为0)

当我运行这段代码时,我得到一个错误,上面写着:

使用fetchneights(第12行)索引时出错,超出了矩阵维度。


有人能帮忙吗?

我猜这个错误是因为您将
放在矩阵
的边界上,然后当您尝试访问元素时,正好是在右边或左边,或者在下面或上面(取决于您所处的边界),您超出了界限,这将引发错误。例如,如果
等于
1
,这意味着您试图在某个点访问
帧(0,列)
,这是非法的

您可以通过在对矩阵的任何访问之前添加一个检查(使用
if
语句)来解决这个问题,以确保您在边界内。我在此补充另一种方法:

function frmBlock = fetchNeighbors(frame, row, column)
% Create a 3x3 matrix that contains the neighbors of the point (row,column)
    [n,m] = size(frame);
    neighbors_x = max(row-1,1):min(row+1,n);
    neighbors_y = max(column-1,1):min(column+1,m);
    frmBlock = zeros(3,3);
    frmBlock(neighbors_x-row+2,neighbors_y-column+2) = frame(neighbors_x,neighbors_y);
end

我猜错误是因为您将
设置在矩阵
的边界上,然后当您尝试访问元素的右、左、下或上方(取决于您所处的边界)时,您超出了边界,这会引发错误。例如,如果
等于
1
,这意味着您试图在某个点访问
帧(0,列)
,这是非法的

您可以通过在对矩阵的任何访问之前添加一个检查(使用
if
语句)来解决这个问题,以确保您在边界内。我在此补充另一种方法:

function frmBlock = fetchNeighbors(frame, row, column)
% Create a 3x3 matrix that contains the neighbors of the point (row,column)
    [n,m] = size(frame);
    neighbors_x = max(row-1,1):min(row+1,n);
    neighbors_y = max(column-1,1):min(column+1,m);
    frmBlock = zeros(3,3);
    frmBlock(neighbors_x-row+2,neighbors_y-column+2) = frame(neighbors_x,neighbors_y);
end

如果您有RGB格式的彩色图像,则需要
frmBlock=nan(3,3,3)
。第三个维度是RBG向量
frmBlock(1,1,:)=帧(x-1,y-1,:)
将对一个像素执行此操作。不,我没有彩色图像。我更新我的问题。如果您有RGB格式的彩色图像,您需要
frmBlock=nan(3,3,3)
。第三个维度是RBG向量
frmBlock(1,1,:)=frame(x-1,y-1,:)
将对一个像素起作用。不,我没有彩色图像。我更新了我的问题。谢谢你的回答,它起作用了,如果可以的话,最后一件事就是,输入(行,列)不是整数,它们是实的,所以a使用了
round()
函数,但我得到了以下错误:
索引操作无效
访问非整数索引中的矩阵无效。如果确保对整数进行四舍五入,则不应出现该错误。如果您这样做,我建议您重新检查代码,如果问题仍然存在,请打开一个新的StackOverflow问题以解决该问题。您可以在评论中添加问题的链接。再次感谢@Lior。感谢您的回答,它成功了,如果可以的话,最后一件事就是,输入(行、列)不是整数,而是实数,因此使用了
round()
函数,但我得到了以下错误:
索引操作无效
访问非整数索引中的矩阵无效。如果确保对整数进行四舍五入,则不应出现该错误。如果您这样做,我建议您重新检查代码,如果问题仍然存在,请打开一个新的StackOverflow问题以解决该问题。您可以在评论中添加到问题的链接。再次感谢@Lior。