create new images by resizing the images in a folder

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 Comment

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

Sign in to comment.

 Accepted Answer

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 Comments

sir but with copyfile will that resize the image?
Good point, copyfile will not resize; the question that the other person had asked did not talk about resizing. However, once a single copy of the resized image exists, you can copyfile() that.
But anyhow, regardless of what that other person wanted, for your purposes, are all 14 copies (on average) of the 7 images intended to be the same? An definitely resized, rather than a random section cropped ? Your request could potentially make sense if you were doing random croppings, but why you would want 14 identical copies of each of 7 images is difficult to imagine.
I find it difficult in the following 2 points,
Checking if number of images has reached 100 including the initial images
and checking if the new images generated has same image size, if so i need to exclude it and resize with a new size, so that i get total 100 images in the folder
Ah, so different image sizes. Okay, you would not use copyfile() for that.
You need 100 output images, and you have 7 input images. Should five out of the seven input images be copied exactly fourteen times (total 5*14 = 70), and the other two input images be copied exactly 15 times (total 2*15 = 30), for a total of 100?
Or should the source image be chosen from a uniform random distribution, so that the number of copies only averages out to 14 or 15, but could vary, such as
histogram(randi(7, 1, 100), 7)
Initial number of images in folder = 7
Total images needed = 100
So new images needed to create
100 - 7 = 93
93/7 =13.2857
floor(13.2857) = 13
13*7 = 91
91 + 2 more needed for 93
so each image1 resized with 13, (such that image1 and the 13 new resized images have unique image sizes)
image2 resized with same 13 sizes, (such that image2 and the 13 new resized images have unique image sizes. The 13 new resized images of image2 can have or not have same size as new resized images of image1. Only thing is that, the image we take and the new images we create by resizing it must not have the same size)
Next, image3 resized with 13 sizes till image 7
Then resize any two images (or inorder - say first 2 image, with new size)
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.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!