How to crop multiple images?

19 Ansichten (letzte 30 Tage)
Mohammad Shakhawat Sumon
Mohammad Shakhawat Sumon am 16 Sep. 2019
Verschoben: DGM am 22 Feb. 2023
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

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 16 Sep. 2019
Bearbeitet: Walter Roberson am 16 Sep. 2019
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.
  8 Kommentare
Image Analyst
Image Analyst am 15 Jul. 2022
@dinesh bharathi, did you post in the right place? This post involves reading in a sequence of images. What is "interlacing parameter"? No one in this thread mentions "interlacing parameter".
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
dinesh bharathi
dinesh bharathi am 18 Jul. 2022
@Image Analyst I just figured it out. I wanted to process images in batch and save them under Adam 7 interlaced format. I worked out imwrite(croppedImage, fullFileName,'png','InterlaceType','adam7'); in R2020b it was giving me weird response bby writing files differently. But when i tried the same imwrite(croppedImage, fullFileName,"png","InterlaceType","adam7"); in R2022a I was getting what i desired. Now it works fine.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Image Processing and Computer Vision finden Sie in Help Center und File Exchange

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by