Random 1 image from folder and imread it

1 次查看(过去 30 天)
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));
"

回答(1 个)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019-9-25
编辑:KALYAN ACHARJYA 2019-9-25
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!

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by