MATLAB Answers

find 8 neighbours in image

35 views (last 30 days)
Sajid Rahim
Sajid Rahim on 21 Sep 2017
Answered: Mehadi Hasan on 22 Oct 2021
hi dears..... plz help me
how i find the 8 neighbours around the white pixels...
  3 Comments
Image Analyst
Image Analyst on 4 Jul 2020
Use imdilate() then and it with the original
neighbors = imdilate(bw, true(3)) & ~bw;

Sign in to comment.

Answers (2)

KSSV
KSSV on 21 Sep 2017
Read about knnsearch
I = imread('2.png') ;
I = rgb2gray(I) ;
[y,x] = find(I) ;
% 8 nearest neighbors
[ny,nx] = size(I) ;
[X,Y] = meshgrid(1:nx,1:ny) ;
% global indices
idx = knnsearch([X(:) Y(:)],[x,y],'k',8) ;
% sub indices
[i,j] = ind2sub(size(I),idx) ;
  2 Comments
Walter Roberson
Walter Roberson on 17 Oct 2017
This code does not answer the questions "Find them in what sense? What output are you looking for?"

Sign in to comment.


Mehadi Hasan
Mehadi Hasan on 22 Oct 2021
I = imread('2.png') ;
I = rgb2gray(I) ;
[y,x] = find(I) ;
% 8 nearest neighbors
[ny,nx] = size(I) ;
[X,Y] = meshgrid(1:1x,1:1y) ;
% global indices
idx = knnsearch([X(:1) Y(:1)],[x,y],'k',8) ;
% sub indices
[i,j] = ind2sub(size(I),idx) ;

Community Treasure Hunt

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

Start Hunting!

Translated by