Presenting simultaneously 40 images, non overlapping.

1 次查看(过去 30 天)
Hi everyone,
I have a specific problem I hope you will help me with. I am running an experiment using eyetracking. In each trial (84 trials in total) I have to present 40 images, 100x100 pixels each, 20 on the right side of the screen, 20 on the left, simultaneously. The screen has a 1600x1200 pixels resolution.
I am trying to generate a random vector of coordinates of x and y which adheres the following conditions:
Each randomly generated x and y location coordinate pair must ensure the image presented is equidistant by 50 pixels from the next. This means that all the images displayed on screen, all 40 of them, must have at least 50 pixels distance from each other. So if on of the image location pairs is x = 1 and y = 1, the other x and y pairs must not have a value within x + 100(image pixel number) + 50 (distance between images) = 151, and y + 100(image pixel number) + 50 (distance between images) = 151. Each trial must have coordinate/location x and y numbers for all 40 images that adhere to these rules.
I am sorry this is so wordy. Can anyone please help?
All the best and thanks, Dritan
  1 个评论
utkarsh kumar singh
sir can you please how to write a code for generatin 6 block module in same plane for vlsi floorplanning. its urgent

请先登录,再进行评论。

采纳的回答

John BG
John BG 2017-6-20
编辑:John BG 2017-6-23
Hi Dritan Nikolla
zipped attachment split_coordinates.zip contains the following files: example1.m scatter_points_saturate.m sactter_points7.m and 2 free license files.
example1.m contains the following steps to get the relative coordinates of the split images, using scattered points meeting the minimum distance requirement.
1. simulating scattered points with minimum distance
W0=2000;H0=2000;R0=150;
[X,Y,Nmax,Dmatrix]=scatter_points_saturate(W0,H0,R0)
.
the attached functions scatter_points7.m and scatter_points_saturate.m generate random points within a given rectangle meeting constraint of minimum distance.
scatter_points7.m asks how many random points and minimum distance. scatter_points_saturate.m generates as many random points as possible meeting the minimum distance you choose.
please note that R0 is the minimum distance between any adjacent points.
2.
With some minor coordinate corrections we can easily obtain the coordinates as follows
X=X+W0/2+1;Y=Y+H0/2+1; % centring
X(1)=[];Y(1)=[]; % correction
A=zeros(W0,H0);
for k=1:1:numel(X)
A(X(k),Y(k))=1;
end
figure(2);plot(X,Y,'sr');axis([0 W0+1 0 H0+1]);
ax=gca
ax.DataAspectRatio=[W0/2 H0/2 1];
.
The coordinates within the original image are already in variables X and Y.
But these coordinates have the reference right in the centre of the square, I assume you want the coordinates reference on the bottom left corner.
3.
Now let's generate the tiles 100x100. Since the distance you require between any adjacent points has to be at least 151 (I am using 150) there may be some empty blocks
% generating tiles
L=100
N=W0/L
B=zeros(L,L,N);
for k=1:1:N
for s=1:1:N
B(:,:,k+s-1)=A((s-1)*L+[1:1:L],(k-1)*L+[1:1:L]);
end
end
bxn=0;byn=0; % relative coordinates
the 20x20 images with the same scattered points are contained in B.
B(:,:,1) is the bottom left corner image. B(:,:,20) is the top left corner image. B(:,:,380) is the bottom right corner image. B(:,:,400) is the top right corner image.
4.
Result
the absolute coordinates are contained in X and Y.
As mentioned above the 100x100 images are contained in B.
The relative coordinates of the scattered points in each 100x100 are also available in variables bxn and byn. If empty then there's no point.
5.
Please not that since I simulate the points with random generations each time the obtained coordinates are different.
6.
For this example note that the very 1st point in the 1st 100x100 image has coordinates [22 4] x=22 y=4.
Since it's the 1st image, such coordinates exactly match the coordinates of the 1st 100x100 image, let's check plotting the 1st 5 100x100 images
for d=1:1:5
[bx,by,vx]=find(B(:,:,d));
bxn=[bxn bx]; byn=[byn by];
fn=figure(2+d);
bx=bx(:);by=by(:);
for s=1:1:numel(bx)
plot(bx(s),by(s),'sr');
axis([-2 L+2 -2 L+2]);hold('all');
end
axn=gca;
axn.DataAspectRatio=[L L 1];
end
bxn(1)=[];byn(1)=[];
the 1st image
has 1 point, checking coordinates with the marker
for the 2nd image, 2nd block from bottom left and 1 block up, only the x coordinate coincides with the original image coordinates:
the marker on 2nd point in original big image
and the 2nd 100x100 image with marker
So, Dritan Nikolla
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG
  2 个评论
