Go to the documentation of this file.00001
00002 import roslib; roslib.load_manifest('head_pose_estimation')
00003 import rospy
00004 import cv2
00005 import numpy as np
00006 from cv_bridge import CvBridge, CvBridgeError
00007 from sensor_msgs.msg import Image
00008 from geometry_msgs.msg import Vector3
00009
00010 lk_params = dict( winSize = (15, 15),
00011 maxLevel = 2,
00012 criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03),
00013 derivLambda = 0.0 )
00014
00015
00016
00017
00018
00019
00020 feat_params = dict(
00021 maxCorners = 100,
00022 qualityLevel = 0.01,
00023 minDistance = 5)
00024
00025
00026 class OpticFlow(object):
00027 last_frame = None
00028 prev_pts = None
00029 bridge = CvBridge()
00030 vect_pub = rospy.Publisher('flow', Vector3)
00031
00032 def image_cb(self, msg):
00033 frame = np.asarray(self.bridge.imgmsg_to_cv(msg, 'mono8'))
00034 if self.last_frame is not None:
00035 next_pts, status, err = cv2.calcOpticalFlowPyrLK(self.last_frame, frame, self.prev_pts, None, **lk_params)
00036 diffs = (next_pts - self.prev_pts).squeeze()
00037 mean = diffs.mean(0)
00038 sigma = diffs.std(0)
00039
00040
00041
00042
00043
00044
00045
00046 self.vect_pub.publish(mean[0], mean[1], 0)
00047 self.prev_pts = next_pts
00048
00049 else:
00050 self.prev_pts = cv2.goodFeaturesToTrack(frame, **feat_params)
00051 self.last_frame = frame
00052
00053 if __name__ == '__main__':
00054 rospy.init_node('optic_flow')
00055 flow = OpticFlow()
00056 rospy.Subscriber('image', Image, flow.image_cb)
00057 rospy.spin()