Skip to content

Commit 2c7d5ce

Browse files
Add files via upload
- Ant motion tracking python program using Python OpenCV library. The ManyAntsActualMovement.py file is containing the tracking program which alter every frame in the video by the ants tracking. Then the ManyAntsVideoCreation.py file append all frames to perform a new video with ant tracking. The ManyAnts.mov video is the original video before applying the motion tracking,the ActualManyAnts.avi is the output video after applying the motion tracking.
1 parent 57bb8d0 commit 2c7d5ce

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

ActualManyAnts.avi

2.21 MB
Binary file not shown.

ManyAnts.mov

17.8 MB
Binary file not shown.

ManyAntsActualMovment.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# import the necessary packages
2+
from collections import deque
3+
import numpy as np
4+
import argparse
5+
import imutils
6+
import cv2
7+
import math
8+
import copy
9+
10+
# to check if two contours very close then will compine them
11+
def find_if_close(cnt1,cnt2):
12+
row1,row2 = cnt1.shape[0],cnt2.shape[0]
13+
for i in xrange(row1):
14+
for j in xrange(row2):
15+
dist = np.linalg.norm(cnt1[i]-cnt2[j])
16+
if abs(dist) < 50 :
17+
return True
18+
elif i==row1-1 and j==row2-1:
19+
return False
20+
21+
greenLower = (19, 15, 34)
22+
greenUpper = (30, 22, 46)
23+
pts = deque(maxlen=32)
24+
counter = 0
25+
26+
camera = cv2.VideoCapture("Sample_Tracking_Release/Many_Ants/ManyAnts.mov")
27+
histroy = []
28+
histroy.append(deque())
29+
frameIndex = 0
30+
while True:
31+
# grab the current frame
32+
(grabbed, frame) = camera.read()
33+
if not grabbed:
34+
break
35+
36+
hsv = cv2.cvtColor(frame, cv2.CAP_MODE_RGB)
37+
mask = cv2.inRange(hsv, greenLower, greenUpper)
38+
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
39+
cv2.CHAIN_APPROX_SIMPLE)[-2]
40+
41+
center = None
42+
# only proceed if at least one contour was found
43+
if len(cnts) > 0:
44+
45+
LENGTH = len(cnts)
46+
status = np.zeros((LENGTH, 1))
47+
for i, cnt1 in enumerate(cnts):
48+
x = i
49+
if i != LENGTH - 1:
50+
for j, cnt2 in enumerate(cnts[i + 1:]):
51+
x = x + 1
52+
dist = find_if_close(cnt1, cnt2)
53+
if dist == True:
54+
val = min(status[i], status[x])
55+
status[x] = status[i] = val
56+
else:
57+
if status[x] == status[i]:
58+
status[x] = i + 1
59+
60+
unified = []
61+
maximum = int(status.max()) + 1
62+
for i in xrange(maximum):
63+
pos = np.where(status == i)[0]
64+
if pos.size != 0:
65+
c = np.vstack(cnts[i] for i in pos)
66+
hull = cv2.convexHull(c)
67+
unified.append(hull)
68+
69+
((x, y), radius) = cv2.minEnclosingCircle(c)
70+
M = cv2.moments(c)
71+
drwaCirlce= "no"
72+
if(len(cnts) >2):
73+
if(M["m00"] >0):
74+
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
75+
drwaCirlce = "yes"
76+
else:
77+
center = (cnts[0][:, 0][0, 0], cnts[0][:, 0][0, 1])
78+
else:
79+
center =(cnts[0][:,0][0,0],cnts[0][:,0][0,1])
80+
81+
if(drwaCirlce is "yes"):
82+
#cricle1 = cv2.circle(frame, (int(x), int(y)), int(50),
83+
cricle1 = cv2.circle(frame, (int(x), int(y)), int(50),
84+
(0, 255, 255), 2)
85+
86+
#cv2.circle(frame, center, 5*10, (0, 0, 255), 2)
87+
pts.appendleft((int(x), int(y)))
88+
if histroy is not None:
89+
histroy[frameIndex].appendleft(copy.deepcopy((int(x), int(y))))
90+
91+
if frameIndex > 0:
92+
for j in range(0, frameIndex):
93+
if len(histroy[j]) < len(histroy[j - 1]):
94+
min1 = len(histroy[j])
95+
else:
96+
min1 = len(histroy[j - 1])
97+
for i in np.arange(0, min1):
98+
99+
if j >0 and len(histroy[j])>0 :
100+
dist = math.hypot(histroy[j][i][0] - histroy[j-1][i][0], histroy[j][i][1] - histroy[j-1][i][1])
101+
if dist <35:
102+
cv2.line(frame, histroy[j-1][i], histroy[j][i], (0, 0, 255), 5)
103+
104+
if len(histroy[frameIndex]) > 0:
105+
histroy.append(deque())
106+
frameIndex += 1
107+
108+
cv2.namedWindow('Frame', cv2.WINDOW_NORMAL)
109+
cv2.resizeWindow('Frame', 500, 500)
110+
cv2.imshow("Frame", frame)
111+
name = "frames/actualMovment/frameoutput%d.jpg" % counter
112+
cv2.imwrite(name, frame)
113+
key = cv2.waitKey(1) & 0xFF
114+
counter += 1
115+
116+
# if the 'q' key is pressed, stop the loop
117+
if key == ord("q"):
118+
break
119+
120+
camera.release()
121+
cv2.destroyAllWindows()

ManyAntsActualMovmentVideoCreation.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# import the necessary packages
2+
from collections import deque
3+
import numpy as np
4+
import argparse
5+
import imutils
6+
import cv2
7+
import math
8+
import copy
9+
10+
out = cv2.VideoWriter('output/ActualManyAnts.avi', -1, 20.0, (1920,1080))
11+
for m in range(0,132):
12+
out.write(cv2.imread("frames/actualMovment/frameoutput%d.jpg" % m))
13+
14+
out.release()
15+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)