Random 1 image from folder and imread it

1 view (last 30 days)
Brajan Nosal
Brajan Nosal on 25 Sep 2019
Edited: KALYAN ACHARJYA on 25 Sep 2019
Hello, i have a problem with random 1 image from folder and imread it. In my current folder i have folder name "Test3" with 40 image, i would like random one and put in "image". I tried with this:
"MyImages = dir(fullfile('Test3','*.jpg'));
% generate a random number between 1 and the number of images
RandomNumber = randi([1 size(MyImages,1)]);
% get the corresponding name of the image
MyRandomImage = MyImages(RandomNumber).name;
% display the image
image=(imread(MyRandomImage));"
I got error:
"Error using imread>get_full_filename (line 568)
File "dog.4774.jpg" does not exist.
Error in imread (line 377)
fullname = get_full_filename(filename);
Error in zaajecia1>pushbutton16_Callback (line 587)
image=(imread(MyRandomImage));
"

Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 25 Sep 2019
Edited: KALYAN ACHARJYA on 25 Sep 2019
Try?
%Save the folder of images in the current directory
path_directory='Test3'; % 'Folder name'
original_files=dir([path_directory '/*.jpg']);
filename=[path_directory '/' original_files(random(length(original_files))).name];
image_data=imread(filename);
Still the problem is not solved, then
% Save all images name in a sequence manner before doing the operation
% names for example im1,im2,im3...
Hope it helps!

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!