opencv中根据中心点和边长获得四个角点的函数
时间: 2023-11-27 17:55:07 浏览: 52
在OpenCV中,可以使用cv2.boxPoints()函数来根据中心点和边长获取矩形的四个角点坐标。
具体用法如下:
```
import cv2
import numpy as np
# 中心点坐标
center = (100, 100)
# 矩形边长
length = 50
# 计算四个角点坐标
box = cv2.boxPoints(((center[0], center[1]), (length, length), 0))
box = np.int0(box)
# 显示结果
img = np.zeros((200, 200), dtype=np.uint8)
cv2.drawContours(img, [box], 0, 255, 2)
cv2.imshow('rect', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
其中,cv2.boxPoints()函数的参数是一个元组,包含中心点坐标、矩形的长和宽以及旋转角度。在这个例子中,我们没有旋转矩形,因此旋转角度为0。函数的返回值是一个四行两列的浮点型数组,每一行对应一个角点的x和y坐标。我们使用numpy的int0()函数将这个数组转换成整型,以便在图像上绘制矩形。
相关问题
opencv如何中心点和边长获得矩形轮廓点的坐标
使用OpenCV中的函数 `cv2.boxPoints()` 可以从矩形的中心点和边长计算出矩形的四个角点的坐标。
下面是一个示例代码,展示了如何从矩形的中心点和边长计算出矩形的四个角点的坐标:
```python
import cv2
# 矩形中心点坐标
center = (100, 100)
# 矩形边长
width = 50
height = 80
# 矩形旋转角度(可选)
angle = 30
# 创建旋转矩阵(如果需要旋转的话)
rotation_matrix = cv2.getRotationMatrix2D(center, angle, 1)
# 计算矩形四个角点坐标
rect_points = cv2.boxPoints((center, (width, height), angle))
# 应用旋转矩阵(如果需要旋转的话)
rect_points = cv2.transform(np.array([rect_points]), rotation_matrix)[0]
# 打印矩形四个角点坐标
for point in rect_points:
print(point)
```
这个代码会输出矩形四个角点的坐标,可以根据需要进行使用。
opencv如何根据矩形四个角点的坐标得到矩形轮廓的点坐标
可以使用OpenCV中的函数`cv2.boxPoints()`来根据矩形四个角点的坐标得到矩形轮廓的点坐标。
具体操作如下:
1. 将四个角点的坐标转换为一个矩形的中心点、大小和旋转角度,可以使用函数`cv2.minAreaRect()`来实现。
2. 使用函数`cv2.boxPoints()`,输入上一步得到的中心点、大小和旋转角度,即可得到矩形轮廓的点坐标。
以下是示例代码:
```python
import cv2
import numpy as np
# 矩形四个角点的坐标
pts = np.array([[50, 50], [150, 50], [150, 150], [50, 150]])
# 将四个角点的坐标转换为一个矩形的中心点、大小和旋转角度
rect = cv2.minAreaRect(pts)
# 使用函数cv2.boxPoints(),输入上一步得到的中心点、大小和旋转角度,即可得到矩形轮廓的点坐标
box = cv2.boxPoints(rect)
box = np.int0(box)
# 画出矩形轮廓
img = np.zeros((200, 200), dtype=np.uint8)
cv2.drawContours(img, [box], 0, 255, 2)
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
运行以上代码,可以得到一个画有矩形轮廓的黑色图像。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)