Displaying random image from array of file names

조회 수: 4 (최근 30일)
Alisha Arshad
Alisha Arshad 2018년 4월 7일
답변: Image Analyst 2018년 4월 8일
I am trying to figure out how to take an array of png image files and then have MATLAB randomly select one of the file names and then display it.
For example we have an array called red_shirts with 4 png files listed across the first row (4x1). We really have no idea how to go about solving this issue. Any help would be extremely appreciated!!!
We tried:
x=1:length(red_shirts)
y=rand(x)
n=imread(red_shirts(y).name);
imshow('n');
end

채택된 답변

Image Analyst
Image Analyst 2018년 4월 8일
Try this:
red_shirts = dir('*.png');
numberOfImages = length(red_shirts)
randomIndex = randi(numberOfImages)
randomImage = imread(red_shirts(randomIndex).name);
imshow(randomImage);

추가 답변 (1개)

David Fletcher
David Fletcher 2018년 4월 7일
편집: David Fletcher 2018년 4월 7일
I have no idea what structure you have your image file names stored in, so I can only give an example of something that does work (with the file names stored in a basic cell array):
red_shirts=[{'one.png'} {'two.png'} {'three.png'} {'four.png'}]
y=randi([1 length(red_shirts)]])
image=imread(red_shirts{y});
imshow(image)
  댓글 수: 1
Image Analyst
Image Analyst 2018년 4월 8일
Since red_shirts is a structure with a "name" field, it came from calling the dir() function.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by