python获取图像灰度极值点_opencv-python读取tiff影像上任意点的灰度值和RBG值

主要是给OpenCV的窗体添加鼠标的滑动事件,经过鼠标的事件返回的X,Y值,去图像的数据上查找对应的值,绘图值须要将tiff转为灰度图像来查找,RGB值是经过tiff以RGB模式读取得到。方便查看图像上任意一点的像元值,能够作研究用。html

代码以下python

# 导入cv模块

import cv2 as cv

import time

import numpy as np

# 读取图像,支持 bmp、jpg、png、tiff 等经常使用格式

# 第二个参数是通道数和位深的参数,有四种选择,参考https://www.cnblogs.com/goushibao/p/6671079.html

# 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()

运行如图code

python获取图像灰度极值点_opencv-python读取tiff影像上任意点的灰度值和RBG值_第1张图片

你可能感兴趣的:(python获取图像灰度极值点)