opencv+python 鼠标指针颜色检测

该模块有助于了解opencv中颜色的分布,返回rgb值并在屏幕左上角显示出来
import numpy as np
import math
import cv2
import time
ix=1
iy=1
f=[]
a=1
b=1
c=1
ixx=1
iyy=1
def nothing(x):
    pass
def onmouse(event,x,y,flags,param):
    global ix,iy,f,a,b,c,ixx,iyy
    if event==cv2.EVENT_MOUSEMOVE:
        #frame[y,x].tolist()
        #print(frame[y,x])
        f=list(frame[y,x])
        ix=x
        iy=y
        a=f[0]
        b=f[1]
        c=f[2]
    if event==cv2.EVENT_LBUTTONDBLCLK:
        ixx=x
        iyy=y











cap = cv2.VideoCapture(0)   #打开摄像头0
var = cv2.CAP_PVAPI
LowerRed = np.array([24, 75, 37])

# 创建改变颜色的滚动条
# cv2.namedWindow("fps")  #新建一个窗口
# cv2.createTrackbar('R','fps',89,255,nothing)    #想要追踪颜色的R# cv2.createTrackbar('G','fps',254,255,nothing)   #想要追踪颜色的G# cv2.createTrackbar('B','fps',254,255,nothing)   #想要追踪颜色的Bcv2.createTrackbar('threshold','fps',19,255,nothing)    #二值化阈值

while(1):
    #滑动条赋值

    r=int(a)
    g=int(b)
    b=int(c)

    #c = cv2.getTrackbarPos('threshold', 'fps')
    #UpperRed = np.array([r, g, b])
    ret, frame = cap.read()#得到每一帧

    imgHSV=cv2.cvtColor(frame,cv2.COLOR_RGB2HSV)


    cv2.setMouseCallback('fps',onmouse)


    #print(ix,iy)
    cv2.rectangle(frame, (10, 10), (100, 50), (r, g, b), 3)
    cv2.line(frame,(ixx,0),(ixx,700),(255,0,0),3)
    cv2.line(frame, (0, iyy), (700, iyy), (255, 0, 0), 3)
    cv2.imshow("fps", frame)
    #print(a[1])
    cv2.imshow("imgHSV",imgHSV)

    if cv2.waitKey(1) & 0xFF == ord('q'):  # q保存一张图片
        #cv2.imwrite("E:\cpy\pictures\\pic.jpg", frame1)
        break
cap.release()
cv2.destroyAllWindows()
 
  

opencv+python 鼠标指针颜色检测_第1张图片

opencv+python 鼠标指针颜色检测_第2张图片

左上角将会显示鼠标指针处的颜色(指针并未出现在截屏处,但实际上是有的)

你可能感兴趣的:(opencv,python3,opencv,serial,python,opencv)