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.
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 ?