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

5 visualizaciones (últimos 30 días)
john karli
john karli el 14 de Oct. de 2021
Comentada: john karli el 14 de Oct. de 2021
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 comentarios
DGM
DGM el 14 de Oct. de 2021
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.

Iniciar sesión para comentar.

Respuesta aceptada

yanqi liu
yanqi liu el 14 de Oct. de 2021
Editada: yanqi liu el 14 de Oct. de 2021
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 comentarios
yanqi liu
yanqi liu el 14 de Oct. de 2021
sir,i think may be the character not satisfied

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Read, Write, and Modify Image en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by