finding neighbours around central rows and columns

조회 수: 2 (최근 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 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