将灰度图像转换为rgb图像并在matlab中用imread()替换它

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

我有一个包含 15k 灰度图像的数据集

我需要通过 matlab 中的数据集训练 Alexnet -预训练模型。但是 Alexnet 接受大小为 [227 * 227 * 3]

的 RBG 图像

是否可以将灰度图像转换为 RGB 图像?

我试过这个代码

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

但我发现了这个错误:

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

在更广泛的色彩空间方面,无法将灰色图像转换为 rgb。但是你只想将[227,227,1]的数据结构表示为[227,227,3]的数据结构。

原OP代码如下。 imread returns 一个大小为 227,227,3 的数组。然后 imresize 将再次 return 一个 227,227,3 数组。最终 cat 将重新创建一个 227,227,9 数组。因此我们需要在imread之后转换数据结构。

在 matlab 中:

 im = imread(filename);
 im = rgb2gray(im);
 rgb_im = repmat(im, [1, 1, 3]); % or cat(3,im,im,im)
 imshow(rgb_im);

在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)