finding neighbours around central rows and columns

1 回表示 (過去 30 日間)
sensation
sensation 2017 年 2 月 8 日
コメント済み: Sajid Rahim 2017 年 10 月 16 日
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.
Walter Roberson
Walter Roberson 2017 年 2 月 8 日
[r_of_index, c_of_index] = ind2sub(size(matrix_cell_id), index);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by