Read, Crop and then Save multiple images - from multiple folders

7 views (last 30 days)
Hi there, I have working code for reading, cropping and saving an image and now I hope to tackle cropping multiple images in a folder.
I used answers posted on this forum for the same purpose, yet sadly I did not have any luck in getting it working.
The working code I have for reading, cropping and saving just one image is as follows:
imshow('1-01-H PS.jpg');%load image
img = imread('1-01-H PS.jpg');
a = img(250:1565,1480:2370);
a = img(250:1565,1480:2370,:);
imshow(a)
imwrite(a,'Cropped 1-01-H PS.jpg');
imshow('Cropped 1-01-H PS.jpg');
I essentially want to loop this through each folder. Each folder contains 10-40 images, all to be cropped to the same dimensions above.
The advice I saw on this forum was as followed:
images = dir('*.jpg') ; % GEt all images of the folder
N = length(images) ; % number o fimages
I = imread(images(1).name) ; % crop one to get rect
[x, rect] = imcrop(I) ;
for i = 1:N
I = imread(images(i).name) ; % REad image
I = imcrop(I,rect) ; % crop image
fullFileName = fullfile(images(i).folder, images(i).name);
imwrite(I,fullFileName) ; % Save image
end
Unfortunately I could not get the two to marry together. I am using 2019a release. Any advice/suggestions would be much appreciated!

Answers (1)

Ameer Hamza
Ameer Hamza on 20 Apr 2020
Edited: Ameer Hamza on 21 Apr 2020
Unfortunately, you are overwriting the original files with the cropped images. I hope that you have backed-up all the images. Try this. I have changed the imwrite line.
images = dir('*.jpg') ; % GEt all images of the folder
N = length(images) ; % number o fimages
I = imread(images(1).name) ; % crop one to get rect
[x, rect] = imcrop(I) ;
for i = 1:N
I = imread(images(i).name) ; % REad image
I = imcrop(I,rect) ; % crop image
fullFileName = fullfile(images(i).folder, ['cropped' images(i).name]);
imwrite(I,fullFileName); % Save image
end
To loop through multiple directories:
If your folder structure is like this
filder1:
image1
image2
..
imageN
folder2:
image1
image2
..
imageN
..
..
folderN:
image1
image2
..
imageN
then you can change the first line of the code to
images = dir('*/*.jpg');
and if there are several levels of subfolders and you are at the top-level, then you can get the name of all the files in the current directory and subdirectory with
images = dir('**/*.jpg');
Apply the second options with care. It will recursively search the directories for jpg files.
  9 Comments
janefrances uba
janefrances uba on 23 Nov 2020
Read, imwrite multiple LBP images - from multiple folders....
please "Image Analyst" i need your help am stuck, i want to read and inwrite the LBP multiple image from multiple folder in a folder but am having errors..
below is my code..
function [lbparray] = LBPfun(dir, path)
filesInfor1 = {dir.name};
image_array = [];
var_matrix = [];
dim = 0;
im = {};
TrainingData = {};
outputFolder = fullfile('C:\Users\uba.janefrances\Desktop\Thesis\All_Faces\LBP_images\LBP Output Images');
if ~exist(outputFolder, 'dir')
mkdir(outputFolder);
end
for k=1:numel(filesInfor1)
% %read image one after the other
img = imread(strcat(path,'\',filesInfor1{k}))
img=imresize(img, [64 64]);
img = rgb2gray(img);
% figure();
% imshow(img);
imgg=LBP2(img);
lbpImage = (imcomplement(uint8(imgg)));
imshow( lbpImage);
cd('C:\Users\uba.janefrances\Desktop\Thesis\All_Faces\LBP_images\LBP Output Images\');
% Now do the writing
imwrite(uint8( lbpImage), 'lbpoutputimage.bmp','bmp');
end
janefrances uba
janefrances uba on 23 Nov 2020
AM WORKING ON SIX CLASSES OF IMAGE DATASET ANGER, DISGUST, FEAR,HAPPY,SAD AND SUPRISE AND i want to read and inwrite the LBP multiple image from multiple folder (6) in a folder (6) but am having errors..
below is my code..
function [lbparray] = LBPfun(dir, path)
filesInfor1 = {dir.name};
image_array = [];
var_matrix = [];
dim = 0;
im = {};
TrainingData = {};
outputFolder = fullfile('C:\Users\uba.janefrances\Desktop\Thesis\All_Faces\LBP_images\LBP Output Images');
if ~exist(outputFolder, 'dir')
mkdir(outputFolder);
end
for k=1:numel(filesInfor1)
% %read image one after the other
img = imread(strcat(path,'\',filesInfor1{k}))
img=imresize(img, [64 64]);
img = rgb2gray(img);
% figure();
% imshow(img);
imgg=LBP2(img);
lbpImage = (imcomplement(uint8(imgg)));
imshow( lbpImage);
cd('C:\Users\uba.janefrances\Desktop\Thesis\All_Faces\LBP_images\LBP Output Images\');
% Now do the writing
imwrite(uint8( lbpImage), 'lbpoutputimage.bmp','bmp');
end

Sign in to comment.

Categories

Find more on Search Path in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!