Opencv点击获得图像中的像素值-python

参考博客

这里要注意的是opencv读取的像素值是bgr格式,因此在图上显示出来的也是bgr的像素值

import numpy as np
import matplotlib.pyplot as plt
import cv2

img_path = r'D:\DataSet\img\seg\SegmentationClassPNG\0.png'
img = cv2.imread(img_path)

def on_EVENT_LBUTTONDOWN(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        xy = "(%d,%d,%d)" % (img[y][x][0], img[y][x][1], img[y][x][2])
        # xy = "%d,%d" % (x, y)
        cv2.circle(img, (x, y), 1, (255, 0, 0), thickness = -1)
        cv2.putText(img, xy, (x, y), cv2.FONT_HERSHEY_PLAIN,
                    1.0, (0,0,0), thickness = 1)
        cv2.imshow("image", img)

cv2.namedWindow("image")
cv2.setMouseCallback("image", on_EVENT_LBUTTONDOWN)
while(1):
    cv2.imshow("image", img)
    if cv2.waitKey(0)&0xFF==27:
        break
cv2.destroyAllWindows()

你可能感兴趣的:(python实用函数,opencv,python,计算机视觉)