Skip to content
This repository has been archived by the owner on Feb 14, 2023. It is now read-only.

Is there any way to replace specific color in an image? #73

Open
dgemara opened this issue Oct 10, 2018 · 1 comment
Open

Is there any way to replace specific color in an image? #73

dgemara opened this issue Oct 10, 2018 · 1 comment

Comments

@dgemara
Copy link

dgemara commented Oct 10, 2018

For example, replacing all pinks with blacks. I was thinking of editing the actual dataframe (ie as.data.frame(im,wide="c")) and changing each instance of a pixel with the color I want. I know there are variations of this in the vignette, such as color.at <- and R(x) <- value, but I can't seem to see a specific one for changing a specific color.

@dahtah
Copy link
Owner

dahtah commented Oct 19, 2018

If you wish to replace all pixels with an exact colour value, try this:


##all pixels with colour c1 are given colour c2
replaceColour <- function(im,c1,c2)
{
    #Split into channels
    l <- imsplit(im,"c")
    #Find pixels that match over all channels
    sel <- map2_il(c1,l,function(a,b) b==a) %>% parall
    #Replace, reconstruct
    map2_il(c2,l,function(a,b) { b[sel]=a; b }) %>% imappend("c")
}

replaceColour(boats,color.at(boats,55,12),c(0,0,0)) %>% color.at(55,12)

If you need to select all pixels that are say, approximately red, have a look at

https://dahtah.github.io/imager/gimptools.html#selection-by-similarity

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants