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

205 ビュー (過去 30 日間)
Aliya Mohammed Salim
Aliya Mohammed Salim 2014 年 9 月 5 日
コメント済み: DGM 2022 年 2 月 5 日
I need to find out the neighbourhood pixel values, given a particular coordinate point in an image. the pixel point is user defined.
  6 件のコメント
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 件のコメント
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