In this lesson I describe how to track an object in OpenCV based on color. We use the HSV color space to make it simpler to train the model. Below, for your convenience, we include the code we develop in this lesson.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | import numpy as np import cv2 print(cv2.__version__) def onTrack1(val): global hueLow hueLow=val print('Hue Low',hueLow) def onTrack2(val): global hueHigh hueHigh=val print('Hue High',hueHigh) def onTrack3(val): global satLow satLow=val print('Sat Low',satLow) def onTrack4(val): global satHigh satHigh=val print('Sat High',satHigh) def onTrack5(val): global valLow valLow=val print('Val Low',valLow) def onTrack6(val): global valHigh valHigh=val print('Val High',valHigh) width=640 height=360 cam=cv2.VideoCapture(0,cv2.CAP_DSHOW) cam.set(cv2.CAP_PROP_FRAME_WIDTH, width) cam.set(cv2.CAP_PROP_FRAME_HEIGHT,height) cam.set(cv2.CAP_PROP_FPS, 30) cam.set(cv2.CAP_PROP_FOURCC,cv2.VideoWriter_fourcc(*'MJPG')) cv2.namedWindow('myTracker') cv2.moveWindow('myTracker',width,0) hueLow=10 hueHigh=20 satLow=10 satHigh=250 valLow=10 valHigh=250 cv2.createTrackbar('Hue Low','myTracker',10,179,onTrack1) cv2.createTrackbar('Hue High','myTracker',20,179,onTrack2) cv2.createTrackbar('Sat Low','myTracker',10,255,onTrack3) cv2.createTrackbar('Sat High','myTracker',250,255,onTrack4) cv2.createTrackbar('Val Low','myTracker',10,255,onTrack5) cv2.createTrackbar('Val High','myTracker',250,255,onTrack6) while True: ignore, frame = cam.read() frameHSV=cv2.cvtColor(frame,cv2.COLOR_BGR2HSV) lowerBound=np.array([hueLow,satLow,valLow]) upperBound=np.array([hueHigh,satHigh,valHigh]) myMask=cv2.inRange(frameHSV,lowerBound,upperBound) #myMask=cv2.bitwise_not(myMask) myObject=cv2.bitwise_and(frame,frame,mask=myMask) myObjectSmall=cv2.resize(myObject,(int(width/2),int(height/2))) cv2.imshow('My Object',myObjectSmall) cv2.moveWindow('My Object',int(width/2),int(height)) myMaskSmall=cv2.resize(myMask,(int(width/2),int(height/2))) cv2.imshow('My Mask',myMaskSmall) cv2.moveWindow('My Mask',0,height) cv2.imshow('my WEBcam', frame) cv2.moveWindow('my WEBcam',0,0) if cv2.waitKey(1) & 0xff ==ord('q'): break cam.release() |