create new images by resizing the images in a folder

3 vues (au cours des 30 derniers jours)
Elysi Cochin
Elysi Cochin le 30 Août 2021
Commenté : Walter Roberson le 30 Août 2021
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 commentaire
Elysi Cochin
Elysi Cochin le 30 Août 2021
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

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 30 Août 2021
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 commentaires
Elysi Cochin
Elysi Cochin le 30 Août 2021
Modifié(e) : Elysi Cochin le 30 Août 2021
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)
Walter Roberson
Walter Roberson le 30 Août 2021
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Image Processing Toolbox dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by