I am trying to get the number of 1s (black pixels) connected by more than 1 other black pixel in a binary matrix. I have a matrix...
set.seed(1234)
mat <- matrix(rbinom(30, 1, 0.5), nrow = 5)
which outputs a matrix...
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0 1 1 0 0 0
[2,] 1 1 1 1 0 0
[3,] 1 0 1 0 0 0
[4,] 1 0 1 1 0 0
I am now trying to figure out how to use this matrix to get the count (sum) of all black pixels that have more than 1 other black pixel connected such as...
[1,2] [1,3] [2,1] [2,2] [2,3] [3,1] [3,3] [4,3] = 8
Where 8 I would think would be the expected result. Is there a way to do this?
You can use
diff
in combination withapply
to get the number of neighbour pixels having a 1.Data: