# Extracting coordinates of segments in raster from TIFF image?

I'm working with the attached tiff file and I am trying to extract the xy coordinates of the green lines.

so far I saved the image as raster stack, and this specific image is from a layer of that raster

```file_path <- "MyCells.tiff" rs <- stack(file_path) plot(rs[[2]])```

however, I don't know how to extract specifically those green lines. I trid to follow these two questions:

but I couldn't find an answer to my question. google sends me to image manipulation files.

• There are no green lines. There are only green pixels. You can extract the green pixels but what then? Do you want to get the vector representation of the line network that seems to have generated the raster? After zooming in the data looks very noisy and conversion might be tricky. – Spacedman Apr 18 at 18:16
• yes, I agree with you, those are position of pixels. for the "what after" I need to calculate the distance of the yellow cells to the green segments. I think I found a solution tho, I edited my post to insert it. – TriRook Apr 18 at 19:20
• A histogram on your raster should show two peaks for the green and yellow features if you want to work out the cut-off point for discriminating – Spacedman Apr 18 at 19:50

so I played around a little creating an sp objet

`rs <- raster(file_path, band=2)`

`sp <- SpatialPoints(rs)`

```res <- extract(rs, sp, fun=max, df=T, cellnumbers=F)```

then kept those with high intensity

`res <- res[res\$All_Cells > 0.9,]`

`coord <- xyFromCell(rs, cell=res\$ID)`

`plot(coord, cex=0.05, col="darkgreen")`

this actually gave me the coordinates of the green segments !!

• That looks like a very complex way of doing it! But well done for the effort! (Too many people on here don't bother trying). – Spacedman Apr 18 at 19:48

Here's one approach that will give you the coordinates of each pixel that meets a criteria:

``````# Generate a raster to play with
library(raster)
library(RStoolbox)
mat = matrix(c(1,2,4,4),nrow=2,ncol=2)
ras=raster(mat)

# Use ggplot2::fortify() to turn it into a data.frame
ras.fort = ggplot2::fortify(ras)

# Select the raster values you're interested in (here, for example, 4)
mylocs = ras.fort[ras.fort\$layer==4,]
# `x` and `y` columns are your coordinates
``````
• fortify() throws the following error, saying it doesn't take class RasterLayer? `rs <- raster(file_path)` `ras.fort <- fortify(rs)` `Error: data must be a data frame, or other object coercible by fortify(), not an S4 object with class RasterLayer` – TriRook Apr 18 at 18:20
• Try loading the tif as a raster rather than a stack using `raster(file_path, band=2)` – JepsonNomad Apr 18 at 18:28
• nope, same error :-/ – TriRook Apr 18 at 19:03
• Oops - you need to load `library(RStoolbox)` – JepsonNomad Apr 18 at 19:17
• I think I was able to get it without using fortify but by creating an sp object and filtering the pixels with certain values :) – TriRook Apr 18 at 19:21

Simplest way is to extract coordinates for the set of cells over the threshold:

``````> r = raster(matrix(runif(50*50),50,50))
> coords = coordinates(r)[r[]>0.9,]
``````

This should show the raster with some dots on the greenest points:

``````> plot(r)
> points(coords)
``````