Home > other >  convert gray images to rgb images and replace it by imread() in matlab
convert gray images to rgb images and replace it by imread() in matlab

Time:10-31

I have A dataset including 15k gray images I need to train the Alexnet -pretrained model- by the dataset in matlab But Alexnet model accept RBG images with size [227 * 227 * 3] is it possible to convert a gray image to a RGB image?

actually I have tried this code

 im = imread(filename);
    im = imresize(im,[227,227]);
    RGB_Image = cat(3, im,im,im);
    imshow(filename);

But I found this Error:

Multi-plane image inputs must be RGB images of size MxNx3.

It turns out not to be a useful Idea!

CodePudding user response:

In the more broad aspect of color spaces it is not possible to convert grey images to rgb.

But you only want to represent a data structure of [227,227,1] to a data structure of [227,227,3].

in matlab you should do something like (didn't had a change to check it):

 im = imread(filename);
 rgb_im = repmat(im, [1, 1, 3]);
 imshow(rgb_im);

in python:

im = matplotlib.pyplot.imread(filename)
im.resize([227,227,1])
rgb_im = numpy.matlib.reshape(numpy.matlib.repmat(im, 1, 3),[227,227,3])
matplotlib.pyplot.imshow(rgb_im)
  • Related