How to find number of neigbours of a pixel in binary image?

6 vues (au cours des 30 derniers jours)
Meshooo
Meshooo le 14 Sep 2016
Commenté : Meshooo le 16 Sep 2016
Dear all,
I have a binary image that contains many lines. I want to detect any pixel that has 2 neighboring pixels and in some cases 3 or 1 neighboring pixel (considering all possible 8-connectivity directions).
In other words, go to each pixel and check how many neighbors it has by checking the 8-connectivity. If it has 2 neighbors then select this pixel and finally will have a binary image that contains these pixels.
Any idea how to do that?
Meshoo

Réponse acceptée

Teja Muppirala
Teja Muppirala le 14 Sep 2016
Here are two ways to do it:
numberNeighboringPixels = 2; % Can also be 1 or 3
BW1 = rand(15,60) > 0.9; % Just a sample image
%%Using Lookup Table
lut = makelut(@(x)sum(x(:))==(numberNeighboringPixels+1),3);
BW2_LUT = bwlookup(BW1,lut) & BW1;
%%Using Convolution
BW2_CONV = ( conv2(single(BW1),ones(3),'same') == numberNeighboringPixels+1 ) & BW1;
%%Plot and show the results are the same
figure
subplot(3,1,1);
imagesc(BW1); title('Original Image');
subplot(3,1,2);
imagesc(BW1+BW2_LUT); title('Pixels with neighbors (using lookup table)');
subplot(3,1,3);
imagesc(BW1+BW2_CONV); title('Pixels with neighbors (using convolution)');
  1 commentaire
Meshooo
Meshooo le 16 Sep 2016
Thank you very much. I think there are some conditions that can not be solved with this approach and will try to note them later.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with Image Processing Toolbox dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by