Identify neighboring pixels in matlab

Suppose A be,

     1 1 1 1 1 1
     1 2 2 3 3 3
     4 4 2 2 3 3
     4 4 2 2 2 3
     4 4 4 4 3 3
     5 5 5 5 5 5

I need to identify all numbers that are adjacent to a specific intensity value. For instance. intensities 1, 3 and 4 are adjacent to an intensity value of 2. What is the efficient way to do this in Matlab?

I can use the following,

   glcm = graycomatrix(A)

But if A has more intensity values, for example. 10,000 sulfur matrices will not be an effective method.

+4
source share
1 answer

You can create a mask with a 2D convolution, select values ​​according to this mask and then reduce them to unique values:

% // Data:
A = [ 1 1 1 1 1 1
      1 2 2 3 3 3
      4 4 2 2 3 3
      4 4 2 2 2 3
      4 4 4 4 3 3
      5 5 5 5 5 5 ];
value = 2;
adj = [0 1 0; 1 0 1; 0 1 0]; %// define adjacency. [1 1 1;1 0 1;1 1 1] to include diagonals

%// Let go
mask = conv2(double(A==value), adj, 'same')>0; %// pixels adjacent to those equal to `value`
result = unique(A(mask));

This example creates

result =
     1
     2
     3
     4

, 2, 2 .

+5

Source: https://habr.com/ru/post/1598698/


All Articles