MATLAB Answers

0

How to crop multiple images?

Asked by Mohammad Shakhawat Sumon on 16 Sep 2019 at 2:37
Latest activity Answered by Mohammad Shakhawat Sumon on 20 Sep 2019 at 5:44
I need some help for multiple images cropping. I used this code but it showed error and said Computer Image System Toolbox used.
In here i use this code for cropping.
imageDir = 'D:\Masters Thesis\ssss';
ssss = imageSet(imageDir);
for k = 1 : ssss.Count
theImage = imread(ssss);
croppedImage = imcrop(theImage,[646, 1291, 481, 600]);
baseFileName = sprintf('Image #%d.png', k);
fullFileName = fullfile(imageDir, baseFileName);
imwrite(croppedImage, fullFileName);
end
can anyone help me to solve this prolem.
TIA

  0 Comments

Sign in to comment.

Tags

Products


Release

R2018b

2 Answers

Answer by Walter Roberson
on 16 Sep 2019 at 2:45
Edited by Walter Roberson
on 16 Sep 2019 at 2:49

Yes, imageSet requires the Computer Vision Toolbox.
imageDir = 'D:\Masters Thesis\ssss';
dinfo = dir(imageDir);
dinfo([dinfo.isdir]) = []; %remove folders
filenames = fullfile(imageDir, {dinfo.name});
numfiles = length(filenames);
for k = 1 : nulfiles
sss = filenames{k};
theImage = imread(ssss);
croppedImage = imcrop(theImage,[646, 1291, 481, 600]);
baseFileName = sprintf('Image #%d.png', k);
fullFileName = fullfile(imageDir, baseFileName);
imwrite(croppedImage, fullFileName);
end
Notice, though, that you are finding all files in the directory, and writing new files to the same directory. Suppose you stop part way (such as due to a bug in your code.) Then Image*.png files will have been created in the directory, and when you go to run the code again, they will be found in the directory -- and will be re-cropped and re-output, probably under a different name.
Nothing that you are doing associates a particular input file name with a particular output file name. Formally, MS Windows makes no promises about the order of files in a directory. You cannot tell, looking at an output file name, which input file it came from. You should consider changing that.

  4 Comments

Show 1 older comment
Walter Roberson
on 16 Sep 2019 at 8:10
You have files in the directory that are not images. It is not clear what you want the result of applying imread() to those non-images to be.
Alternatively, they might be images but matlab does not recognize the file extension . For example matlab does not recognize .orf images (Olympus cameras raw files)
I use this code which is given below. It works only for one picture but when i select the folder for all picture it's not working. So that i need more time for one by one picture.
Can you give me some suggestion for all images read it simultinously.
imageDir = 'D:\Masters Thesis\ssss';
dinfo = dir(imageDir);
dinfo([dinfo.isdir]) = []; %remove folders
filenames = fullfile(imageDir, {dinfo.name});
numfiles = length(filenames);
for k = 1 : numfiles
sssss = filenames{k};
theImage = imread('00027 22.jpg');
croppedImage = imcrop(theImage,[650,620, 481, 600]);
baseFileName = sprintf('Image #%d.png', k);
fullFileName = fullfile(imageDir, baseFileName);
imwrite(croppedImage, fullFileName);
end
And also i attached my folder position.
Walter Roberson
on 19 Sep 2019 at 18:45
Change
dinfo = dir(imageDir);
to
dinfo = dir( fullfile(imageDir, '*.jpg') );
Change
theImage = imread('00027 22.jpg');
to
theImage = imread(sssss);

Sign in to comment.


Answer by Mohammad Shakhawat Sumon on 20 Sep 2019 at 5:44

Thanks a lot.
Its working well.

  0 Comments

Sign in to comment.