Unable to convert grayscale images into rgb from a folder.

5 ビュー (過去 30 日間)
Warid Islam
Warid Islam 2021 年 9 月 20 日
コメント済み: Warid Islam 2021 年 9 月 21 日
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.
  1 件のコメント
Warid Islam
Warid Islam 2021 年 9 月 21 日
Hi @DGM,
It worked perfectly. Thank you.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by