In my image I have a triangle (representing an arrow). This arrow defines the direction and area under consideration for further search in same image. For example if I have a triangle rotated at 30 degree w.r.t x-axis and it’s tip is located at (250,150) in the image. I would want to find and draw a line normal the tip of triangle, as shown in the image below.
In the image above, I have the angle of blue line in the triangle, to which the normal is to be drawn. Also the tip of the triangle is known which is the only point known on the normal line.
My code for the python function is given below. This code draws a line, passing through the tip but not necessarily NORMAL to the blue line.
def draw_intercepts(img,triangle): tip=triangle["tip"] x1=tip y1=tip arrow_angle=triangle["arrow_angle"] y_intercept=int(y1+((1/np.tan(arrow_angle))*x1)) x_intercept=int(x1+(np.tan(arrow_angle)*y1)) cv2.line(img,(x_intercept,0),(0,y_intercept),[0,0,255],3,cv2.LINE_AA)
This code is written by following an answer to this post:
I updated the code as recommended:
def draw_intercepts(img,triangle): tip=triangle["tip"] x1=tip y1=tip arrow_angle=triangle["arrow_angle"] arrow_angle_rad=np.radians(arrow_angle) y_intercept=int(y1+((1/np.tan(arrow_angle_rad))*x1)) x_intercept=int(x1+(np.tan(arrow_angle_rad)*y1)) cv2.line(img, (x_intercept, 0), (0, y_intercept), [0, 0, 255], 3, cv2.LINE_AA)
This resolved the issue.
Now the line is drawn perfectly when the arrow_angle is in 1st or 3rd quadrant e.g. 0 < arrow_angle < 90 and 180 < arrow_angle < 270 but in 2nd and 4th quadrant ( 90 < arrow_angle < 180 and 270 < arrow_angle < 360) line angle is not drawn as correct angle or position. Even I don’t know it line is drawn somewhere because it is not visible in image.
Note that used line equation “in intercept segments” is not universal – does not work for horizontal and vertical lines. With intercept approach cases 0, Pi/2, Pi, 3*Pi/2 or -Pi/2 must be treated separately.
If you arrow_angle is for blue line:
c = -Sin(arrow_angle) s = Cos(arrow_angle)
If you arrow_angle is for red line:
c = Cos(arrow_angle) s = Sin(arrow_angle)
Then draw line through points
(x1 - c * 4096, y1 - s * 4096) and (x1 + c * 4096, y1 + s * 4096)
(I used arbitrary large constant comparable with screen size)