# Creating a Grid for cooridnates

3 views (last 30 days)
Jason on 13 Oct 2023
Commented: Jason on 16 Oct 2023
Hello, I have an image where i have foudn the location of the spot centroid. I have the liist of all the coordimates.
What I want to do is create a set of vertical lines that is an average of the spots x positon, so in this case 4 vertical lines of which I have shown 1 below). and then do it for the horizontal lines.
In reality my images change in size and I can have upto 50x50 spots.
these are the actual coordiantes (x,y)
3.9988 76.5058
5.4914 203.4980
7.3364 330.4643
130.3843 77.1741
132.5535 204.2574
134.5383 331.2441
257.6364 77.8253
259.6390 205.0028
261.5130 331.9971
385.1154 78.8457
386.9360 205.8598
388.7871 332.6533
(Or Broken in to single column vectors)
x =
3.9988
5.4914
7.3364
130.3843
132.5535
134.5383
257.6364
259.6390
261.5130
385.1154
386.9360
388.7871
y =
76.5058
203.4980
330.4643
77.1741
204.2574
331.2441
77.8253
205.0028
331.9971
78.8457
205.8598
332.6533

Matt J on 15 Oct 2023
Edited: Matt J on 15 Oct 2023
and I also do pick up a few spurious spots that are'nt part of the grid
If outliers are present, I would use the approach below, which requries the download of this FEX contribution,
I don't really know the distribution of your outliers, so that may affect choices of certain parameters, as noted below in the code.
C=vertcat(regionprops(Image,'Centroid').Centroid);
x=C(:,1);
y=C(:,2);
D=pdist2([x,y],[x,y],'city','Smallest',2);
D(1,:)=[];
d=median(D(:));
Xedges=binEdges(x,d);
Yedges=binEdges(y,d);
[~,~,~,Gx,Gy]=histcounts2(x,y,Xedges,Yedges);
xl=splitapply(@median,x,Gx);
yl=splitapply(@median,y,Gy);
imshow(Image,[]);
xline(xl,'y--'); yline(yl,'y--')
function edges=binEdges(z,d)
Z=-d/2:round(max(z)+d/2);
T=sum(abs(Z-z(:))<=d/5,1);
thresh=0.5*max(T); %depends on outlier rate
[start,stop]=groupLims(groupTrue(T<=thresh),1);
edges=(Z(start)+Z(stop))/2;
end
##### 3 CommentsShow 1 older commentHide 1 older comment
Matt J on 15 Oct 2023
Edited: Matt J on 15 Oct 2023
So if I wanted to have a seperate median for x and y distances, is this possible?
If you know a fairly tight upper bound on the distance of the points from their grid lines (e.g., 10 pixels) then you can do as below,
dx=dmedian(x);
dy=dmedian(y);
function d=dmedian(u)
D=abs(u(:)-u(:)');
D(D<10)=inf;
d=median(min(D));
end
In the example below, Im trying to see if there is always a pattern to how D is calculated i.e. is it always top left and next point below?
No, the second row of D is just the distance from each (x,y) to its nearest neighbor, whichever that happens to be.
Jason on 16 Oct 2023
Thanks Matt, it all works beautifully!
Spurious spots removed!
Can even get the angle

Matt J on 13 Oct 2023
Edited: Matt J on 13 Oct 2023
x =[3.9988
5.4914
7.3364
130.3843
132.5535
134.5383
257.6364
259.6390
261.5130
385.1154
386.9360
388.7871];
y =[76.5058
203.4980
330.4643
77.1741
204.2574
331.2441
77.8253
205.0028
331.9971
78.8457
205.8598
332.6533];
scatter(x,y)
[~,~,~,Gx,Gy]=histcounts2(x,y,[4,3]);
xl=splitapply(@mean,x,Gx);
yl=splitapply(@mean,y,Gy);
xline(xl,'r--'); yline(yl,'r--')
Matt J on 14 Oct 2023
Image Analyst on 14 Oct 2023
I think you could also use kmeans() to count the number of rows and column.

### Categories

Find more on Read, Write, and Modify Image in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!