0

I take photo with a black background but due to the light, it can have some reflect. In R I would like to change the background to black one (RGB = 0). I would like to select the color with RGB values lower than 80 and change to 0.

enter image description here

I use this code in R:

 library(raster)
    folder <- "C:/Users/PC/Pictures/"
    img <- list.files(folder) 
    img.raster<-stack(img)
    names(img.raster) <- c('r','g','b')
    color <- 80
    img.black<-img.raster[[1]]
    img.black[img.raster$r<color & img.raster$g<color & img.raster$b<color] <- 0

I rebuilt my image using stack

image = stack(img.black, img.black, img.black)

But by doing this I lose information as I have the same layer for R,G and B. If I tried:

image = stack(img.black, img.raster, img.raster)

by this way the dimension of the image is 7 !!

How to select a range of color and change it without modifying the dimension of the image and the other colors. Do I need to use raster or is there another solution ?

Laurent
  • 419
  • 4
  • 14
  • this thread might be helpful: https://stackoverflow.com/questions/15813101/controlling-legend-and-colors-for-raster-values-in-r – Tim Assal Apr 21 '20 at 16:03

1 Answers1

1

Here is how you could do that. Note that I update the color to 255 rather than 0 to be able to see the effect in this example. Also, you used the first layer of RGB as black. I assume you wanted the third.

Example data

library(raster)
img <- stack(system.file("external/rlogo.grd", package="raster"))
plotRGB(img)

Solution

color_threshold <- 80
update_value <- 255 # 0 in your case
black <- 3 
i <- all(img < color_threshold)
img[[black]] <- mask(img[[black]], i, maskvalue=1, updatevalue=update_value)
plotRGB(img)

Or perhaps this is what you are after --- update all cells to zero, not just the black channel:

img <- brick(system.file("external/rlogo.grd", package="raster"))
img2 <- mask(img, all(img < 80), maskvalue=1, updatevalue=0)
plotRGB(img2)
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • I used your code with my image and it can be ran but the area where I would like to change the following color (r=36, G=37 and B = 42) doesn't change to 0. On the contrary, area with the following color code (r=49, G=49 and B = 49) becomes yellow. – Laurent Apr 22 '20 at 01:34
  • I think I understand your question now, see the updated answer – Robert Hijmans Apr 22 '20 at 03:32
  • Perfect and in just 1 line. – Laurent Apr 22 '20 at 03:37