calculating the neighbour values

i have a matrix
A=[1 2 3
4 5 6
7 8 9 ]
please tell how to calculate the neighbour values of centre pixel

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 9월 30일
편집: Andrei Bobrov 2012년 10월 1일

0 개 추천

A = [ 542 605 341 615 928
471 550 839 1185 315
945 798 809 421 625
982 876 201 723 863
231 932 147 277 1100]; %initial matrix
out1 = cell(numel(A),1);
q = false(size(A));
for ii = 1:numel(A)
w = q;
w(ii) = true;
w1 = imdilate(w,[1 1 1;1 0 1;1 1 1]);
out1{ii} = A(w1);
end
% other variant
B = padarray(A,[1 1],nan);
s = size(B);
addm = bsxfun(@plus,(0:2)',(0:2)*s(1));
ad = addm(:)';
C = reshape(1:prod(s),s);
C = C(1:end-2,1:end-2);
out2 = B(bsxfun(@plus,C(:),ad([1:4,6:9])));
% the centre pixel values is 809 and has index:
i1 = 13;
out1{i1}
ans =
550
798
876
839
201
1185
421
723
>> out2(i1,:)
ans =
550 798 876 839 201 1185 421 723
>>

댓글 수: 2

A = randi(1234,5,5)
A =
542 605 341 615 928
471 550 839 1185 315
945 798 809 421 625
982 876 201 723 863
231 932 147 277 1100
In this the centre pixel values is 809,i need 8 neighbours aroubd it
550 839 1185 798 876 201 723 421
Andrei Bobrov
Andrei Bobrov 2012년 10월 1일
Hi Kash! See my answer.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Matt J
Matt J 2012년 9월 30일

1 개 추천

>> A(5)=[]
A =
1 4 7 2 8 3 6 9

댓글 수: 2

kash
kash 2012년 9월 30일
MAtt assume it may be large matrix 256x256,i just posted that for example
Jan
Jan 2012년 10월 1일
@kash: Please do not post a specific problem, when you want to solve a more general problem. This wastes the time of the ones, who want to help you.

댓글을 달려면 로그인하십시오.

Matt J
Matt J 2012년 9월 30일

0 개 추천

If you have the center pixel as a subscript index (m,n), then do
B=A((-1:1)+m,(-1:1)+n);
B(5)=[];
If you have the center pixel as a linear index, you can do as in the following example:
A =
0.3380 0.3042 0.2004 0.3071
0.8604 0.1598 0.5558 0.2813
0.7002 0.6425 0.9985 0.5409
0.2507 0.7799 0.2056 0.7081
jumps=bsxfun(@plus,[0;1;2],[0 1 2]*size(A,2));
jumps=jumps-jumps(5);
jumps(5)=[];
Now, Suppose I want the neighbourhood of A(6)=0.1598. Then I would do
>> A(jumps+6)
ans =
0.3380 0.8604 0.7002 0.3042 0.6425 0.2004 0.5558 0.9985

댓글 수: 5

kash
kash 2012년 9월 30일
i get error Index exceeds matrix dimensions.assume i have 257x257 matrix
Matt J
Matt J 2012년 9월 30일
Note that the method only works for pixels with 8 neighbours. You haven't said how you want to handle pixels on the edge of the matrix.
kash
kash 2012년 9월 30일
i need only 8 neighbours
Image Analyst
Image Analyst 2012년 9월 30일
Let me repeat Matt's statement, which you just seemed to ignore: "You haven't said how you want to handle pixels on the edge of the matrix." In fact, if you just think about this, write down numbers near the edge of your matrix so you can visualize it, I think you'd be able to figure it out yourself.
Matt J
Matt J 2012년 9월 30일
"i need only 8 neighbours"
You mean you think the error is triggered by a non-boundary pixel? Show us the larger code that you're using. Also, use DBSTOP to see the center pixel coordinates that are generating the error.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

질문:

2012년 9월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by