Facing problem in saving the images in folder

1 visualización (últimos 30 días)
saeeda saher
saeeda saher el 30 de En. de 2019
Editada: Balakrishnan Rajan el 30 de En. de 2019
I need some help in my code. I am trying to read the images from a folder and detect faces from then and save the cropped images in another folder. But while running the code it is not saving the cropped images into the folder properly. It's overwriting the images.
HER IS THE CODE
Folder = 'OUTPUT';
FileList = dir(fullfile(Folder, '*.jpg'));
for iFile = 1:numel(FileList)
File = fullfile(Folder, FileList(iFile).name);
I = imread(File);
% figure(1),imshow(I);
FaceDetect = vision.CascadeObjectDetector;
BB = step(FaceDetect,I);
% figure(2),imshow(I);
for i = 1:size(BB,1)
rectangle('Position',BB(i,:),'LineWidth',3,'LineStyle','-','EdgeColor','r');
end
for i = 1:size(BB,1)
J= imcrop(I,BB(i,:));
M = imresize(J,[48 48]);
imgray=rgb2gray(M);
fname = sprintf('%06d.jpg',i);
fpath = fullfile('Fdetected', fname);
imwrite(imgray,fpath);
end
end

Respuesta aceptada

Balakrishnan Rajan
Balakrishnan Rajan el 30 de En. de 2019
The 'fname' line of your code:
fname = sprintf('%06d.jpg', i);
should be in the outer most for loop. Then the 'i' will correspond to different input images. Presently, it seems to be another intermediate variable used for cropping the image.
  3 comentarios
saeeda saher
saeeda saher el 30 de En. de 2019
Editada: saeeda saher el 30 de En. de 2019
Thank You, Will you please guide me through editing my code? I tried but its not working
Balakrishnan Rajan
Balakrishnan Rajan el 30 de En. de 2019
Editada: Balakrishnan Rajan el 30 de En. de 2019
I think something along the lines of:
Folder = 'OUTPUT';
FileList = dir(fullfile(Folder, '*.jpg'));
for iFile = 1:numel(FileList)
File = fullfile(Folder, FileList(iFile).name);
I = imread(File);
% figure(1),imshow(I);
FaceDetect = vision.CascadeObjectDetector;
BB = step(FaceDetect,I);
% figure(2),imshow(I);
for i = 1:size(BB,1)
rectangle('Position',BB(i,:),'LineWidth',3,'LineStyle','-','EdgeColor','r');
end
for i = 1:size(BB,1)
J= imcrop(I,BB(i,:));
M = imresize(J,[48 48]);
imgray=rgb2gray(M);
end
fname = sprintf('%06d.jpg',iFile);
fpath = fullfile('Fdetected', fname);
imwrite(imgray,fpath);
end
Also run MATLAB as administrator. Create the folder 'Fdetected' beforehand. I think thats it.
Also remember to change it to 'iFile' when placing the lines outside as the iterator for the outermost loop is 'iFile' in your case.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Computer Vision Toolbox Supported Hardware en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by