首页 > 解决方案 > 如何在 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()

标签: pythonopencvimage-processing

解决方案


推荐阅读