Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

identify the adjacent pixels in matlab

Let's assume 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 the numbers which are adjacent to a particular intensity value. E.g. the intensities 1, 3, and 4 are adjacent to the intensity value 2. What is the effective way to do it in Matlab?

I can use the following,

   glcm = graycomatrix(A)

But if A have a larger number of intensity values e.g. 10000 graycomatrix will not be an efficient method.

like image 905
user570593 Avatar asked Jul 20 '15 13:07

user570593


People also ask

How do I check pixels in MATLAB?

To determine the values of one or more pixels in an image and return the values in a variable, use the impixel function. You can specify the pixels by passing their coordinates as input arguments or you can select the pixels interactively using a mouse.

How do I find the pixel coordinates of an image in MATLAB?

You can also obtain pixel value information from a figure with imshow by using the impixelinfo function. To save the pixel location and value information displayed, right-click a pixel in the image and choose the Copy pixel info option. Image Viewer copies the x- and y-coordinates and the pixel value to the clipboard.

How does MATLAB calculate pixel area?

total = bwarea( BW ) estimates the area of the objects in binary image BW . total is a scalar whose value corresponds roughly to the total number of on pixels in the image, but might not be exactly the same because different patterns of pixels are weighted differently.


1 Answers

You can build a mask with a 2D convolution, select the values according to that 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's go
mask = conv2(double(A==value), adj, 'same')>0; %// pixels adjacent to those equal to `value`
result = unique(A(mask));

In the example, this produces

result =
     1
     2
     3
     4

Note that the result includes 2 because some pixels with value 2 have adjacent pixels with that value.

like image 64
Luis Mendo Avatar answered Sep 27 '22 23:09

Luis Mendo