如何拉伸一条线以使用Python OpenCV拟合图像?
I have an image with the size of W * H
, I want to draw a line on this and the line should be automatically fit to the image,
for example if I draw it:
I want this:
How can I do this in Python and OpenCV? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
绘制扩展线(无坐标)
方法#1:只需在
- >
之前之后,此后有一个函数,当给定点
p1 p1
和P2
,Will 仅绘制扩展线。默认情况下,该行被图像边界剪辑。还有一个距离
参数可以确定从原始起点绘制的距离或直到线撞击图像的边框。如果需要新的(x1,y1)
和(x2,y2)
坐标,请参见第2节方法#2:完整的绘制坐标
如果您需要新的
(x1,y1)
和(x2,y2)
坐标,它会变得更加复杂,因为我们需要为每个计算每一个的新点可能的情况。可能的情况是水平,垂直,正面倾斜,负倾斜和确切的对角线。这是每种情况下都有新的两个坐标点的结果:白色是原始线,绿色是延伸线垂直
水平
斜率负斜率
负斜率
左角对角线
右角对角线
代码
Method #1: Just drawing the extended line (no coordinates)
Before
->
AfterHere's a function when given points
p1
andp2
, will only draw the extended line. By default, the line is clipped by the image boundaries. There is also adistance
parameter to determine how far to draw from the original starting point or until the line hits the border of the image. If you need the new(x1, y1)
and(x2, y2)
coordinates, see section #2Method #2: Full drawing with coordinates
If you need the new
(x1, y1)
and(x2, y2)
coordinates, it gets a little more complicated since we need to calculate the resulting new points for each possible case. The possible cases are horizontal, vertical, positively sloped, negatively sloped, and exact diagonals. Here's the result for each of the cases with the new two coordinate points: white is the original line and the green is the extended lineVertical
Horizontal
Positive slope
Negative slope
Left corner diagonal
Right corner diagonal
Code
我认为您的意思是以下内容:
您有2分 - 例如
p1
,p2
在图像中。而不是在P1和P2之间绘制一条线,而是需要包含这些点的线,但要沿图像的边缘划分。
您需要处理多种情况,具体取决于该线应达到哪个边缘。这取决于原始线段的位置和角度。
以下是如何处理其中一种情况的代码示例 - 线应到达顶部和右边缘。
您将需要对其进行修改以类似地处理其他情况,并确定应应用哪种情况。
注意:我知道我的代码无法完全解决问题。但是我认为这会引导您朝正确的方向发展。
I assumed you meant the following:
You have 2 points - e.g.
p1
,p2
within the image.Instead of drawing a line between p1 and p2 you want the line that contains these points but streches till the edge of the image.
You will need to handle several cases, depending on which edges the line should reach. This depends on the location and angle of the original line segment.
Below is a code example of how to handle one of the cases - where the line should reach the top and right edges.
You will need to modify it to handle the other cases similarly and also to determine which case should be applied.
Note: I am aware that my code does not solve the problem entirely. But I thought it will steer you in the right direction.
据推测,您的行以其两个端点而知道,让
p
和q
(您应该已经说过)。该线沿线的一般点具有由p + t(q -p)
在矢量上给出的坐标。现在,您必须将与图像边缘的交叉点进行,例如,上图(y = 0
)或pers(y = height-1
)侧。这给出了坐标的点(x,y)
,在哪里,您需要检查
0≤x<宽度
。重复这四个边(在适当的情况下切换x
和y
),最后您会找到两个点。这为您提供了主要的想法。在实践中,有一些拐角处的情况,例如垂直或水平线,或一条线或两个角的线。
Presumably, your line is known by its two endpoints, let
P
andQ
(you should have said that). A general point along this line has coordinates given vectorially byP + t (Q - P)
. Now you have to take the intersections with the image edges, for example with the upper (Y=0
) or lower (Y=Height-1
) side. This gives the point of coordinates(X, Y)
, whereand you need to check if
0 ≤ X < Width
. Repeat this for the four sides (switchingX
andY
where appropriate), and you will find two points in the end.This gives you the main idea. In practice, there are corner cases such as a vertical or horizontal line, or a line going though a corner or two.