## Checking if Adjacent Values are in a Numpy Matrix

So, I am currently trying to figure out a more optimal solution to determine connected components in an image. Currently, I have an array with coordinates that have certain values. I want to create groups of these coordinates based on if they are touching. I am using a numpy array, and currently I have to check if each value (top left, top middle, top right, middle-left, middle right, bottom left, bottom middle, bottom right) is in that array. I do so via this code:

```
for x in range (0, groupCoords.shape[0]):
global tgroup
xCoord = groupCoords.item((x,0))
yCoord = groupCoords.item((x,1))
new = np.array([[xCoord, yCoord]])
if np.equal(Arr,[xCoord, yCoord+1]).all(1).any():
tgroup = np.append(tgroup, [[xCoord,yCoord+1]], axis=0)
new = np.append(new, [[xCoord,yCoord+1]], axis=0)
index = np.argwhere((Arr == [xCoord,yCoord+1]).all(1))
Arr = np.delete(Arr, (index), axis=0)
if np.equal(Arr,[xCoord, yCoord-1]).all(1).any():
tgroup = np.append(tgroup, [[xCoord, yCoord-1]],axis=0)
new = np.append(new, [[xCoord,yCoord-1]], axis=0)
index = np.argwhere((Arr == [xCoord,yCoord-1]).all(1))
Arr = np.delete(Arr, (index), axis=0)
if np.equal(Arr,[xCoord+1, yCoord]).all(1).any():
tgroup = np.append(tgroup, [[xCoord+1,yCoord]],axis=0)
new = np.append(new, [[xCoord+1,yCoord]], axis=0)
index = np.argwhere((Arr == [xCoord+1,yCoord]).all(1))
Arr = np.delete(Arr, (index), axis=0)
if np.equal(Arr,[xCoord+1, yCoord+1]).all(1).any():
tgroup = np.append(tgroup, [[xCoord+1,yCoord+1]],axis=0)
new = np.append(new, [[xCoord+1,yCoord+1]], axis=0)
index = np.argwhere((Arr == [xCoord+1,yCoord+1]).all(1))
Arr = np.delete(Arr, (index), axis=0)
if np.equal(Arr,[xCoord+1, yCoord-1]).all(1).any():
tgroup = np.append(tgroup, [[xCoord+1,yCoord-1]],axis=0)
new = np.append(new, [[xCoord+1,yCoord-1]], axis=0)
index = np.argwhere((Arr == [xCoord+1,yCoord-1]).all(1))
Arr = np.delete(Arr, (index), axis=0)
if np.equal(Arr,[xCoord-1, yCoord]).all(1).any():
tgroup = np.append(tgroup, [[xCoord-1,yCoord]],axis=0)
new = np.append(new, [[xCoord-1,yCoord]], axis=0)
index = np.argwhere((Arr == [xCoord-1,yCoord]).all(1))
Arr = np.delete(Arr, (index), axis=0)
if np.equal(Arr,[xCoord-1, yCoord+1]).all(1).any():
tgroup = np.append(tgroup, [[xCoord-1,yCoord+1]],axis=0)
new = np.append(new, [[xCoord-1,yCoord+1]], axis=0)
index = np.argwhere((Arr == [xCoord-1,yCoord+1]).all(1))
Arr = np.delete(Arr, (index), axis=0)
if np.equal(Arr,[xCoord-1, yCoord-1]).all(1).any():
tgroup = np.append(tgroup, [[xCoord-1,yCoord-1]],axis=0)
new = np.append(new, [[xCoord-1,yCoord-1]], axis=0)
index = np.argwhere((Arr == [xCoord-1,yCoord-1]).all(1))
Arr = np.delete(Arr, (index), axis=0)
```

However, this clearly takes a significant amount of time if the image is large. I had the idea to just create an boolean matrix with dimensions of the width and height of the image, and then assign the value "true" to values in the matrix which correspond to pixels in the image (the image is black-white).

I was wondering, is it possible to, instead of having to check each value like that, determine if their are elements labled "true" directly surrounding another "true" value?

This is what the input array would look like:

```
[
[0 0]
[0 1]
[0 2]
[10 2]
]
```

The output would look like

```
[
[0 0]
[0 1]
[0 2]
]
```

The function I am hoping to refine would check if the "true" values are touching, and create a 'network' of all values touching (it would keep running with the new values found).

Show source

## Answers to Checking if Adjacent Values are in a Numpy Matrix ( 2 )

Approach #1We could get the euclidean distances and see if any of the distances is within

`sqrt(2)`

, which would cover`up-down`

with`distance = 1`

and diagonal with`distance = sqrt(2)`

. This would give us a mask, which when indexed into the group coordinates array would give us the connected ones from it.Thus, an implementation using

`Scipy's cdist`

for getting those euclidean distances, would be -Sample run -

Approach #2Another way would be checking with absolute element-wise differences between the first columns of the two arrays and similarly for the second columns. Finally, get a joint mask from these two masks and checking any match and indexing again into group array for the filtered coordinates.

Thus, implementation for such a method would be -

Approach #3Another approach and probably would be better if you have

`Arr`

as a boolean array instead of as a 2-column coordinates array. The idea is to`dilate this boolean array`

of`Arr`

and then see which coordinates from`groupCoords`

would also lie in this dilated image. For the dilation, we would use a`3 x 3`

kernel of all ones to cover all of those neighborhood places. For detecting those common points, we need to draw an image with those`groupCoords`

.Thus, the code would be -

Sample run -

Depending on the ultimate goal of your code, you might find

`scipy.ndimage.label`

and its relatives useful.For example,

`label`

returns two values. The first is an array with the same size as the input array. Each distinct connected component in the input is assigned an integer value, starting at 1. The background is 0. The second return value is the number of components found.In the follwing,

`where(labeled_array = i)`

returns a tuple containing two arrays. These arrays are the row and column indices, resp., of the connected components:You can zip those together to convert them to lists of (row, col) pairs: