How to get random image from specific folder in matlab ?

I have set of image in one folder. From that I want to get only one image from this folder. You can get one image by any feature or any logic or non logic..

 Accepted Answer

d='Yourfolder';
f=dir([d '\*.jpg']);
n=numel(f);
idx=randi(n);
im=imread(f(idx).name);
imshow(im)

2 Comments

I am using Matlab 2012 and got this type of error.. Thanks for your reply...
Error using randi First input must be a positive scalar integer value IMAX, or two integer values [IMIN IMAX] with IMIN less than or equal to IMAX.
Error in One_image (line 11) idx=randi(n);
This is because your folder is empty, I mean it doesn't contain jpg files

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!