Color Tracking

From KameRider
Jump to: navigation, search
  • follow the instructions on this website to identify the colour that user want to track: [1]
  • the topic related to color tracking is /blobs which published by cmvision.
  • the messages contains in /blobs are the number of colour object detected and its coordinates in reference to the camera size.
  • for an example:
    • if camera detects a big group of desire colour at point A and a small group of desire colour at point B, then the number of colour object will be two.
    • assume the camera image size is 640x320, if point A locates at the middle of the camera image then its coordinate will be:
      • x = 320
      • y = 160
  • An example of source code with explanation:

include section:

1 # Include cmvision library and its message type
2 import roslib; roslib.load_manifest('cmvision')
3 from cmvision.msg import Blobs
4 
5 # Global variable to store color blob position
6 blob_position_x = 0

initiation section:

1 #subscribe to the /blobs topic to calculate the colour position.
2 rospy.Subscriber('/blobs', Blobs, self.callback)

callback function section

 1 def callback(self, data):
 2     # if there is any number of colour blob in "data.blobs"
 3     if(len(data.blobs)):
 4 	global blob_position_x
 5 
 6 	blob_position_x = 0
 7         # "obj" is the number of colour blob detected
 8 	for obj in data.blobs:
 9 	    blob_position_x = blob_position_x + obj.x
10 	blob_position_x = blob_position_x/len(data.blobs)

main (loop) section:

1     # Display the position of colour blob
2     rospy.loginfo("blob is at x: %s"%blob_position_x)
  • source code of the above example can be found here: [2]
  • source code of integration of colour tracking with navigation can be found here: [3]