Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added background subtraction to tracker, and adjusted some parameters…
…. (Still a little ad hoc, but not terribly so.) Tracking performance has improved. Added a couple of more tests.
- Loading branch information
Nathaniel M Guy
committed
May 30, 2015
1 parent
362f016
commit 7338155
Showing
4 changed files
with
84 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import cv2 | ||
import numpy as np | ||
|
||
# Open up a video from which to get frames | ||
cap = cv2.VideoCapture('juggling.mp4') | ||
|
||
fgbg = cv2.createBackgroundSubtractorMOG2() | ||
|
||
while True: | ||
|
||
# Read a frame | ||
_, frame = cap.read() | ||
|
||
fgmask = fgbg.apply(frame) | ||
res = cv2.bitwise_and(frame,frame,mask = fgmask) | ||
|
||
cv2.imshow('frame',res) | ||
# cv2.imshow('fgmask',fgmask) | ||
|
||
# img = cv2.medianBlur(frame,9) | ||
# bwimg = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) | ||
|
||
# circles = cv2.HoughCircles(bwimg, cv2.HOUGH_GRADIENT,1,20, | ||
# param1=50,param2=50,minRadius=20,maxRadius=100) | ||
# if circles is None or len(circles) > 10: | ||
# continue | ||
|
||
# circles = np.uint16(np.around(circles)) | ||
# for i in circles[0,:]: | ||
# # draw the outer circle | ||
# cv2.circle(frame,(i[0],i[1]),i[2],(0,0,0),2) | ||
# # draw the center of the circle | ||
# # cv2.circle(img,(i[0],i[1]),2,(0,0,0),3) | ||
|
||
# cv2.imshow('detected circles',frame) | ||
|
||
k = cv2.waitKey(5) & 0xFF | ||
if k == 27: | ||
# User hit ESC | ||
break | ||
|
||
cv2.destroyAllWindows() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import cv2 | ||
import numpy as np | ||
cap = cv2.VideoCapture("juggling.mp4") | ||
|
||
ret, frame1 = cap.read() | ||
prvs = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY) | ||
hsv = np.zeros_like(frame1) | ||
hsv[...,1] = 255 | ||
|
||
while(1): | ||
ret, frame2 = cap.read() | ||
next = cv2.cvtColor(frame2,cv2.COLOR_BGR2GRAY) | ||
|
||
flow = cv2.calcOpticalFlowFarneback(prvs,next, None, 0.5, 3, 15, 3, 5, 1.2, 0) | ||
|
||
mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1]) | ||
hsv[...,0] = ang*180/np.pi/2 | ||
hsv[...,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX) | ||
rgb = cv2.cvtColor(hsv,cv2.COLOR_HSV2BGR) | ||
|
||
cv2.imshow('frame2',rgb) | ||
k = cv2.waitKey(30) & 0xff | ||
if k == 27: | ||
break | ||
elif k == ord('s'): | ||
cv2.imwrite('opticalfb.png',frame2) | ||
cv2.imwrite('opticalhsv.png',rgb) | ||
prvs = next | ||
|
||
cap.release() | ||
cv2.destroyAllWindows() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters