Find the point lies outside the line python opencv
Answers
Answer:
If c and s are the angle's cosine and sine, the function must return True for a point (x, y) iff c*(x-x1) + s*(y-y1) >= 0 where (x1,y1) is any point on the red line.
Explanation:
Find the points beyond a line using Python
Ask Question
2
With reference to the question asked here Drawing a line given the angle and a point on the line, I have written a python function "find_normal" which returns the slope and y-intercept of the normal vector to a given point at given angle.
Now I have a list of points and I want to verify if these points are beyond that normal vector in the direction of given angle of not. e.g.
Angle = 180: True if point is to the left of the line, false otherwise
Angle = 0: True if point is to the right of the line, false otherwise
0 < Angle < 180: True if point is below the line, false otherwise
180 < Angle < 360: True if point is above the line, false otherwise
For this purpose I have written another function find_points_ahead that uses the results of find_normal to highlight all the points beyond the normal vector. As shown in the screen shot below.
enter image description here
But at some angles, some points that do not meet the criteria are also highlighted. For instance arrow is pointed downwards and only the points below the red line should have been highlighted. Instead some of points above the line were also highlighted. Can anyone help me here?
def find_normal(img,point,angle):
x1=point[0]
y1=point[1]
angle_rad=np.radians(angle)
c=-1*np.sin(angle_rad)
s=np.cos(angle_rad)
start_x=int(x1 - c * 640)
start_y=int(y1 - s * 640)
end_x=int(x1 + c * 640)
end_y=int(y1 + s * 640)
m=int((end_y-start_y)/(end_x-start_x))
y_intercept=end_y-(m*end_x)
cv2.line(img, (start_x,start_y), (end_x,end_y), [0, 0, 255], 3, cv2.LINE_AA)
return m,y_intercept
def find_points_ahead(img,base_point,angle,points):
m,c=find_normal(img,base_point,angle)
for pt in points:
pt_x=pt[0]
pt_y=pt[1]
result=m*pt_x-pt_y+c
if angle>0 and angle<180 and result<0:
cv2.circle(img,(pt_x,pt_y),3,[0,0,255],3,cv2.LINE_AA)
elif angle>180 and angle<360 and result>0:
cv2.circle(img,(pt_x,pt_y),3,[0,0,255],3,cv2.LINE_AA)