Unable to convert grayscale images into rgb from a folder.

3 次查看(过去 30 天)
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');

采纳的回答

DGM
DGM 2021-9-21
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Import, Export, and Conversion 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by