|
| 1 | +import rospy |
| 2 | +import cv2 |
| 3 | +import numpy as np |
| 4 | +from cv_bridge import CvBridge |
| 5 | +from sensor_msgs.msg import Image, PointCloud2 |
| 6 | +from std_msgs.msg import Header |
| 7 | +import sensor_msgs.point_cloud2 as pc2 |
| 8 | + |
| 9 | + |
| 10 | +from stitch import cvtImg2PointCloudMsg, maskCamKeyArea, \ |
| 11 | + PerspectiveTransform, \ |
| 12 | + srcFrontCamListXY, dstFrontCamListXY |
| 13 | + |
| 14 | + |
| 15 | +class SubImagePubPointCloudNode: |
| 16 | + def __init__(self): |
| 17 | + rospy.init_node("image_to_pointcloud_node") |
| 18 | + |
| 19 | + self.bridge = CvBridge() |
| 20 | + self.image_sub = rospy.Subscriber("/oak/rgb/image_raw", Image, self.image_callback, queue_size=1) |
| 21 | + self.pc_pub = rospy.Publisher("/image_pointcloud", PointCloud2, queue_size=1) |
| 22 | + |
| 23 | + self.latest_msg = None |
| 24 | + |
| 25 | + # 相机参数 |
| 26 | + self.src_pts = srcFrontCamListXY |
| 27 | + self.dst_pts = dstFrontCamListXY |
| 28 | + |
| 29 | + rospy.loginfo("Image to PointCloud2 node started.") |
| 30 | + |
| 31 | + def image_callback(self, msg): |
| 32 | + try: |
| 33 | + img = self.bridge.imgmsg_to_cv2(msg, desired_encoding='bgr8') |
| 34 | + except Exception as e: |
| 35 | + rospy.logwarn(f"CV Bridge error: {e}") |
| 36 | + return |
| 37 | + |
| 38 | + masked_img = maskCamKeyArea(img, "FRONT") |
| 39 | + transformer = PerspectiveTransform(masked_img, self.src_pts.copy(), self.dst_pts.copy()) |
| 40 | + transformed_img = transformer.getTransformedImg() |
| 41 | + |
| 42 | + pc_msg = cvtImg2PointCloudMsg( |
| 43 | + transformed_img, |
| 44 | + frameId="base_link", |
| 45 | + pixelSize=0.02, |
| 46 | + stride=6, |
| 47 | + stamp = msg.header.stamp |
| 48 | + ) |
| 49 | + self.pc_pub.publish(pc_msg) |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +if __name__ == "__main__": |
| 54 | + try: |
| 55 | + node = SubImagePubPointCloudNode() |
| 56 | + rospy.spin() |
| 57 | + except rospy.ROSInterruptException: |
| 58 | + pass |
| 59 | + |
| 60 | + |
| 61 | +# TODO : try down-sample |
0 commit comments