Cropping multiple Images and save them in a folder

4 visualizaciones (últimos 30 días)
Vaswati Biswas
Vaswati Biswas el 19 de Abr. de 2021
Comentada: Vaswati Biswas el 24 de Abr. de 2021
I have multiple images I want to crop them and save them in another folder. The code that I am using is given below.
myFolder = 'jpg'; % number of images
filePattern = fullfile(myFolder, '*.jpg') ;
theFiles = dir(filePattern);
numberOfFiles = length(theFiles); % GEt all images of the folder
I = imread(images(1).name) ; % crop one to get rect
[x, rect] = imcrop(I) ;
for i = 1:numberOfFiles
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
I = imread(fullFileName);% REad image
I = imcrop(I,rect) ; % crop image
fullFileName = fullfile(images(i).folder, ['cropped' images(i).name]);
imwrite(I,fullfilename) ; % Save image
end
But No folder is generated which contains the cropped images . Also I am getting the following error
Index exceeds the number of array elements (0).
Error in cropimages (line 5)
I = imread(images(1).name) ; % crop one to get rect
Can someone help me to solve this problem?

Respuestas (1)

Mahesh Taparia
Mahesh Taparia el 22 de Abr. de 2021
Hi
By looking at your code, it seems the variable 'images' is not defined. You are creating a 'theFiles' variable, which contains the information of image directory. So replace that line wih the below line:
I = imread(theFiles(1).name) ; % crop one to get rect
Hope it will work!
  3 comentarios
Mahesh Taparia
Mahesh Taparia el 23 de Abr. de 2021
Try this, it will work:
I = imread(fullfile(theFiles(1).folder,theFiles(1).name)) ; % crop one to get rect
Vaswati Biswas
Vaswati Biswas el 24 de Abr. de 2021
Still I am getting error in the code i.e.
Error in cropimages (line 13)
imwrite(I,fullfilename) ; % Save image

Iniciar sesión para comentar.

Categorías

Más información sobre Images 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