Object Tracker
Simple color-based object detection/tracking.
By Laurent Ittiitti@usc.eduhttp://jevois.orgGPL v3
 Language: C++Supports mappings with USB output: YesSupports mappings with NO USB output: Yes 
 Video Mapping:   YUYV 320 254 60.0 YUYV 320 240 60.0 JeVois ObjectTracker
 Video Mapping:   NONE 0 0 0.0 YUYV 320 240 60.0 JeVois ObjectTracker

Module Documentation

This modules isolates pixels within a given HSV range (hue, saturation, and value of color pixels), does some cleanups, and extracts object contours. It sends information about object centers over serial.

This module usually works best with the camera sensor set to manual exposure, manual gain, manual color balance, etc so that HSV color values are reliable. See the file script.cfg file in this module's directory for an example of how to set the camera settings each time this module is loaded.

This code was loosely inspired by: https://raw.githubusercontent.com/kylehounslow/opencv-tuts/master/object-tracking-tut/objectTrackingTut.cpp written by Kyle Hounslow, 2013.

Serial Messages

This module can send standardized serial messages as described in Standardized serial messages formatting. One message is issued on every video frame for each detected and good object (good objects have a pixel area within the range specified by objectarea, and are only reported when the image is clean enough according to maxnumobj). The id field in the messages simply is blob for all messages.

  • Serial message type: 2D
  • id: always blob
  • x, y, or vertices: standardized 2D coordinates of blob center or of corners of bounding box (depending on serstyle)
  • w, h: standardized object size
  • extra: none (empty string)

JeVois v1.8.1: Note that this module sends one message per detected object (blob) and per frame. You may want to specify setpar serstamp Frame if you need to get all the objects on one given frame (they will have the same frame number prefix). Here is an example output with setpar serstyle Detail and setpar serstamp Frame (see Standardized serial messages formatting for more info about serstamp and serstyle):

2940 D2 blob 4 -238 237 -238 19 13 19 13 237
2940 D2 blob 4 266 209 194 206 213 -277 285 -274
2940 D2 blob 4 575 -243 499 -511 613 -544 689 -275
2940 D2 blob 4 -150 -413 -150 -613 50 -613 50 -413
2940 D2 blob 4 -913 -306 -1000 -306 -1000 -750 -913 -750
2941 D2 blob 4 -313 119 -313 -106 -63 -106 -63 119
2941 D2 blob 4 189 105 115 99 157 -385 231 -379
2941 D2 blob 4 469 -275 469 -500 575 -500 575 -275
2941 D2 blob 4 -200 -531 -200 -744 -13 -744 -13 -531
2942 D2 blob 4 -381 -13 -381 -244 -113 -244 -113 -13
2942 D2 blob 4 478 -213 310 -402 414 -494 582 -305
2942 D2 blob 4 144 -6 53 -15 114 -668 206 -660

In this example, we detected 5 blobs on frame 2940, then 4 blobs on frame 2941, then 3 blobs on frame 2942.

See Standardized serial messages formatting for more on standardized serial messages, and Helper functions to convert coordinates from camera resolution to standardized for more info on standardized coordinates.

Trying it out

The default parameter settings (which are set in script.cfg explained below) attempt to detect light blue objects. Present a light blue object to the JeVois camera and see whether it is detected. When detected and good enough according to objectarea and maxnumobj, a green circle will be drawn at the center of each good object.

For further use of this module, you may want to check out the following tutorials:


You should adjust parameters hrange, srange, and vrange to isolate the range of Hue, Saturation, and Value (respectively) that correspond to the objects you want to detect. Note that there is a script.cfg file in this module's directory that provides a range tuned to a lighgt blue object, as shown in the demo screenshot.

Tuning the parameters is best done interactively by connecting to your JeVois camera while it is looking at some object of the desired color. Once you have achieved a tuning, you may want to set the hrange, srange, and vrange parameters in your script.cfg file for this module on the microSD card (see below).

Typically, you would start by narrowing down on the hue, then the value, and finally the saturation. Make sure you also move your camera around and show it typical background clutter so check for false positives (detections of things which you are not interested, which can happen if your ranges are too wide).

Config file

JeVois allows you to store parameter settings and commands in a file named script.cfg stored in the directory of a module. The file script.cfg may contain any sequence of commands as you would type them interactively in the JeVois command-line interface. For the ObjectTracker module, a default script is provided that sets the camera to manual color, gain, and exposure mode (for more reliable color values), and to setup communication with a pan/tilt head as described in Tutorial on how to write Arduino code that interacts with JeVois.

The script.cfg file for ObjectTracker is stored on your microSD at JEVOIS:/modules/JeVois/ObjectTracker/script.cfg and is shown in Tutorial on how to write Arduino code that interacts with JeVois as an example.

ParameterTypeDescriptionDefaultValid Values
(BlobDetector) hrangejevois::Range<unsigned char>Range of H values for HSV window (0=red/do not use because of wraparound, 30=yellow, 45=light green, 60=green, 75=green cyan, 90=cyan, 105=light blue, 120=blue, 135=purple, 150=pink)jevois::Range<unsigned char>(10, 245)-
(BlobDetector) srangejevois::Range<unsigned char>Range of S (saturation) values for HSV windowjevois::Range<unsigned char>(10, 245)-
(BlobDetector) vrangejevois::Range<unsigned char>Range of V (brightness) values for HSV windowjevois::Range<unsigned char>(10, 245)-
(BlobDetector) maxnumobjsize_tMax number of objects to declare a clean image10-
(BlobDetector) objectareajevois::Range<unsigned int>Range of object area (in pixels) to trackjevois::Range<unsigned int>(20*20, 100*100)-
(BlobDetector) erodesizesize_tErosion structuring element size (pixels)3-
(BlobDetector) dilatesizesize_tDilation structuring element size (pixels)8-
script.cfg file
# Demo configuration script for ObjectTracker module.

# Set camera to fixed color balance, gain, and exposure, so that we get more reliable colors than we would obtain under
# automatic mode:
setcam autowb 1
setcam autogain 0
setcam autoexp 0
setcam redbal 110
setcam bluebal 170
setcam gain 16
setcam absexp 500

# Detect a light blue flash drive:
setpar hrange 95...110
setpar srange 100...255
setpar vrange 60...253

# Send info log messages to None, send serial strings from module to Hard serial port:
setpar serlog None
setpar serout Hard

# Apply high gain to our pan/tilt servos, sending the commands below to our Arduino over the Hard serial port that we
# configured above to handle the serout messages. The Arduino controlling the pan/tilt servos will receive and parse
# these commands, and will set the servo gains:
serout PANGAIN 400
serout TILTGAIN 300
Detailed docs:ObjectTracker
Copyright:Copyright (C) 2016 by Laurent Itti, iLab and the University of Southern California
License:GPL v3
Support URL:http://jevois.org/doc
Other URL:http://iLab.usc.edu
Address:University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA