主要是给OpenCV的窗体添加鼠标的滑动事件,通过鼠标的事件返回的X,Y值,去图像的数据上查找对应的值,绘图值需要将tiff转为灰度图像来查找,RGB值是通过tiff以RGB模式读取获得。方便查看图像上任意一点的像元值,可以做研究用。
代码如下
# 导入cv模块
import cv2 as cv
import time
import numpy as np
# 读取图像,支持 bmp、jpg、png、tiff 等常用格式
# 第二个参数是通道数和位深的参数,有四种选择,
# 1彩色2灰度
img = cv.imread("sancun.tif", 1)
title = "image"
# 创建窗口并显示图像
cv.namedWindow(title, cv.WINDOW_NORMAL)
cv.imshow(title, img)
# 采集转灰度
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
def mouse_click(event, x, y, flags, param):
if event == cv.EVENT_MOUSEMOVE:
# cv.destroyAllWindows()
# print(event, x, y, flags, param)
# print("像素值", gray[x][y])
# print("RGB", img[x][y])
time1 = time.clock()
img1 = img.copy()
print("耗时", time.clock() - time1)
cv.putText(img1, "Gray:"+str(gray[x][y]), (50, 150), cv.FONT_HERSHEY_COMPLEX, 5, (0, 255, 0), 12)
cv.putText(img1, "RGB:"+str(img[x][y]), (50, 350), cv.FONT_HERSHEY_COMPLEX, 5, (0, 255, 0), 12)
cv.imshow(title, img1)
# print("耗时", time.clock()-time1)
cv.setMouseCallback(title, mouse_click)
cv.waitKey(0)
# 释放窗口
cv.destroyAllWindows()
运行如图