opencv鼠标事件练习:图片取色器实现

opencv的鼠标事件格式比较固定:调用setMouseCallback函数

setMouseCallback(窗口名, 鼠标事件响应函数, 传入响应函数的参数)

响应函数格式如下:

functionName(event, x, y, flag, param)
其中event flag 是鼠标事件标志,详见官方文档
(x,y)为鼠标在窗口中点击的位置,与数组坐标同参考系
param是传入参数

import cv2 as cv

def tirgger_left_clicked(event, x, y, flags, param):
    if event == cv.EVENT_LBUTTONDOWN:
        print('coordinate is :', (x, y))
        print('In BGR is:' ,param[0][y][x])
        print('In HSV is:' ,param[1][y][x] ,'\n')


if __name__ == '__main__':
    img = cv.imread('test01.jpg')
    img_hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
    cv.namedWindow('image', cv.WINDOW_AUTOSIZE)

    flag = True
    while(flag):
        cv.imshow('image', img)
        cv.setMouseCallback('image', tirgger_left_clicked, [img ,img_hsv])
        flag = (cv.waitKey(20) & 0xFF != 27)
    cv.destroyWindow('image')

运行结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/white_156/article/details/105120362