how do i read random jpeg frames from a folder?

조회 수: 1 (최근 30일)
sidra Rafique
sidra Rafique 2020년 1월 3일
답변: Image Analyst 2020년 1월 3일
i have a folder consisting of 2000 images .jpg, i want to made random selection of these images.
e.g;
i am using
mgs = dir(fullfile(Directory, '\*.jpg'));
for j=1:length(Imgs)
{
img = imread(fullfile(Directory,Imgs(j).name));
-----
---
----
}
but i want to select random imgaes in one go as i have same functionalities for all of them.
e.g;
i want to make range for frame 14,25,36,47,58,69,80 etc ( i have observed that these are every 11th frame which i am needing)
is there any way of making such selection.?
i am waiting for response.
thanku in advance
  댓글 수: 2
Stephen23
Stephen23 2020년 1월 3일
"is there any way of making such selection.?"
Sure, use indexing.
Also note that one of the main reasons for using fullfile is that it handles the file separator character automatically, so you should use it like this:
fullfile(Directory,'*.jpg')
Walter Roberson
Walter Roberson 2020년 1월 3일
for j = 14:11:length(Imgs)

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

채택된 답변

Bhaskar R
Bhaskar R 2020년 1월 3일
편집: Bhaskar R 2020년 1월 3일
% assuming your image file names are in sequence as 1.jpg,2.jpg,..., 2000.jpg
n_imges = 10; % number of random images you want to pick
Imgs = randi([1, 2000], 1, n_imges); % its the range between 1, 2000 images
( i have observed that these are every 11th frame which i am needing) -This means you need the 11th frame in the range 14 to 80 then
Imgs = 14:11:80;
Your code
mgs = dir(fullfile(Directory, '*.jpg'));
for j=1:length(Imgs)
{
img = imread(fullfile(Directory,mgs(Imgs(j)).name));
%-----
%---
%----
}

추가 답변 (1개)

Image Analyst
Image Analyst 2020년 1월 3일
Try randperm():
fileList = dir(fullfile(Directory, '\*.jpg'));
order = randperm(length(fileList));
for k = 1 : length(fileList)
index = order(k); % Get random index.
fullFileName = fullfile(Directory, fileList(index).name)
fprintf('Reading %s\n', fullFileName)
img = imread(fullFileName);
% More code .....
end

카테고리

Help CenterFile Exchange에서 Image Sequences and Batch Processing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by