Suggestions on how to count on only one side of a drawn line
1 次查看(过去 30 天)
显示 更早的评论
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1039410/image.png)
1 个评论
回答(1 个)
Arun
2023-10-17
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
pointsOnLine
pointsRight
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Elementary Polygons 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!