简体   繁体   中英

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); and I find the coordinates' of 0 values.

% 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:

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.

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:

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. 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)];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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