python - Python OpenCV - 添加状态栏以显示鼠标位置和颜色 RGB
问题描述
当使用“imread”命令加载图像或视频时,我想在状态栏上显示鼠标位置的坐标和 RGB 颜色。我正在使用 Windows 10 Python 3.7.4 Opencv 4.2.0.34
例子:
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
exit()
cv2.imshow('example', frame)
k = cv2.waitKey(1)
if k == ord('q'):
exit()
cap.release()
cv2.destroyAllWindows()
解决方案
要在状态栏上显示值,您必须按照此处所述构建您的OpenCV
with ,现在为了在图像顶部展示值,您可以执行以下操作:Qt
R, G, B
import cv2
"""
Reference:
https://www.geeksforgeeks.org/displaying-the-coordinates-of-the-points-clicked-on-the-image-using-python-opencv/
"""
# function to display the coordinates of
# of the points clicked on the image
def move_event(event, x, y, flags, params):
imgk = img.copy()
# checking for right mouse clicks
if event==cv2.EVENT_MOUSEMOVE:
# displaying the coordinates
# on the Shell
# print(x, ' ', y)
# displaying the coordinates
# on the image window
font = cv2.FONT_HERSHEY_SIMPLEX
org = (x, y)
B = imgk[y, x, 0]
G = imgk[y, x, 1]
R = imgk[y, x, 2]
cv2.putText(imgk, '(x, y)=({}, {})'.format(x, y), org, font, 1, (255, 255, 255), 1, cv2.LINE_8)
cv2.putText(imgk, ' ,R={}'.format(R), org, font, 1, (0, 0, 255), 1, cv2.LINE_8)
cv2.putText(imgk, ' ,G={}'.format(G), org, font, 1, (0, 255, 0), 1, cv2.LINE_8)
cv2.putText(imgk, ' ,B={}'.format(B), org, font, 1, (255, 0, 0), 1, cv2.LINE_8)
cv2.imshow('image', imgk)
# reading the image
img = cv2.imread('image.jpg')
# displaying the image
cv2.namedWindow("image", cv2.WINDOW_NORMAL)
cv2.imshow('image', img)
# setting mouse hadler for the image
# and calling the click_event() function
cv2.setMouseCallback('image', move_event)
# wait for a key to be pressed to exit
cv2.waitKey(0)
# close the window
cv2.destroyAllWindows()
推荐阅读
- google-bigquery - 无法将小型数据子集合并到大型分区 BigQuery 表中
- google-apps-script - 谷歌表格 API PDF 命名
- sql - 简单查询给出 - '' 附近的语法不正确
- flutter - 将 Firebase 与 Flutter 一起使用时遇到问题
- powershell - wkhtmltopdf url 到带有传递凭据的 pdf
- python - 循环中的三次方程求解器:Python
- python - [x](y) 运算符到底是做什么的?
- c - 试图从文本文件中读取两个数字
- automation - 根据过滤器参数自动将SSRS报告导出到excel
- ruby - 执行 ruby 代码而不保存文件