python cv2画点
时间: 2023-10-03 11:11:26 浏览: 159
要使用Python的cv2库在图像上画点,可以按照以下步骤进行操作:
1. 导入cv2模块和numpy模块:
import cv2
import numpy as np
2. 创建一个空白图像:
image = np.zeros((256, 256, 3), np.uint8)
3. 定义一个回调函数onmouse_pick_points,用于处理鼠标事件,在该函数中绘制点和输出坐标:
def onmouse_pick_points(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
print('x = %d, y = %d' % (x, y))
cv2.drawMarker(param, (x, y), (0, 255, 0))
4. 创建一个窗口并设置鼠标回调函数:
WIN_NAME = 'pick_points'
cv2.namedWindow(WIN_NAME, 0)
cv2.setMouseCallback(WIN_NAME, onmouse_pick_points, image)
5. 进入主循环,显示图像并等待键盘输入,按下ESC键退出:
while True:
cv2.imshow(WIN_NAME, image)
key = cv2.waitKey(30)
if key == 27: # ESC
break
6. 关闭窗口:
cv2.destroyAllWindows()
这样就可以通过鼠标点击,在图像上绘制点,并输出点的坐标。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [python使用opencv(cv2)在图像上绘制点线等](https://blog.csdn.net/u013685264/article/details/121693261)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [opencv-python鼠标画点:cv2.drawMarker()](https://blog.csdn.net/bby1987/article/details/107302373)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文