how to get image from specific folder and resize each image and save them is a new folder

조회 수: 18 (최근 30일)
i try below code, but it show error at 'thisimage' variable. whats the problem.
clc;
close all;
clear;
OutputFolder = 'D:\Datasets\handwritten-signatures\sample_Signature\edited'; % Set as needed [EDITED]
dinfo = dir('D:\Datasets\handwritten-signatures\sample_Signature\genuine\*.png');% image extension
for K = 1 : length(dinfo)
thisimage = dinfo(K).name;
Img = imread(thisimage);
Y = imshow(Img);
Gray = rgb2gray(Img);
i = imresize(Img, [227, 227], 'bilinear');
imwrite(i, fullfile(OutputFolder, thisimage)); % [EDITED]
end
  댓글 수: 2
Jan
Jan 2019년 5월 27일
"it show error at 'thisimage' variable" - please post the complete error message. For solving a problem it is important to know, what the problem is.
Awais Khan
Awais Khan 2019년 5월 27일
Error using imread>get_full_filename (line 516)
File "NFI-00101001.png" does not exist.
Error in imread (line 340)
fullname = get_full_filename(filename);
Error in resizing_dataset_images (line 9)
Img = imread(thisimage);
above error show as a result of above provide code. but in data set file with this name exist.

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

채택된 답변

Murugan C
Murugan C 2019년 5월 27일
편집: Rik 2019년 5월 27일
HI,
Before reading the image (imread), you shoud change directory, where the files are present, as like as below
clc;
close all;
clear;
OutputFolder = 'D:\Datasets\handwritten-signatures\sample_Signature\edited'; % Set as needed [EDITED]
dinfo = dir('D:\Datasets\handwritten-signatures\sample_Signature\genuine\*.png');% image extension
for K = 1 : length(dinfo)
thisimage = dinfo(K).name;
cd 'D:\Datasets\handwritten-signatures\sample_Signature\genuine\'
Img = imread(thisimage);
cd ..
Y = imshow(Img);
Gray = rgb2gray(Img);
i = imresize(Img, [227, 227], 'bilinear');
imwrite(i, fullfile(OutputFolder, thisimage)); % [EDITED]
end
  댓글 수: 2
Rik
Rik 2019년 5월 27일
You shouldn't be using cd in your functions. imread will accept a full path without requiring the current folder to be changed. Also, why are you using clc,close all,clear? Most of the times that is cargo cult programming. (and you should use the layout tools to make your code more readable)

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

추가 답변 (1개)

Rik
Rik 2019년 5월 27일
This is how you can provide the full path to imread:
for K = 1 : length(dinfo)
thisimage = dinfo(K).name;
Img = imread(fullfile(dinfo(K).folder,thisimage));
Y = imshow(Img);
Gray = rgb2gray(Img);
i = imresize(Img, [227, 227], 'bilinear');
imwrite(i, fullfile(OutputFolder, thisimage));
end

카테고리

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