Dritan Nikolla
Dritan Nikolla 2017-6-20
Hi John,
Nothing kind from me here, the kindness is coming from you. I want to give you a massive thank you and would've given you several massive beers too. Thank you so much sir :).

请先登录,再进行评论。

更多回答(2 个)

Image Analyst
Image Analyst 2017-6-20
Try the montage() function.
  4 个评论
Dritan Nikolla
Dritan Nikolla 2017-6-20
Thanks. Those are not random. Its really quite a complicated question, although it looks deceptively simple.
Image Analyst
Image Analyst 2017-6-21
编辑:Image Analyst 2017-6-21
I have a demo that gives random locations with no point closer to any other point than a specified distance. Here it is:
% Creates a random pattern of points with no point closer to another than some specified distance.
clc;
x = rand(1, 10000);
y = rand(1, 10000);
minAllowableDistance = 0.05;
numberOfPoints = 300;
% Initialize first point.
keeperX = x(1);
keeperY = y(1);
% Try dropping down more points.
counter = 2;
for k = 2 : numberOfPoints
% Get a trial point.
thisX = x(k);
thisY = y(k);
% See how far is is away from existing keeper points.
distances = sqrt((thisX-keeperX).^2 + (thisY - keeperY).^2);
minDistance = min(distances);
if minDistance >= minAllowableDistance
keeperX(counter) = thisX;
keeperY(counter) = thisY;
counter = counter + 1;
end
end
plot(keeperX, keeperY, 'b*');
grid on;

请先登录,再进行评论。


Jan
Jan 2017-6-21
编辑:Jan 2017-6-21
As usual a rejection method to determine random areas without overlap:
function Pos = GetPositions
count = 0;
success = false;
while ~success
count = count + 1;
if count > 100 % Try it 100 times
error('Cannot find enough positions.');
end
Pos = GetPositionsCore;
success = ~any(isnan(Pos));
end
% Draw a test to show success (remove this from productive code):
figure;
axes('XLim', [1, 800], 'YLim', [1, 1200], 'NextPlot', 'add');
for k = 1:size(Pos, 1)
line([Pos(k,1), Pos(k,1), Pos(k,1)+100, Pos(k,1)+100, Pos(k,1)], ...
[Pos(k,2), Pos(k,2)+100, Pos(k,2)+100, Pos(k,2), Pos(k,2)]);
end
end
function Pos = GetPositionsCore
PicSize = 100;
n = 20; % Number of pictures
Area = [800, 1200]; % Available area (half of the screen)
Space = 50; % Empty pixels in X and Y direction separately
Pos = nan(n, 2); % Pre-allocate
for k = 1:n
found = false;
count = 0;
while ~found
X = randi([1, Area(1) - PicSize]);
Y = randi([1, Area(2) - PicSize]);
if all(abs(X - Pos(1:k-1, 1)) > Space + PicSize | ...
abs(Y - Pos(1:k-1, 2)) > Space + PicSize)
% Distance to all former found points is accepted:
found = true;
Pos(k, 1) = X;
Pos(k, 2) = Y;
end
count = count + 1; % Security limit
if count > 1e5
return;
end
end
end
end
20 images with 100x100 and 50 pixels distance is rather dense. Therefore about every 100th trial to run GetPositionCore fails. Therefore it is tried multiple times. If you want to find much more than 20 positions, this approach is not sufficient.
This is called for the right and the left side separately. Add 800 to the X-position of the images for the right side.
The code needs about 0.1 seconds.

Community Treasure Hunt

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

Start Hunting!

Translated by