dahtah / imager

R package for image processing
GNU Lesser General Public License v3.0
187 stars 43 forks source link

how to load image with 3 spectrum value only #119

Closed sajis997 closed 4 years ago

sajis997 commented 4 years ago

I am loaded a image outside the package with imager library for R. Then I am trying to use the grayscale function from the library and I am getting the following error:

Error in grayscale(im) : Image should have three colour channels The loaded image has the following information :

dim(im) [1] 433 310 1 4 As you can see that spectrum has the value 4 which is not allowed for the grayscale operation to function.

How to select and initiate image object in R with selective spectrum channels ?

ShotaOchi commented 4 years ago

We can remove an unnecessary channel.

I guess your image has R channel, G channel, B channel, and alpha channel. If you don't need alpha channel, use rm.alpha function as shown below.

im <- load.image("your_image.png") # im has 4 channels (R, G, B, alpha)
im2 <- rm.alpha(im) # im2 has 3 channels (R, G, B)

If you need some specific combination of channels, use imappend function and channels function.

sajis997 commented 4 years ago

Thanks for the hint! Should it not be documented somewhere ?