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
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
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File 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!