How to solve error A filename must be supplied in imwrite?

5 次查看(过去 30 天)
I have multiple rgb images in a folder and i want to convert them into gray scale and the save them into another folder
code
ImageFolder = "D:\sys2\New folder\8PSK\8PSK"
imds = imageDatastore('D:\sys2\New folder\8PSK\*.png') % Whatever...
numberOfImages = length(imds.Files)
for k = 1 : numberOfImages
% Get the input filename. It already has the folder prepended so we don't need to worry about that.
inputFileName = imds.Files{k};
fprintf('Checking %s\n', inputFileName);
rgbImage = imread(inputFileName);
grayImage = rgb2gray(rgbImage);
imshow(grayImage);
file_name = sprintf('Image%d.png', k)% name Image with a sequence of number, ex Image1.png , Image2.png....
fullFileName = fullfile(ImageFolder, file_name);
imgName = [ImageFolder,'\Image_',num2str(k),'.png'] ;
imwrite(grayImage,imgName) ;
axis('on', 'image');
drawnow;
pause(0.1); % Short delay so we can see the image a little bit.
end
msgbox('Done!');
error
Error using imwrite>parse_inputs (line 610)
A filename must be supplied.
Error in imwrite (line 440)
[data, map, filename, format, paramPairs] = parse_inputs(varargin{:});
Please help
  5 个评论
DGM
DGM 2021-10-14
You're giving imwrite a vector of multiple strings. Unless behavior has changed since the version I'm using, that's not a valid input.

请先登录,再进行评论。

采纳的回答

yanqi liu
yanqi liu 2021-10-14
编辑:yanqi liu 2021-10-14
sir,please check the follow code to get some information
ImageFolder = "D:\sys2\New folder\8PSK\8PSK"
imds = imageDatastore('D:\sys2\New folder\8PSK\*.png') % Whatever...
numberOfImages = length(imds.Files)
for k = 1 : numberOfImages
% Get the input filename. It already has the folder prepended so we don't need to worry about that.
inputFileName = imds.Files{k};
fprintf('Checking %s\n', inputFileName);
rgbImage = imread(inputFileName);
grayImage = rgb2gray(rgbImage);
imshow(grayImage);
file_name = sprintf('Image%d.png', k)% name Image with a sequence of number, ex Image1.png , Image2.png....
fullFileName = fullfile(ImageFolder, file_name);
imgName = fullfile(ImageFolder,['Image_',num2str(k),'.png']);
disp(imgName);
[fd,~,~] = fileparts(imgName);
if ~exist(fd)
mkdir(fd);
end
imwrite(grayImage,imgName) ;
axis('on', 'image');
drawnow;
pause(0.1); % Short delay so we can see the image a little bit.
end
msgbox('Done!');

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Read, Write, and Modify Image 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by