How to write multiple image from a for loop to a dir

3 次查看(过去 30 天)
Hi everyone, I'm trying write the output image of a "for loop" into a dir but instead writing all the output only four images is save in the directory. since it's a for loop, it's over-written the first four until the loop finishes. please help check the code, here it is below:
imds = imageDatastore ( 'D:\lfw' , ...
'IncludeSubfolders' , true, ...
'LabelSource' , 'foldernames' );
idx = randperm (numel (imds.Files), 5749);
j = 1;
count=0;
folder ='C:\Users\Asirajdin\Documents\T Chapters\Face Detection JKJJ\Class photos\Cropped';
% figure
for t = 1: 5749
img = readimage (imds, idx (t));
FaceDetect = vision.CascadeObjectDetector('FaceDetector-haar3.xml');
FaceDetect.MinSize=[32,32];
FaceDetect.MergeThreshold = 7;
BB = step (FaceDetect, img);
figure (2);
imshow (img);
for i = 1: size (BB, 1)
rectangle ( 'Position' , BB (i, :), 'LineWidth' , 3, 'LineStyle' , '-' , 'EdgeColor' , 'r' );
count = count+1;
end
hold off;
for i = 1: size (BB, 1)
J = imcrop (img, BB (i,:));
fileName = fullfile(folder,sprintf('Image %d.jpg',i));
imwrite(J,fileName) ;
figure (3);
subplot(11, 10,i);
imshow (J);
j = j + 1;
end
  1 个评论
asirajdin
asirajdin 2020-12-3
While trying to solve the problem, i used something like fid= fopen(filename) that i saw in a solution online, now the code is not running again. it is showing error:
Error using imwrite (line 548)
Unable to open file "C:\Users\Asirajdin\Documents\T Chapters\Face Detection JKJJ\Class photos\Cropped\_"
for writing. You might not have write permission.
when I opened the error msg, the matlab code is pointing to the line 548, the code in 548 is this:
547 if fid == -1
548 error(message('MATLAB:imagesci:imwrite:fileOpen', filename));
549 else
550 % File can be created. Get full filename.
551 filename = fopen(fid);
552 fclose(fid);
553 end
please someone should help me out. Initially, it was working, it just that the all images are not cropped to the folder, only four shows in the folder and it continuously overwritting them untill the running finishes. Now, it is not working again.

请先登录,再进行评论。

回答(2 个)

Paul Hoffrichter
Paul Hoffrichter 2020-12-4
I copied your method of writing the images to files and there were no problems.
Irgb = imread('Lena.jpg');
J = Irgb;
for i = 1:3
fileName = fullfile('.',sprintf('Image %d.jpg',i));
imwrite(J,fileName) ;
end
I verified as follows:
i1 = imread('Image 1.jpg');
i2 = imread('Image 2.jpg');
i3 = imread('Image 3.jpg');
imshow(i1)
imshow(i2)
imshow(i3)

Rik
Rik 2020-12-4
Assuming the missing end keyword goes at the end:
Your filename depends on only the inner loop, not the outer loop.
  2 个评论
Paul Hoffrichter
Paul Hoffrichter 2020-12-4
编辑:Paul Hoffrichter 2020-12-4
Was not concerned about that that since, as you know, MATLAB is kind enough to point that out immediately. Just a copy and paste mistake.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by