1

I would like to import a JPEG image, find and extract 3 colors and change by 3 others to use as ID.

I can find the 3 colors I want by using Raster and I save the 3 images associated to these 3 colors. Then, I open these 3 JPEG images to combine in 1 using EBImage package. The image I got is not nice, the colors are indefinable.

enter image description here

I do not find the way to change these 3 colors in 3 others (by example I would like finally just red, green and blue to identify 3 items)

##################################################
#Plug it into a stack object using package raster

img <- list.files(folder) 
img.raster<-stack(img)
names(img.raster) <- c('r','g','b')

## Extract blue
img.blue<-img.raster
img.blue$blue<-0
img.blue$blue[img.blue$r<60 & img.blue$g<80 & img.blue$b<60] <- 1

## Extract grey
img.grey<-img.raster
img.grey$grey<-0
img.grey$grey[img.grey$r>85 & img.grey$g>82 & img.grey$b>77] <- 1

## Extract green
img.green<-img.raster
img.green$green<-0
img.green$green[img.green$r>65 & img.green$r<120
                  & img.green$g>90 & img.green$g<160
                  & img.green$b>45 & img.green$b<70] <- 1

#Export images using JPEG package
setwd("C:/Users/*******/export")
writeJPEG(as.array(img.grey), "grey.jpeg") 
writeJPEG(as.array(img.blue), "blue.jpeg") 
writeJPEG(as.array(img.green), "green.jpeg") 

#Open images using Using EBImage package
x <- readImage(file.choose())
y <- readImage(file.choose())
z <- readImage(file.choose())

#put 3 images in 1
total <- x + y + z

#Export images
writeJPEG(as.array(total), "total.jpeg") 

Is it the good way or can it be done easily?

  • I think where you are going wrong is with img.blue<-img.raster; img.blue$blue<-0 etc. That then means img.blue has four layers (r,g,b,blue) which means when you add them you aren't adding the new red, green, and blue components. Solution coming soon I think... – Spacedman Apr 28 at 16:48
1

First I'll make a test raster of all the colours from (0,0,0) to (255,255,255) in steps of 16:

> steps = seq(0,255, len=16)
> rgb = expand.grid(steps,steps,steps)

rgb is now all combinations of those 16 steps:

> dim(rgb)
[1] 4096    3

These are now to be packed into a 64x64 raster:

> sqrt(4096)
[1] 64

The red, green, and blue values:

> rr = raster(matrix(rgb[,1],64,64))
> gr = raster(matrix(rgb[,2],64,64))
> br = raster(matrix(rgb[,3],64,64))

Go into an RGB stack:

> img.raster = stack(rr,gr,br)

That's your source image. Plot it:

> plotRGB(img.raster)
> names(img.raster) = c("r","g","b")

enter image description here

Now I want to create three separate single layer rasters for each of the colours you want to extract. First the blue extraction.

> img.blue<-img.raster[[1]]   # **one layer**
> img.blue[] =0               # fill with zeroes
> img.blue[img.raster$r<60 & img.raster$g<80 & img.raster$b<60] <- 255
> plot(img.blue)

enter image description here

I'm not sure I believe this is the bluest pixels you are testing $b < 60 which isn't particularly blue. Correct that if you have to.

Note I'm testing values from img.raster and putting them in the single layer img.blue and I'm putting the value 255 (not 1).

Repeat that for img.grey and img.green. Here's all three:

enter image description here

Now we want to create an RGB raster where the "grey" pixels are red, the "blue" pixels are green, and the "green" pixels are blue.

> replace = stack(img.grey, img.blue, img.green)
> plotRGB(replace)

enter image description here

Which I think is what you want.

  • Perfect. Thanks a lot for your really detailed answer – Laurent Apr 29 at 0:22

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.