如何使用python在开放的cv中扩展线段
How to extend a line segment in open cv using python
我有一个线段的两个端点,我想扩展这条线。我通过这个网站找到了以下算法
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;
但是当我在我的程序上实现它时,我无法获得输出。我需要整数值,但 cx 和 cy 处于浮动状态。
![ a(x,y)=(200,140) , b(x,y)=(232,146) ][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
赋值给"点"变量时强制转换为
intA=(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)
或在您的代码中。元组转换我真的不记得这是否 #needed 但"如果它没有坏"
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()
从您的错误中,您将浮点值传递给 cv2.line。将浮点数转换为整数,如下所示:
cv2.line(img,(200,158),(int(math.floor(cx)),int(math.floor(cy))),(33,322,122),3)
相关文章:
- 是否可以通过C++扩展强制多个python进程共享同一内存
- static_assert在宏中,但也可以扩展到可以用作函数参数的东西
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 扩展光电二极管探测器以支持多个传感器
- 概念中的cv限定符需要表达式参数列表
- C++中的VLA,扩展名为std=C++11
- 将CHW格式的浮点向量转换为cv::Mat
- OpenGL 和 GLM 矩阵无法正确扩展,总是按比例缩小
- 错误的cv::face FacemarkLBF实例化
- 基于范围的 for 循环:迭代使用一个元素扩展的向量
- C++返回 Numpy 数组的 Python 扩展模块
- 扩展可变参数模板中的变量名称
- 如何检查给定的参数是否为 cv::noArray()?
- 开放 CV 中的动态内存分配,用于视频处理
- 扩展C++生成的代码的模板参数类型名称
- 我想通过带有C++和Python的插件创建一个可扩展的应用程序
- VSCode IntelliSense无法识别SDL框架的SDL_image扩展库
- 如何使用python在开放的cv中扩展线段
- 对多个扩展使用cv::glob