简体   繁体   English

如何通过Matlab在矩阵中找到具有特殊价值的邻居?

[英]How can I find neighbors of spesific value in a matrix via Matlab?

I have a 256*256 matrix, some values are 0 (close the each other); 我有一个256 * 256的矩阵,有些值是0(彼此靠近); and I find the coordinates' of 0 values. 我找到了0值的坐标。

% finding missing rows and cols: xi, yi
[row,col]=find(~X);
MIS=[row,col];
MISWO=[MIS zeros(size(MIS,1),1) ];
MISWO
...
   168   224     0
   169   224     0
   170   224     0
   171   224     0
   172   224     0
   173   224     0
   174   224     0

Part of the X matrix: X矩阵的一部分:

0.57    0.58    0.00    0.55    0.54
0.55    0.54    0.00    0.55    0.52
0.56    0.55    0.00    0.55    0.53
0.56    0.55    0.00    0.53    0.52
0.56    0.00    0.00    0.53    0.54
0.55    0.00    0.00    0.53    0.52
0.55    0.00    0.00    0.55    0.51
0.55    0.00    0.00    0.53    0.51
0.56    0.00    0.00    0.51    0.53
0.55    0.00    0.00    0.51    0.51
0.55    0.00    0.00    0.51    0.49
0.55    0.00    0.00    0.52    0.49
0.56    0.00    0.53    0.51    0.48

My goal is finding the zero values 5-10 neighbors with coordinates and values. 我的目标是找到具有坐标和值的零值5-10邻居。

Can anybody help me? 有谁能够帮助我?

All the best 祝一切顺利

In order to find all nearest neighbors in a 5x5 box around each zero pixel we can use 2d convolution: 为了在每个零像素周围的5x5框中找到所有最近的邻居,我们可以使用2d卷积:

X1=conv2(double(~X),ones(5),'same')>0; 

This yields a binary matrix with 1 in the places of ALL the nearest neighbors positions around zero pixels. 这将在零像素附近的所有最近邻居位置中产生1的二进制矩阵。 finding the rows and cols for all the nearest neighbors without the zeros is just: 查找所有不带零的最近邻居的行和列只是:

[row2 col2]=find(X1.*X);

Then the matrix that you want is: 然后,您想要的矩阵是:

MIS2=[row2 col2 X(row2, col2)];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM