In this lesson we showed how to track on object in openCV based on color, create contours for the object of interests, and then track the object of interest by drawing a bounding box around it. For your convenience, the code is included below.
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | import numpy as np import cv2 print(cv2.__version__) hueLow=90 hueHigh=100 hueLow2=90 hueHigh2=100 satLow=20 satHigh=200 valLow=20 valHigh=200 def onTrack1(val): global hueLow hueLow=val print('Low Hue: ',val) def onTrack2(val): global hueHigh hueHigh=val print('High Hue: ',val) def onTrack3(val): global satLow satLow=val print('Low Sat: ',val) def onTrack4(val): global satHigh satHigh=val print('High Sat: ',val) def onTrack5(val): global valLow valLow=val print('Low Val: ',val) def onTrack6(val): global valHigh valHigh=val print('High Val: ',val) def onTrack7(val): global hueLow2 hueLow2=val print('Low Hue2: ',val) def onTrack8(val): global hueHigh2 hueHigh2=val print('High Hue2: ',val) width=960 height=540 cam=cv2.VideoCapture(0) cam.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'MJPG')) cam.set(cv2.CAP_PROP_FRAME_WIDTH, width) cam.set(cv2.CAP_PROP_FRAME_HEIGHT, height) cv2.namedWindow('myTracker') cv2.moveWindow('myTracker',width,0) cv2.resizeWindow('myTracker',400,500) cv2.createTrackbar('Hue Low','myTracker',15,180,onTrack1) cv2.createTrackbar('Hue High','myTracker',30,180,onTrack2) cv2.createTrackbar('Hue Low2','myTracker',50,180,onTrack7) cv2.createTrackbar('Hue High2','myTracker',60,180,onTrack8) cv2.createTrackbar('Sat Low','myTracker',10,255,onTrack3) cv2.createTrackbar('Sat High','myTracker',255,255,onTrack4) cv2.createTrackbar('Val Low','myTracker',10,255,onTrack5) cv2.createTrackbar('Val High','myTracker',255,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]) lowerBound2=np.array([hueLow2,satLow,valLow]) upperBound2=np.array([hueHigh2,satHigh,valHigh]) myMask=cv2.inRange(frameHSV,lowerBound,upperBound) myMask2=cv2.inRange(frameHSV,lowerBound2,upperBound2) myMask=myMask | myMask2 #myMask=cv2.add(myMask,myMask2) #myMask=np.logical_or(myMask,myMask2) contours,junk=cv2.findContours(myMask,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) #cv2.drawContours(frame,contours,-1,(255,0,0),3) for contour in contours: area=cv2.contourArea(contour) if area>=200: #cv2.drawContours(frame,[contour],0,(255,0,0),3) x,y,w,h=cv2.boundingRect(contour) cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),3) #myMask=cv2.bitwise_not(myMask) myMaskSmall=cv2.resize(myMask,(int(width/2),int(height/2))) mySelection=cv2.bitwise_and(frame,frame, mask=myMask) mySelection=cv2.resize(mySelection,(int(width/2),int(height/2))) cv2.imshow('My Selection', mySelection) cv2.moveWindow('My Selection',int(width/2),height) 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() |