このモジュールは、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 ): グローバルix,iy,f,a,b,c,ixx,iyy ifevent ==cv2.EVENT_MOUSEMOVE: #frame[y,x].tolist() #print(frame[y,x] ) f=リスト(frame[y,x]) ix=x iy=y a=f[ 0 ] b=f[ 1 ] c=f[ 2 ] イベント==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) #Bを追跡したい色の値 cv2.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、フレーム = キャップ。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()
左上隅にはマウス ポインタの色が表示されます (スクリーンショットにはポインタが表示されていませんが、実際に表示されています)。