Is there a way to find the neighbourhood pixels, given a cordinate point of the pixel?

조회 수: 33 (최근 30일)
I need to find out the neighbourhood pixel values, given a particular coordinate point in an image. the pixel point is user defined.
  댓글 수: 6
Image Analyst
Image Analyst 2016년 4월 22일
Sorry, I haven't worked at that task before, and I don't have any demos. I'd have to delve into it and learn it just as you would.
shikha mangal
shikha mangal 2016년 4월 22일
Ohh,well ok..i am still thankful to you for your help.If anyone find something related to the above problem please let me know it will be much helpful to me thanks.

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

채택된 답변

Image Analyst
Image Analyst 2014년 9월 5일
neighbors(1) = img(r-1,c-1); % Upper left. r = row, c = column.
neighbors(2) = img(r-1,c); % Upper middle. r = row, c = column.
neighbors(3) = img(r-1,c+1); % Upper right. r = row, c = column.
neighbors(4) = img(r,c-1); % left. r = row, c = column.
neighbors(5) = img(r,c+1); % right. r = row, c = column.
neighbors(6) = img(r+1,c+1); % Lowerleft. r = row, c = column.
neighbors(7) = img(r+1,c); % lower middle. r = row, c = column.
neighbors(8) = img(r+1,c-1); % Lower left. r = row, c = column.
Order them in whatever order is convenient for you.
  댓글 수: 7
Image Analyst
Image Analyst 2022년 2월 5일
The only other way I know is
neighbors = img(r-1:r+1, c-1:c+1); % Get 3x3 array.
neighbors(5) = []; % Delete the center pixel itself.
Of course if you just want to process the array somehow, unless you're doing something really unusual, you can just use imfilter(), nlfilter(), or blockproc(). Demoa attached. Using those you don't need to manually scan the image and get the neighbors yourself since it's handled by the function.
DGM
DGM 2022년 2월 5일
You should be able to do it any number of ways. This extracts the pixel and its neighbors:
inpict = randi([0 1],10);
pt = [5 5]; % row, col
imshow(inpict); hold on
plot(pt(2),pt(1),'yx','linewidth',2)
% find the sub-array
s = size(inpict);
idy = max(pt(1)-1,1):min(pt(1)+1,s(1));
idx = max(pt(2)-1,1):min(pt(2)+1,s(2));
nhood = inpict(idy,idx)
nhood = 3×3
0 0 1 0 1 1 1 1 0
Note that the output is truncated if the selected point is on the edge of the image.
It should be asked why this is being done. If this is part of a filter process, then edge handling wouldn't be done inside the loop. If the edge handling is done elsewhere and the neighborhood is guaranteed to be 3x3, then this can be simplified:
% another way
s = size(inpict);
idx = pt.' + [-1 0 1];
nhood = inpict(idx(1,:),idx(2,:))
nhood = 3×3
0 0 1 0 1 1 1 1 0
% or if only the neighbors are needed
nhood = nhood([1:4 6:9])
nhood = 1×8
0 0 1 0 1 1 1 0

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

추가 답변 (1개)

Yawar Rehman
Yawar Rehman 2014년 9월 5일
  댓글 수: 1
Aliya Mohammed Salim
Aliya Mohammed Salim 2014년 9월 5일
thanks yawar, but using the sub2ind, gives error, as in 'out of range subscripts', when I give a 2 x 3 matrix as input.

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

Community Treasure Hunt

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

Start Hunting!

Translated by