필터 지우기
필터 지우기

Display pairs of random images from a file

조회 수: 1 (최근 30일)
Ryan
Ryan 2012년 1월 25일
Hi,
I am new to MATLAB, so I don't know exactly how to get it to do what I want.
Right now, I have a script that just randomly displays an image file from a directory. I want to change this so it randomly displays two images from that directory ... however, I want to do so in a way that every possible pairing of images will be given exactly once. So, if the pictures are:
frog cat dog mouse house
I want to get:
frog - cat frog - dog frog - mouse frog - house cat - dog cat - mouse cat - house dog - mouse dog - house mouse - house
What is the easiest way to go about doing this? Originally I was going to simply create new picture files such that each one already contains the two images I want side by side. However, there are a large number of possible pairings so it will be too time consuming to do this.
How do I do this?
(Here is my script as it is, which I know doesn't do what I want it to do:
dfiles = d(~[d.isdir]);
genRandNum = randperm(length(dfiles));
filename = dfiles(genRandNum(i)).name;
imageName = fullfile('Desktop', 'SEMREL', 'Pictures', filename);
imshow(imageName, 'Parent');

답변 (1개)

David Young
David Young 2012년 1월 25일
Something based on this should work (not tested):
dfiles = d(~[d.isdir]);
N = length(dfiles);
genRandNum = randperm(N^2);
for i = 1:N^2;
[n1, n2] = ind2sub(N, genRandNum(i));
if n1 < n2
filename1 = dfiles(n1).name;
imageName1 = fullfile('Desktop', 'SEMREL', 'Pictures', filename1);
filename2 = dfiles(n2).name;
imageName2 = fullfile('Desktop', 'SEMREL', 'Pictures', filename2);
image1 = imread(imagename1);
image2 = imread(imagename2);
subplot(1,2,1); imshow(image1);
subplot(1,2,2); imshow(image2);
pause;
end
end

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by