如何使用python扩展开放cv中的线段
我有一条线段的两个端点,我想延长这条线。 我通过这个网站找到了以下算法如何使用python扩展开放cv中的线段,python,c++,algorithm,python-2.7,opencv,Python,C++,Algorithm,Python 2.7,Opencv,我有一条线段的两个端点,我想延长这条线。 我通过这个网站找到了以下算法 lengthAB = sqrt((a.x - b.x)^2 + (a.y - b.y)^2) c.x = b.x + (b.x - a.x) / lengthAB * length; c.y = b.y + (b.y - a.y) / lengthAB * length; 但是当我在我的程序上实现它时,我不能得到输出。我需要int值,但是cx和cy是浮动的 !!
lengthAB = sqrt((a.x - b.x)^2 + (a.y - b.y)^2)
c.x = b.x + (b.x - a.x) / lengthAB * length;
c.y = b.y + (b.y - a.y) / lengthAB * length;
但是当我在我的程序上实现它时,我不能得到输出。我需要int值,但是cx和cy是浮动的
!![ax,y=200140,bx,y=232146][1]
import numpy as np
import cv2
import math
img = np.zeros((500,500,3), np.uint8)
lenab = math.sqrt((200-232)**2+(158-146)**2)
length = 100
cx = 232 + (232-200) / lenab*length
cy = 146 + (146-158) / lenab*length
cv2.line(img,(200,158),(cx,cy),(33,322,122),3)
cv2.imshow('Tha',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
我的o/p屏幕:
Traceback (most recent call last):
File "E:/Nan/inclined_line.py", line 9, in <module>
cv2.line(img,(200,158),(cx,cy),(33,322,122),3)
TypeError: integer argument expected, got float
从错误中,您正在将浮点值传递给cv2.line。将浮点转换为整数,如下所示:
cv2.line(img,(200,158),(int(math.floor(cx)),int(math.floor(cy))),(33,322,122),3)
赋值给点变量时强制转换为int
A=(100,100 )
B=(200,200 )
C=[200,200 ]
lenAB = math.sqrt(math.pow(A[0] - B[0], 2.0) + math.pow(A[1] - B[1], 2.0))
C[0] =int (B[0] + (B[0] - A[0]) / lenAB * 500)
C[1] = int(B[1] + (B[1] - A[1]) / lenAB * 500)
cv2.line(img,A,tuple(C),Colour_store.blue,1,1)
或者在你的代码中。元组转换我真的不记得是否需要,但如果它没有损坏
您能发布解决方案的完整代码吗?只是为了避免猜测必须修改原始代码的位置。
import numpy as np
import cv2
import math
img = np.zeros((500,500,3), np.uint8)
lenab = math.sqrt((200-232)**2+(158-146)**2)
length = 100
C=[200,200 ]
C[0] =int( 232 + (232-200) / lenab*length)
C[1] = int(146 + (146-158) / lenab*length)
cv2.line(img,(200,158),tuple(C),(33,322,122),3)
cv2.imshow('Tha',img)
cv2.waitKey(0)
cv2.destroyAllWindows()