Grayscale image to RGB

How to convert grayscale images to RGB, some images are already in RGB format (1:3 dimensions) but some images have a 1:4 dimension, so how can i convert those images to RGB?

in image data, RGB is often supplemented with a 4th channel, the Alpha channel for opacity, and this is known as an RGBA image, perhaps that's what you are dealing with ? if so you might simply ignore the alpha channel.

this is what im getting,

Error in abind::abind(train, along = 1) :
arg 'X72' has dims=100, 100, 1, 4; but need dims=X, 100, 1, 3

how do i fix this?

Hi @Knight_coder ,

In order to help folks here help you, it's important to share the code you ran, along with all the objects that are necessary for running that code. Sharing data can be tricky sometimes, so the best place to start is to share the code you ran by placing it in a codeblock here.

One way to do that is to:

  1. make sure you cursor is on a new line in your post, then
  2. click the </> icon in the post editor menu, and then
  3. follow the instructions that appear.

Now it works!! :)))

train <- list()
for(i in 1:length(pic_train)){
  train[[i]] <- load.image(pic_train[i])
  train[[i]] <- resize(train[[i]], w = 100, h = 100)
}

test <- list()
for(j in 1:length(pic_test)){
  test[[j]] <- load.image(pic_test[j])
  test[[j]] <- resize(test[[j]], w = 100, h = 100)
}

# Changing the dimensions
for(i in 1:length(train)){
  if(dim(train[[i]])[4] == 4){
    train[[i]] <- array(train[[i]][,,,1:3], dim = c(dim(train[[i]])[1:3], 3))
  }
}

for(j in 1:length(test)){
  if(dim(test[[j]])[4] == 4){
    test[[j]] <- array(test[[j]][,,,1:3], dim = c(dim(test[[j]])[1:3], 3))
  }
}

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.