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

조회 수: 4 (최근 30일)
john karli
john karli 2021년 10월 14일
댓글: john karli 2021년 10월 14일
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
john karli
john karli 2021년 10월 14일
MATLAB R2021a
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!');
  댓글 수: 3
yanqi liu
yanqi liu 2021년 10월 14일
sir,i think may be the character not satisfied

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by