Crop and save multiple images

조회 수: 4 (최근 30일)
mixaela pozoukidou
mixaela pozoukidou 2021년 5월 16일
댓글: Walter Roberson 2021년 8월 11일
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
Image Analyst
Image Analyst 2021년 8월 7일
편집: Image Analyst 2021년 8월 7일
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 2021년 8월 8일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2021년 5월 16일
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
mixaela pozoukidou
mixaela pozoukidou 2021년 8월 11일
did you have any opportunity to see the equations?
Walter Roberson
Walter Roberson 2021년 8월 11일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by