我正在尝试订阅两个相机主题:“realsense_front_left/color/image_raw”主题和“realsense_front_right/color/image_raw”主题,这两个主题都是来自两个单目相机的数据。在此之后,我需要将左摄像头图像的左半部分添加到右摄像头图像的右半部分,然后发布合并后的图像。每次我尝试运行节点时,我都会收到一个 AttributeError:'Merger' 对象没有属性 'front_left_camera_image。我尝试了一些不同的东西,但我通常会遇到同样的错误。有人可以帮我吗?
# !/usr/bin/env python
import rospy
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
import cv2
import os
import numpy as np
from PIL import Image as img
bridge = CvBridge()
class Merger:
def __init__(self):
self.front_left_camera = None
self.front_right_camera = None
self.bridge = CvBridge()
# Node cycle rate (in Hz)
self.loop_rate = rospy.Rate(1)
# Publisher
self.pub = rospy.Publisher('post_merged_image', Image, queue_size = 10)
# Subscribers
rospy.Subscriber("realsense_front_left/color/image_raw", Image, self.front_left_camera_callback)
rospy.Subscriber("realsense_front_right/color/image_raw", Image, self.front_right_camera_callback)
def front_left_camera_callback(self,msg):
rospy.loginfo('Left camera Image received...')
# Extract the left camera image
self.front_left_camera_image = self.bridge.imgmsg_to_cv2(msg)
def front_right_camera_callback(self,msg):
rospy.loginfo('Right camera Image received...')
# Extract the right camera image
self.front_right_camera_image = self.bridge.imgmsg_to_cv2(msg)
def overlap(self):
if self.front_left_camera_image is not None and self.front_right_camera_image is not None:
# Merging operation here (Merge the left half of left camera image to right half of right camera image)
left_image_array = np.asarray(self.front_left_camera_image)
h_l, w_l = left_image_array[0], left_image_array[1]
right_image_array = np.asarray(self.front_right_camera_image)
h_r, w_r = right_image_array[0], right_image_array[1]
# Assusming both are equal in size (h_l = h_r and w_l = w_r)
# Slice the half images
left_half_array = left_image_array[h_l, 0:w_l/2, :]
right_half_array = right_image_array[h_r, w_r/2:w_r, :]
# concatenate the arrays
merged_array = np.concatenate(left_half_array, right_half_array, axis = 1)
# convert numpy array to image
self.merged_image = img.fromarray(merged_array)
else:
pass
def start(self):
while not rospy.is_shutdown():
rospy.loginfo("merging images")
self.overlap()
if self.merged_image is not None:
rospy.loginfo("publishing merged image")
self.pub.publish(bridge.cv2_to_imgmsg(self.merged_image))
self.loop_rate.sleep()
if __name__ == '__main__':
rospy.init_node('image_merger', anonymous=True)
my_node = Merger()
my_node.start()
`