Unable to convert grayscale images into rgb from a folder.
3 views (last 30 days)
Show older comments
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');
0 Comments
Accepted Answer
DGM
on 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.
More Answers (0)
See Also
Categories
Find more on Import, Export, and Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!