Unable to convert grayscale images into rgb from a folder.

6 visualizaciones (últimos 30 días)
Warid Islam
Warid Islam el 20 de Sept. de 2021
Comentada: Warid Islam el 21 de Sept. de 2021
Hi,
I want to convert grayscale images from a folder into rgb. I would then save the resulting rgb images into a separate folder. Below are my code:
files_loc = 'D:\regionGrowing_MLT\png_orig_imgs1\Training\Benign';
filenames = dir(fullfile(files_loc,'*.png'));
fnames = {filenames.name};
img_name = fnames{1};
img_file = fullfile(files_loc,img_name);
save_dir = 'D:\regionGrowing_MLT\png_orig_imgs1\Training\Benign\trainColorBenign';
for iter = 1:length(fnames)
img_name = fnames{iter};
png_name =strsplit(img_name,'.');
png_name = png_name{1}
new_png_path = fullfile(save_dir,sprintf('%s.png',png_name));
img_file = fullfile(files_loc,img_name);
rgbImage = cat(3, img_file, img_file, img_file);
imwrite(rgbImage(iter),new_png_path,'mode','loseless');
end
However, I get the following error message:
Error using imwrite>parse_inputs (line 615)
First argument must be image data specified by a numeric matrix.
Error in imwrite (line 440)
[data, map, filename, format, paramPairs] = parse_inputs(varargin{:});
Error in colorconv (line 17)
imwrite(rgbImage(iter),new_png_path,'mode','loseless');

Respuesta aceptada

DGM
DGM el 21 de Sept. de 2021
imwrite() needs numeric/logical data as its primary input. You're feeding it a 3D char array of file paths. Read the file, concatenate, write. The fix might be something like the following, though I didn't set up a sandbox to test this.
% ...
imgdata = imread(img_file);
rgbImage = repmat(imgdata,[1 1 3]);
imwrite(rgbImage,new_png_path,'mode','loseless');
% ...
There might be other ways to do this quicker, but something like this should work.

Más respuestas (0)

Categorías

Más información sobre Convert Image Type en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by