Search code examples
rimageebimage

How to combine images of different color modes using EBImage?


I have around 400 images which I want to combine using abind function of EBImage R package. But it returns me the following error

Error in abind(..., along = along) : images have different color modes

An example data to reproduce the error

library(EBImage)
img1 <- readImage(system.file("images", "sample-color.png", package="EBImage"))
dim(img1)

img2 <- channel(img1, 'grey')

z = abind(img1, img2, along=2)

How to convert all the images to single band grayscale image so that abind can be run without any error?


Solution

  • The images all need to have the same color mode. If you convert your grayscale images to RGB, then you can use abind and won't lose any information in the process.

    library(EBImage)
    
    img1 <- readImage(system.file("images", "sample-color.png", package="EBImage"))
    dim(img1)
    #> [1] 768 512   3
    
    img2 <- channel(img1, 'grey')
    
    plot(img2)
    

    z = abind(img1, toRGB(img2), along=2)
    
    plot(z)
    

    Created on 2022-07-28 by the reprex package (v2.0.1)