Clear Filters
Clear Filters

Suggestions on how to count on only one side of a drawn line

3 views (last 30 days)
I have to count the number of green dots in a set of pictures. I have already written the code to count the dots. The image is divided by the drawn pink line and I only need to count the dots on the left side of the line. I am thinking of using a condition to count the cell only if it is on the left side, but I am not sure how to or how to find the coordinates of the line.

Answers (1)

Arun
Arun on 17 Oct 2023
Hey Marianne,
I understand that you want to determine the count of the points that are on the left of a given line. I will assume that you possess the coordinates of the points and the equation of the line.
One approach to solve this issue is to form a polygon on the left with the line as one of its edges and then count the point in that polygon. Here is a code that you can use for solving the issue:
% random data for sample that represent points data
%generate N random numbers in the interval (a,b) with the formula r = a + (b-a).*rand(N,1).
points= [(-2000 + (2000+2000)*rand(100,1)),(-2000 + (2000+2000)*rand(100,1))];
leftMost= min(points(:,1));%for left edge of the polygon
upperMost= max(points(:,2));%for top edge of the polygon
lowerMost= min(points(:,2));%for bottom edge of the polygon
% line asumming the equation to be y=3x + 5
lowerIntersection=(lowerMost-5.0)/3.0;
upperIntersection=(upperMost-5.0)/3.0;
%you can use the the below code if you have point coordinates for the line
% lowerIntersection=find(points(:,2)==lowerMost);
% upperIntersection=find(points(:,2)==upperMost);
%edge of polygon are
polyx=[leftMost,lowerIntersection,upperIntersection,leftMost]; %x-coordinates for the polygon edges
polyy=[lowerMost,lowerMost,upperMost,upperMost]; %y-coordi
nates for the polygon edges
%inpolygon function
[in,on]=inpolygon(points(:,1),points(:,2),polyx,polyy);
pointsLeft=numel(points(in,1)); %points on left or on the line
pointsOnLine=numel(points(on,1)); %points on the line
pointsRight=numel(~points(in,1)); %points on the right of the line
pointsLeft
pointsLeft = 52
pointsOnLine
pointsOnLine = 3
pointsRight
pointsRight = 52
In case you are unfamiliar with the process of obtaining the line equation, you can refer the following link for guidance: https://www.mathworks.com/matlabcentral/answers/295992-finding-the-equation-of-a-line-passing-2-points .
Please refer the shared link for more information regarding “inpolygon function: https://in.mathworks.com/help/matlab/ref/inpolygon.html
Hope this helps.

Categories

Find more on Elementary Polygons in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!