本文介绍了绘制一条线,给出角度和线上的一个点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 在我的图像中,我有一个三角形(代表一个箭头)。该箭头定义了在同一图像中进一步搜索时考虑的方向和区域。例如,如果我有一个三角形旋转30度w.r.t x轴,它的尖端位于图像(250,150)。我想找到并画出一条线,正常的三角形的尖端,如下图所示。 在上面的图片中,我将三角形中的蓝线角度绘制到法线上。另外,三角形的尖端是已知的,这是法线上唯一已知的点。 我的python函数代码如下。这段代码绘制了一条线,通过尖端,但不一定是NORMAL到蓝线。 def draw_intercepts(img,triangle) : tip = triangle [tip] x1 = tip [0] y1 = tip [1] 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) https://math.stackexchange.com/questions/2381119/how-to-find-slope-x-and-y-截取给定的角度到法线向量和a-poi 注意:我更新了推荐的代码: def draw_intercepts(img,triangle): tip = triangle [tip] x1 =尖端[0 ] y1 = tip [1] 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) 解决了这个问题。 现在当arrow_angle处于第一或第三象限时,线条被完美绘制,例如0 注意,使用的直线方程在拦截段不通用 - 不适用于水平线和垂直线。在截获方案0中,必须分别处理Pi / 2,Pi,3 * Pi / 2或-Pi / 2。 arrow_angle 用于蓝线: c = -Sin(arrow_angle)s = Cos (arrow_angle) 如果 arrow_angle 红线: c = Cos(arrow_angle)s = Sin(arrow_angle) 然后通过点划线 (x1-c * 4096,y1-s * 4096)和(x1 + c * 4096,y1 + s * 4096) (我使用与屏幕大小相当的任意大常量)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[0] y1=tip[1] 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:https://math.stackexchange.com/questions/2381119/how-to-find-slope-x-and-y-intercepts-given-angle-to-the-normal-vector-and-a-poiNote:I updated the code as recommended:def draw_intercepts(img,triangle): tip=triangle["tip"] x1=tip[0] y1=tip[1] 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) 这篇关于绘制一条线,给出角度和线上的一个点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 01:13