Crop and save multiple images

3 views (last 30 days)
Hi, there i want to crop and save images 7x7 from a folder the fullname of the folder is c:\users\michaela\Desktop\homework\2 can anyone help me with the code in matlab .I want to take a different picture it's time and crop it and this i want it for 10.000 times .How can i achive that?
  5 Comments
Image Analyst
Image Analyst on 7 Aug 2021
Edited: Image Analyst on 7 Aug 2021
So, just to be clear, let's say you have a folder with 500 images, and each image is 1920x1080 pixels. So you are "not interested at portion of the image" which I take to mean that you want to extract out a 7x7 patch from somewhere, but don't care where. From some random location, I guess. So you want to extract a 7x7 chunk of the image from a random location in the 1920x1080 image. In the end you will have 500 7x7 "images" -- is that correct?
By the way, what good are 7x7 images? Nothing in them will be recognizable.
Walter Roberson
Walter Roberson on 8 Aug 2021
One of the reasons people take random small samples from an array is if they are doing Compressive Sensing -- though for that particular application you would usually want to know where you extracted the patch from.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 16 May 2021
projectdir = 'c:\users\michaela\Desktop\homework\2'
outdir = 'c:\users\michaela\Desktop\homework\2\cropped';
outsize = 7;
if ~exist(outdir, 'dir'); mkdir(outdir); end
ext = '.jpg'; %adjust as appropriate
dinfo = dir( fullfile( projectdir, ['*' ext]));
filenames = fullfile {dinfo.folder}, {dinfo.name} );
for K = 1 : length(filenames)
thisfile = filenames{K};
[~, fname, fext] = fileparts(thisfile);
outfile = fullfile( outdir, [fname fext]) );
thisimg = imread(thisfile);
[r, c, p] = size(thisimg);
rc = randi([1 r-outsize+1]);
cc = randi([1 c-outsize+1]);
cropped = thisimg(rc:rc+outsize-1, cc:cc+outsize-1, :);
imwrite(cropped, outfile);
end
This crops a random 7 x 7 section out of the image and saves the result into the directory indicated by outdir
We know that you want a random portion cropped out because you did not answer the question about which part of the image should be selected, so that tells us that you do not care which part as long as it is 7 x 7.
  11 Comments
mixaela pozoukidou
mixaela pozoukidou on 11 Aug 2021
did you have any opportunity to see the equations?
Walter Roberson
Walter Roberson on 11 Aug 2021
This does not appear to have anything to do with the Question about cropping and saving; you should start a new Question, and you should be more specific about what assistance you are asking for.
It is not likely that I myself will have the resources to program the code for you.
Historically, it has been unlikely that anyone will program up a paper for someone else, unless the paper was unusually simple.
Historically, it has been much more common for people to have time to help debug problems in code that someone has already written.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!