fisheye_ray.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import cv2
3 
4 import rospy
5 import cv_bridge
6 from sensor_msgs.msg import Image, CameraInfo
7 from geometry_msgs.msg import Vector3, PoseStamped, PointStamped
8 import math
9 import tf
10 import numpy as np
11 
12 
13 def camera_info_cb(msg):
14  global latest_camera_info
15  latest_camera_info = msg;
16 
17 
18 def image_cb(msg):
19  global latest_image
20  latest_image = msg;
21 
22 
23 def cloud_cb(msg):
24  global latest_camera_info, latest_image, frame_id
25  # if latest_camera_info:
26  if latest_image is not None:
27  # r = math.sqrt((msg.point.x - latest_camera_info.width/2)*(msg.point.x - latest_camera_info.width/2)+ (msg.point.y - latest_camera_info.height/2.0) * (msg.point.y - latest_camera_info.height/2.0))
28  r = math.sqrt((msg.point.x - latest_image.width/2)*(msg.point.x - latest_image.width/2)+ (msg.point.y - latest_image.height/2.0) * (msg.point.y - latest_image.height/2.0))
29  phi=r/341.0
30  # x = -(msg.point.y - latest_camera_info.height/2.0) / r * math.sin(phi)
31  # y = (msg.point.x - latest_camera_info.width/2.0) / r * math.sin(phi)
32  x = -(msg.point.y - latest_image.height/2.0) / r * math.sin(phi)
33  y = (msg.point.x - latest_image.width/2.0) / r * math.sin(phi)
34  z = 1.0 * math.cos(phi)
35  pose = PoseStamped()
36  pose.pose.position.x = 0
37  pose.pose.position.y = 0
38  pose.pose.position.z = 0
39 
40  first_vec = [x, -y,-z]
41  second_vec = [1,0,0]
42  dot = np.dot(first_vec, second_vec)/(tf.transformations.vector_norm(first_vec)*tf.transformations.vector_norm(second_vec))
43  c = np.arccos(dot)
44 
45  M = tf.transformations.rotation_matrix(c,np.cross(first_vec, second_vec))
46  quat = tf.transformations.quaternion_from_matrix(M)
47  pose.pose.orientation.x = quat[0]
48  pose.pose.orientation.y = quat[1]
49  pose.pose.orientation.z = quat[2]
50  pose.pose.orientation.w = quat[3]
51 
52  pose.header.frame_id=frame_id
53  pub.publish(pose)
54 
55  point = PointStamped()
56  point.header.stamp = rospy.Time.now()
57  point.header.frame_id= frame_id
58  point.point.x = x
59  point.point.y = y
60  point.point.z = z
61  pub_p.publish(point)
62 
63 if __name__ == "__main__":
64  rospy.init_node("click_to_pose")
65  latest_image = None
66  pub = rospy.Publisher("~output", PoseStamped, queue_size=1)
67  pub_p = rospy.Publisher("~output_point", PointStamped, queue_size=1)
68  frame_id = rospy.get_param("~frame_id", "fisheye")
69  rospy.Subscriber("clicked_point", PointStamped, cloud_cb)
70  rospy.Subscriber("camera_info", CameraInfo, camera_info_cb)
71  rospy.Subscriber("image", Image, image_cb)
72  rospy.spin()


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Mon May 3 2021 03:03:27