Calculating distance between each pixel of rasterstack image?

I am new on R and I have an image of class "rasterstack" of only 3 colors: Red, Green and blue as illustrated:

I use this function: image = stack(img.red, img.green, img.blue)

I would like to find the distance between each pixel of one color to each others pixel of another color. By example the distance between each red pixel, to each blue pixel:

• Distance between red pixel 1 to blue pixel 1 ,pixel 2 ,pixel 3...
• Distance between red pixel 2 to blue pixel 1 ,pixel 2 ,pixel 3...
• etc

And create a table with all these distances. Then I could do some statistic as to know the distribution of the distance, the average min distance between red and blue, etc

I have no idea how to do it?

If you have a raster `red`, in this case 35 cells in a 5x7 raster:

``````> red
class       : RasterLayer
dimensions  : 5, 7, 35  (nrow, ncol, ncell)
resolution  : 0.1428571, 0.2  (x, y)
extent      : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
coord. ref. : NA
data source : in memory
names       : layer
values      : 0, 255  (min, max)
``````

You can get a vector of the values:

``````> red[]
[1]   0   0 255   0   0 255   0 255   0   0   0   0   0   0   0   0 255   0   0
[20]   0   0   0   0 255   0   0   0   0   0   0   0   0 255   0 255
``````

and you can convert all 35 cell centres to SpatialPoints:

``````> as(red,"SpatialPoints")
class       : SpatialPoints
features    : 35
extent      : 0.07142857, 0.9285714, 0.1, 0.9  (xmin, xmax, ymin, ymax)
coord. ref. : NA
``````

To then get only the red centres, subset the above:

``````> as(red,"SpatialPoints")[red[]==255]
class       : SpatialPoints
features    : 7
extent      : 0.07142857, 0.9285714, 0.1, 0.9  (xmin, xmax, ymin, ymax)
coord. ref. : NA
>
``````

So that's the coordinates of the 7 red pixels in my `red` layer.

Repeat for green and blue to get three SpatialPoints objects and then compute distance using `rgeos::gDistance`:

``````> redpts = as(red,"SpatialPoints")[red[]==255]
> greenpts = as(green,"SpatialPoints")[green[]==255]
> library(rgeos)
> gDistance(redpts, greenpts, byid=TRUE)
1         2         3         4         5         6         7
1  0.1428571 0.5714286 0.2457807 0.4247448 0.6167724 0.9075646 1.0724757
2  0.1428571 0.2857143 0.4729413 0.4247448 0.6167724 0.8126550 0.9075646
3  0.2857143 0.1428571 0.6054177 0.4915614 0.6645545 0.8000000 0.8494896
[ 12 rows for the 12 green pixels ]
``````

Its very useful when you are working on problems to create small example, both so that it is easy for you to see what is going on and also to share with others. For example, I created the 5x7 rasters with this code:

Make a 5x7 matrix with random 1,2,3 values:

``````rgb = matrix(sample(1:3,35,TRUE),5,7)
``````

Turn into a raster object:

``````rgb = raster(rgb)
``````

Extract the 1s, 2s, and 3s into three rasters:

``````red = (rgb==1)*255
green = (rgb==2)*255
blue = (rgb==3)*255
``````

The scaling by 255 is so that `plotRGB(stack(red,green,blue))` produces a truly coloured plot. Please try and make sample data in your questions to save us time answering them.