How to run all the image with format jpg in folder? and save into folder?

조회 수: 2 (최근 30일)
Hello. Anyone know how to run a folder with thousand images instead of run one by one the image? and save into folder?
I=imread('11_285.jpg');
imshow(I)
magnificationFactor = 1.2;
J=imresize(I,magnificationFactor, 'bilinear');
imshowpair(I,J,method="montage")
imwrite(J,'11_285_rescale.jpg');

채택된 답변

Walter Roberson
Walter Roberson 2024년 4월 22일
dinfo = dir('*.jpg');
filenames = fullfile({dinfo.folder}, {dinfo.name});
nfiles = length(filenames);
magnificationFactor = 1.2;
for K = 1 : nfiles
thisfile = filenames{K};
[folder, basename, ext] = fileparts(thisfile);
outfile = fullfile(folder, basename + "_rescale" + ext);
I = imread(thisfile);
J = imresize(I, magnificationFactor, 'bilinear');
imwrite(J, outfile);
end
  댓글 수: 5
Dayangku Nur Faizah Pengiran Mohamad
ok thanks sir. I get it now. But now I want to crop my image. Here's my codes:
mypath = '/home/motmot/Downloads/All data/275/Ground truth/Rescale image/';
dinfo = dir(fullfile(mypath,'*_275.jpg'));
filenames = fullfile({dinfo.folder}, {dinfo.name});
nfiles = length(filenames);
for K = 1 : nfiles
thisfile = filenames{K};
[folder, basename, ext] = fileparts(thisfile);
outfile = fullfile(folder, basename + "_crop" + ext);
x_cent= 450;
y_cent= 260;
size_of_cropped_img=511;
centroide=[x_cent y_cent];
I3=imread(thisfile);
xmin=x_cent-size_of_cropped_img/2;
ymin=y_cent-size_of_cropped_img/2;
I4=imcrop(I3,[xmin ymin size_of_cropped_img size_of_cropped_img]);
imshow(I4)
imwrite(I4, outfile);
end
The image for outfile for cropping image did not save in the folder. How can I save the crop image?
Dayangku Nur Faizah Pengiran Mohamad
OK sir. I get the idea. I forgot to put 'rescale' at back the code
from this,
dinfo = dir(fullfile(mypath,'*_275.jpg'));
into this,
dinfo = dir(fullfile(mypath,'*_275_rescale.jpg'));

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Import, Export, and Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by