finding neighbours around central rows and columns

3 vues (au cours des 30 derniers jours)
sensation
sensation le 8 Fév 2017
Commenté : Sajid Rahim le 16 Oct 2017
Hi,
I was wondering if some can help me regarding the row-column looping.
I have my row, col positions as [row,col]. e.g. array 300X2
I am trying to find their 8 neighbour values as:
neighbors_id= [matrix_cell_id(row, col),...
matrix_cell_id(row-1, col-1),...
matrix_cell_id(row-1, col),...
matrix_cell_id(row-1, col+1),...
matrix_cell_id(row, col-1),...
matrix_cell_id(row, col+1),...
matrix_cell_id(row+1, col-1),...
matrix_cell_id(row+1, col),...
matrix_cell_id(row+1, col+1)];
however when I run this code I get multiple values (matrix 300*2700) instead of 300row x 9 columns (corresponding to those neighbors) array.
Any clue is more than welcome,
Thanks a lot,
  2 commentaires
Sajid Rahim
Sajid Rahim le 16 Oct 2017
clear all;
[filename pathname]=uigetfile('*.jpg;*.png;*.tif;*.tiff;*.gif;*.bmp;');
inputimage=imread([pathname filename]);
I=inputimage;
% I = imread('5.png') ;
I = rgb2gray(I) ;
[y,x] = find(I) ;
figure imshow(I)
hold on
plot(x,y,'.b') % x
% y
% z=(y+x)/2
% plot(z,'.r')
a=x(1)
b=y(1)
plot(x(1),y(1),'*y')
plot(x(end),y(end),'*y')
% neighbors(1) = (a,b-1);
neighbors(1) = I(a,b-1);
plot(a,b-1,'*r')
neighbors(2) = I(a,b+1); plot(a,b+1,'*r')
neighbors(3) = I(a,b+2);
plot(a,b+2,'*r') neighbors(4) = I(a+1,b);
plot(a+1,b,'*r')
neighbors(5) = I(a+1,b+1);
plot(a+1,b+1,'*r')
neighbors(6) = I(a+2,b+2);
plot(a+2,b+2,'*r')
neighbors(7) = I(a+2,b);
plot(a+2,b,'*r')
neighbors(8) = I(a+2,b+1);
plot(a+2,b+1,'*r')
neighbors(9) = I(a+2,b+2);
plot(a+2,b+2,'*r')

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 8 Fév 2017
Try it with one element at first:
ThePoint = matrix_cell_id(row, col);
size(ThePoint)
What do you observe? This replies a matrix, which is bigger than you expect.
Use sub2ind to get the single elements.
index = sub2ind(size(matrix_cell_id), row, col);
Now the surrounding elemnts can be determined by simple additions to the linear index also.
  2 commentaires
sensation
sensation le 8 Fév 2017
Thanks for your answer Jan.
I tried this on my data but sub2ind rewrites ids, so I can not extract I think the right ids and areas in the second step. I have 670*9 matrix now but it does not take into account anymore my original rows and columns.
Walter Roberson
Walter Roberson le 8 Fév 2017
[r_of_index, c_of_index] = ind2sub(size(matrix_cell_id), index);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by