Display pairs of random images from a file
4 views (last 30 days)
Show older comments
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');
0 Comments
Answers (1)
David Young
on 25 Jan 2012
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
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!