用opencv绘制一个圆,然后可以拖动滚动条来更改圆的半径
时间: 2023-05-22 08:04:32 浏览: 57
非常好的问题,您可以使用以下代码来实现:
```python
import cv2
import numpy as np
img = np.zeros((512, 512, 3), np.uint8)
cv2.namedWindow('image')
def nothing(x):
pass
cv2.createTrackbar('Radius', 'image', 0, 255, nothing)
while True:
radius = cv2.getTrackbarPos('Radius', 'image')
img = np.zeros((512, 512, 3), np.uint8)
cv2.circle(img, (256, 256), radius, (0, 0, 255), -1)
cv2.imshow('image', img)
if cv2.waitKey(1) & 0xFF == 27:
break
cv2.destroyAllWindows()
```
这个代码首先创建了一个空白的图像,然后使用 OpenCV 中的 `cv2.circle()` 函数来绘制一个圆,圆心坐标为 `(256, 256)`,颜色为红色,半径默认为 0。
然后,通过 `cv2.createTrackbar()` 函数创建了一个名为 "Radius" 的滑动条,并将当前的值设置为 0。接下来通过 `cv2.getTrackbarPos()` 函数获取滑动条的当前值,即半径。然后,根据半径使用 `cv2.circle()` 函数重新绘制圆,并将其显示在图像窗口中。
最后,使用 `cv2.waitKey()` 等待按下 ESC 键终止程序,并使用 `cv2.destroyAllWindows()` 关闭所有窗口。
相关推荐
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)