Unable to convert grayscale images into rgb from a folder.

5 Ansichten (letzte 30 Tage)
Warid Islam
Warid Islam am 20 Sep. 2021
Kommentiert: Warid Islam am 21 Sep. 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');

Akzeptierte Antwort

DGM
DGM am 21 Sep. 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.

Weitere Antworten (0)

Kategorien

Mehr zu Convert Image Type finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by