Here I reduced the size of an image. But I want to reduce the size of all the images of my folder. Images are : abc_20190304_0001 to abc_20190304_0095. How can I make the loop?
I=imread('abc_20190304_0001.png');
imshow abc_20190304_0001.png
L = imresize(I,.25,'bilinear');
figure, imshow(L);

 採用された回答

KSSV
KSSV 2021 年 9 月 8 日

0 投票

imgNames = dir('*.png') ; % give your exntenion of images in the folder
N = length(imgNames) ; % toal number of images
% loop for each image
for i = 1:N
thisImg = imgNames(i).name ;
I=imread(thisImg);
figure(1)
imshow(I) ;
L = imresize(I,.25,'bilinear');
figure(2)
imshow(L);
end

7 件のコメント

Joydeb Saha
Joydeb Saha 2021 年 9 月 8 日
Can I save the new reduced images by the same name or 1.png, 2.png.. etc in a separate folder?
KSSV
KSSV 2021 年 9 月 8 日
Yes, you can. Read about imwrite.
thepath = ''; % give path of the folder here
for i = 1:N
thisImg = imgNames(i).name ;
I=imread(thisImg);
figure(1)
imshow(I) ;
L = imresize(I,.25,'bilinear');
figure(2)
imshow(L);
% WRite image into a folder
name = [thepath,filesep,num2str(i),'.png'] ;
imwrite(L,name)
end
Joydeb Saha
Joydeb Saha 2021 年 9 月 8 日
Yes its working. The names of the files are like abc_20190304_0001,abc_20190304_0002, abc_20190304_0003 etc. Can we save the files in that way? Or in the 4digit format like.. 0001.png,0002.png etc
Joydeb Saha
Joydeb Saha 2021 年 9 月 8 日
actually when the code read the files, it reads as 0001,0002....0011..0095 etc, but when it saves the files it saves as 1.png,2.png....22.png etc.
so when i make the video of the files .. the continuation of the files gets scattered. so i want to save the files in 4digit format
KSSV
KSSV 2021 年 9 月 8 日
imgNames = dir('*.png') ; % give your exntenion of images in the folder
N = length(imgNames) ; % toal number of images
thepath = ''; % give path of the folder here
% loop for each image
for i = 1:N
thisImg = imgNames(i).name ;
I=imread(thisImg);
figure(1)
imshow(I) ;
L = imresize(I,.25,'bilinear');
figure(2)
imshow(L);
% WRite image into a folder
[filepath,name,ext] = fileparts(thisImg) ;
name = [thepath,filesep,name,'.png'] ;
imwrite(L,name)
end
Joydeb Saha
Joydeb Saha 2021 年 9 月 8 日
Can you just make me understand the last two lines...
consider my path name is : E:\MW\Channel
and I want to save it in the same folder as : abc_20190304_0001, abc_20190304_0002 ....abc_20190304_0095 etc
Image Analyst
Image Analyst 2021 年 9 月 8 日
You can use sprintf()
baseFileName = sprintf('%4.4d.png', i); % 0001.png for example.
fullOutputFileName = fullfile(outputFolder, baseFileName);
imwrite(L, fullOutputFileName);

サインインしてコメントする。

その他の回答 (1 件)

Joydeb Saha
Joydeb Saha 2021 年 9 月 8 日

0 投票

well the problem is solved

質問済み:

2021 年 9 月 8 日

コメント済み:

2021 年 9 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by