create new images by resizing the images in a folder

조회 수: 2(최근 30일)
i have a folder of 7 images
i wanted to read each image one by one and create new images of different sizes, till i have 100 total images, including the initial 7 images
when i resize, the new images i create from a single image, should not have the same size, each image i create should have unique size
  댓글 수: 1
Elysi Cochin
Elysi Cochin 2021년 8월 30일
Below is the code i wrote
But its not complete
Checking if number of images has reached 100, checking if the new images generated has same image size
The above two points, when i add its going to infinite loop,
please can someone help me write the code in a simple way
% Generate 25 random numbers
a = 0.5;
b = 1;
rand('seed',0);
r = (b-a).*rand(25,1) + a;
for icnt = 3 : tnum
for i = 1 : rn
filename = cfilename(icnt,1).name;
imfiname = sprintf('Data/%s/%s', subFolders(fcnt).name, filename);
im = imread(imfiname);
if size(im,3) == 3
im = rgb2gray(im);
end
im = imresize(im,r(i));
sz(i,:) = size(im);
cnt = cnt + 1;
outfiname = sprintf('Data/%s/copy_%s_%s', num2str(cnt), filename);
imwrite(im, outfiname);
end
end

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

채택된 답변

Walter Roberson
Walter Roberson 2021년 8월 30일
please can someone help me write the code in a simple way
Oddly enough, yesterday someone had the same need, and I described to them how to write the code in a simple way. However, they then deleted the question, so I cannot refer you to what that other person had already posted and my response to it.
Fortunately, the logs were complete enough that I was able to rescue a partial copy of what I wrote then:
"If you have only 7 images and you need a total of 100, does that mean that you want each image to be copied 14 times, plus another 2 images to make the total 100? If so then use copyfile() instead of imread() / imwrite()"
That is, the proper way to make copies of images is not to imread() / imwrite(): the proper way is to copyfile() the image. You can copyfile() to a different file name if you need to.
  댓글 수: 6
Walter Roberson
Walter Roberson 2021년 8월 30일
numrep = [repmat(13,1,5), 14, 14];
numrep = numrep(randperm(7));
for K = 1 : 7
filename = cfilename(K).name;
this_image = imread(filename);
copy this_image to the destination
numrow = size(this_image,1);
numcol = size(this_image,2);
target_row_range = ceil(numrow/2):numrow-1;
target_rows = target_row_range(randperm(length(target_row_range), numrep(K)));
for R = 1 : numrep(K)
new_cols = ceil(target_rows(R)/numrow * numcol);
resized_image = imresize(this_image, [target_rows(R), new_cols]);
write out resized_image
end
end
No need to keep track of the size previously generated for a particular image because the algorithm generates the sizes in a way that is certain to have a different number of rows. It generates all of the integers from numrow/2 to numrow-1 and randomly selects the necessary number of them without repetition.
Likewise, there is no need to dynamically check as you go through whether this is an image that needs to be generated more often, and no need to test as you go whether you have reached the desired total number of images. Instead, it uses the information about the total number of images to generate a vector with the correct total and then scrambles that; the scrambled order is certain to still have the same total.

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

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by