finding neighbours around central rows and columns

2 次查看(过去 30 天)
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 个评论
Sajid Rahim
Sajid Rahim 2017-10-16
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')

请先登录,再进行评论。

采纳的回答

Jan
Jan 2017-2-8
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 个评论
sensation
sensation 2017-2-8
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by