1

I have a 2 part question, the first part was easy & is answered but I have one issue :

1) Load map in R and convert to Raster : Done

2) Tell R to only show a defined color band : NOT done

As you can see in my example map, it is a map of Namibia, and it is a single layer black and white map, with one blue dot. The RGB code of this blue dot is: Red == 14 , Green == 6, and Blue == 209

What I would like to do is tell R to Isolate this dot, and to leave out the rest, so in essence, when I plot the map, I have only the blue dot.

Here is my current code, and here is an example map.

rm(list=ls())

library(raster)

library(maps)

library(magick)

library(rgdal)

# working directory

setwd("Working here")
# load first image
Species_id<-1
Species_i <- image_read(paste("Species (", Species_id, ").png", sep=""))

Species_i_rotated<-image_rotate(Species_i,0)
image_write(Species_i_rotated, path = "Temporary.png", format = "png")

# create raster
Converted <- raster("Temporary.png", band=1)

#### This is where my problem needs solving
#This defines the threshold to classify where the dot on the image is present as 1
# While removing all the unwanted features

Converted<-Converted == 14 & Converted==6 & Converted==209
Converted[Converted==0]<-NA

plot(Converted) # I would like this to show only the blue dot.

enter image description here

1

Instead image_read, I used readPNG to solve your problem with less code:

# read image
img <- png::readPNG('~/path/to/image.png')

The output is an array with colors in 0 - 1 format.

# convert to raster
r <- brick(img)

raster::plotRGB(r, scale = 1)

enter image description here

# select blue color (R = 0%, G = 0%, B = 100%)    
plot(r[[1]] == 0 & r[[3]] == 1)

enter image description here

# Use it to mask
raster::plotRGB(mask(r, r[[1]] == 0 & r[[3]] == 1, maskvalue = 0), scale = 1)

enter image description here

  • Thanks! it works great! But.. Using the above map, I would like to associate that blue dot with a GPS point. What I do is upload the map, convert it to a raster, define a coordinate system, plot the raster and I overlay a map of Namibia using this function map(region="Namibia", add=T) From here, I generate a new map of Namibia, keeping that blue dot, and in the end, I will have the data of the GPS location. Is there a way, using my above code, of defining the threshold to classify where the dot on the image is present? (I assume this is with the RGB band values) – Andy Jan 9 '18 at 16:35
  • Use georeferencer plug-in from QGIS first – aldo_tapia Jan 10 '18 at 14:24
  • Hi @aldo_tapia: I did not get a proper chance to say thanks for your help and your advice. Looking forward to the day I know enough about this topic to help others myself – Andy Jan 12 '18 at 15:16
  • @Andy you're welcome. The best way to say 'thanks you' in this site is with upvotes or accepting answers. – aldo_tapia Jan 12 '18 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.