python - 如何在 OpenCV 中的图像上临时显示文本?
问题描述
我想在鼠标悬停时显示像素值,但是当我putText
在图像上使用方法时,它会替换原始图像变量。
另外,我想在鼠标悬停时暂时显示坐标。
因此,当我将鼠标移动到不同的位置时,我应该只能显示当前坐标。
有人可以帮帮我吗?
# importing the libraries
import cv2
# storing the coordinates
points = []
# event function
def click_event(event, x, y, flag, params):
global points
if event == cv2.EVENT_LBUTTONDOWN:
cv2.circle(img, (x,y), 2, (0,0,0), -1)
points.append((x,y))
if len(points) == 2:
cv2.line(img, points[0], points[1], (0,0,0), 2)
points = []
cv2.imshow('Scientists', img)
elif event == cv2.EVENT_MOUSEMOVE:
coordinates = '('+str(x)+','+str(y)+')'
cv2.putText(img, coordinates, (x,y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,0))
cv2.imshow('Scientists', img)
# reading the image
img = cv2.imread('scientists.jpg', -1)
# displaying the image
cv2.imshow('Scientists', img)
# event listener method
cv2.setMouseCallback('Scientists', click_event)
cv2.waitKey(0)
cv2.destroyAllWindows()
解决方案
推荐阅读
- c# - 如何将我的新图像的路径作为字符串保存到数据库中,以便在创建新记录后我可以看到我选择的图像?
- java - 从底部对齐reclyerview子级
- google-drive-api - 用于创建特定输入列表的自动表
- html - 如何平滑 html 网络摄像头视频?(与原生移动相机相比非常生涩)
- linux - ext4 为目录条目启用哈希
- swift - 使用静态数据构建字典类 (Swift)
- css - 使用 Material-UI 将徽标与中心对齐
- verilog - 如何在 Chisel3 中使用多输入逻辑门?
- c++ - 使用暂停按钮 ARDUINO 创建灯光序列的问题
- pg-promise - 如何使用 pg-promise 将 jsonb[] 数据插入列