how to automatically Crop images in a folder and saving them in a sub folder

I want to wright a code that automatically crop images with a known position, the images are stored in a folder with names 1,2,3,.....,i. it's required to save it with a prefix with the same name, I tried this code to cropping:
imageDir = fullfile('D:\3D_construction\im_set');
im_Set = imageSet(imageDir);
images = cell(1, im_Set.Count);
for i = 1:im_Set.Count
imagcrop{i} = imcrop(i,[1519 1179 760 440]);
end
I tried many ways to save the cropped images but it didn't work if any one can help me I'd be grateful. Thank you in advance

3 Comments

Hello Image Analyst, pls I adapted your code to crop a multiple files (jpg) to another folder but i am getting this error:
Invalid input for argument 1 (iri):
Value must be a character vector or string scalar.
The code is this:
imageDir = 'C:\Users\Asirajdin\Documents\MATLAB\Negative Images';
imgSets = imageSet(imageDir,'recursive');
pathName ='C:\Users\Asirajdin\Documents\MATLAB\Gray sc';
for k = 1 : imgSets.Count
theImage = imread(imgSets);
croppedImage = imcrop(theImage);
baseFileName = sprintf('Image #%d.jpg', k);
fullFileName = fullfile(pathName, baseFileName);
imwrite(croppedImage, fullFileName);
end
I will really appreciate if you can help out. Thanks in advance
Sirajdin, there is no iri in your code.
Please start a new question and there put the entire error message (ALL the red text) which would include the actual line number and actual line of code that is throwing the error. In the meantime, try this:
inputFolder = 'C:\Users\Asirajdin\Documents\MATLAB\Negative Images';
imgSets = imageSet(inputFolder, 'recursive')
outputFolder = fullfile(pwd, 'Output'); %'C:\Users\Asirajdin\Documents\MATLAB\Gray sc';
if ~isfolder(outputFolder)
mkdir(outputFolder);
end
keepGoing = true;
for folderIndex = 1 : length(imgSets)
thisFolder = imgSets(folderIndex).Description;
numImagesInThisFolder = imgSets(folderIndex).Count;
fprintf('Processing the %d images in folder "%s"\n', numImagesInThisFolder, thisFolder);
for k = 1 : numImagesInThisFolder
% Read in input image.
thisFullInputFileName = imgSets(folderIndex).ImageLocation{k};
theImage = imread(thisFullInputFileName);
imshow(theImage);
title(thisFullInputFileName);
drawnow;
% Crop the image.
promptMessage = sprintf('Do you want to crop and save this image,\nor Quit processing?');
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Yes, Continue', 'No, Quit', 'Yes, Continue');
if contains(buttonText, 'Quit', 'IgnoreCase', true)
keepGoing = false;
break;
end
croppedImage = imcrop(theImage);
% Create output image.
baseFileName = sprintf('Image #%d.jpg', k);
fullOutputFileName = fullfile(outputFolder, baseFileName);
fprintf('Copying #%d of %d: "%s"\n to "%s"\n', k, numImagesInThisFolder, thisFullInputFileName, fullOutputFileName);
imwrite(croppedImage, fullOutputFileName);
end
if ~keepGoing
break;
end
end
Thanks for the response, I have actually realised where the problem was and it has been resolved.

Sign in to comment.

 Accepted Answer

How about this:
imageDir = 'D:\3D_construction\im_set';
im_Set = imageSet(imageDir);
for k = 1 : im_Set.Count
theImage = imread(im_set........whatever....
croppedImage = imcrop(theImage,[1519, 1179, 760, 440]);
baseFileName = sprintf('Image #%d.png', k);
fullFileName = fullfile(imageDir, baseFileName);
imwrite(croppedImage, fullFileName);
end

7 Comments

it works thank you for your quick response
croppedImage() = imcrop(theImage,[1519, 1179, 760, 440]); this line is giving error kindly somebody cn help
Sure, if you give us the error message. Make sure that your image is at least 1159+760 columns wide, and 1179+440 rows tall. Make sure you replace "theImage" with the actual name of your image variable.
theImage = imread(im_set........whatever.... what am I supposed to add after "im_set"?
Presumably im_set is a string so all you should NEED to add is a right parenthesis and a semicolon, though there ore other options to imread() that might possibly be required depending on what kind of image file it is.
my image files are named IMG_1, IMG_2 ......IMG_1000, SO what should I name it here for im_set? It shows error about the imread file type

Sign in to comment.

More Answers (1)

this code gives error in cropped image. why?

3 Comments

Probably because the parameters used in George's code, [1519 1179 760 440], do not apply to your image, which is probably smaller than George's
now what should be written in place of points??
I don't see "points" mentioned in George's code. Let's see your code and your error message, both of which you forgot to attach.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!