Random 1 image from folder and imread it

조회 수: 1 (최근 30일)
Brajan Nosal
Brajan Nosal 2019년 9월 25일
편집: KALYAN ACHARJYA 2019년 9월 25일
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