Skip to content

Commit 6e41e7d

Browse files
committed
added obj tracking using hsv
1 parent 9f26d6d commit 6e41e7d

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Object detection and tracking
2+
### Using HSV color spaces
3+
4+
> Benefits: Simple code; Fast object tracking in real-time, easily obtaining 32+ FPS on modern hardware systems.
5+
6+
- To test on a video: ```python run.py --video video.mp4```.
7+
8+
- To test on webcam: ```python run.py```.
9+
10+
> Where video.mp4 should be a video file in your disk. Note that the script is intended to find balls (green and blue) based only on their colors.
11+
12+
- One example to define such a color space (line 12-14 in run.py) is by using the HSV chart:
13+
14+
<div align="center">
15+
<img src= misc/hsv.png?raw=true width=500>
16+
</div>
17+
18+
- Notice how values fall in the range [0, 360]. Simply divide by 2 to bring values into the range [0, 180] which is what OpenCV expects.
19+
20+
> As we know, colors can appear dramatically different depending on our lighting conditions. Instead, an approach of gathering data, performing experiments, and validating is better.
21+
22+
## References
23+
- For solid theory: https://www.pyimagesearch.com/pyimagesearch-gurus/
24+
25+
---
26+
27+
saimj7/ 26-05-2021 © <a href="http://saimj7.github.io" target="_blank">Sai_Mj</a>.
Loading
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# import the necessary packages
2+
import argparse
3+
import imutils
4+
import cv2
5+
6+
# construct the argument parse and parse the arguments
7+
ap = argparse.ArgumentParser()
8+
ap.add_argument("-v", "--video", help="path to the (optional) video file")
9+
args = vars(ap.parse_args())
10+
11+
# define the color ranges
12+
colorRanges = [
13+
((29, 86, 6), (64, 255, 255), "green"),
14+
((57, 68, 0), (151, 255, 255), "blue")]
15+
16+
# if a video path was not supplied, grab the reference to the webcam
17+
if not args.get("video", False):
18+
camera = cv2.VideoCapture(0)
19+
20+
# otherwise, grab a reference to the video file
21+
else:
22+
camera = cv2.VideoCapture(args["video"])
23+
24+
# keep looping
25+
while True:
26+
# grab the current frame
27+
(grabbed, frame) = camera.read()
28+
29+
# if we are viewing a video and we did not grab a frame, then we have
30+
# reached the end of the video
31+
if args.get("video") and not grabbed:
32+
break
33+
34+
# resize the frame, blur it, and convert it to the HSV color space
35+
frame = imutils.resize(frame, width=600)
36+
blurred = cv2.GaussianBlur(frame, (11, 11), 0)
37+
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
38+
39+
# loop over the color ranges
40+
for (lower, upper, colorName) in colorRanges:
41+
# construct a mask for all colors in the current HSV range, then
42+
# perform a series of dilations and erosions to remove any small
43+
# blobs left in the mask
44+
mask = cv2.inRange(hsv, lower, upper)
45+
mask = cv2.erode(mask, None, iterations=2)
46+
mask = cv2.dilate(mask, None, iterations=2)
47+
48+
# find contours in the mask
49+
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
50+
cv2.CHAIN_APPROX_SIMPLE)
51+
cnts = imutils.grab_contours(cnts)
52+
53+
# only proceed if at least one contour was found
54+
if len(cnts) > 0:
55+
# find the largest contour in the mask, then use it to compute
56+
# the minimum enclosing circle and centroid
57+
c = max(cnts, key=cv2.contourArea)
58+
((x, y), radius) = cv2.minEnclosingCircle(c)
59+
M = cv2.moments(c)
60+
(cX, cY) = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
61+
62+
# only draw the enclosing circle and text if the radious meets
63+
# a minimum size
64+
if radius > 10:
65+
cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2)
66+
cv2.putText(frame, colorName, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX,
67+
1.0, (0, 255, 255), 2)
68+
69+
# show the frame to our screen
70+
cv2.imshow("Frame", frame)
71+
key = cv2.waitKey(1) & 0xFF
72+
73+
# if the 'q' key is pressed, stop the loop
74+
if key == ord("q"):
75+
break
76+
77+
# cleanup the camera and close any open windows
78+
camera.release()
79+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)