用python实现在地址为"C:\\Users\\11508\\Desktop\\2024-03\\X.bmp"的图像上画一个可移动的圆形,并能实时显示圆心的坐标
时间: 2024-04-15 18:23:45 浏览: 90
实现圆形图片展示
你可以使用Python的OpenCV库来实现在图像上画一个可移动的圆形,并实时显示圆心的坐标。下面是一个简单的示例代码:
```python
import cv2
def draw_circle(event, x, y, flags, param):
global center, drawing
if event == cv2.EVENT_LBUTTONDOWN:
center = (x, y)
drawing = True
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
if drawing:
img_copy = img.copy()
cv2.circle(img_copy, center, radius, (0, 255, 0), 2)
cv2.putText(img_copy, f"Center: {center}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.imshow("Image", img_copy)
img_path = "C:\\Users\\11508\\Desktop\\2024-03\\X.bmp"
img = cv2.imread(img_path)
drawing = False
center = (0, 0)
radius = 50
cv2.namedWindow("Image")
cv2.setMouseCallback("Image", draw_circle)
while True:
cv2.imshow("Image", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
```
运行这段代码后,会打开一个名为"Image"的窗口显示指定路径下的图像。你可以点击鼠标左键并拖动来绘制一个圆形,松开鼠标左键后圆形将固定在图像上。同时,窗口中会实时显示圆心的坐标。按下键盘上的"q"键可以退出程序。
阅读全文