使用最新的3.1系统,地平线x5板,mipi双目相机,使用以下命令获取图像
```
ros2 launch mipi_cam mipi_cam_dual_channel.launch.py \-
mipi_image_width:=1280 mipi_image_height:=640 \-
frame_id:=pcl_link \-
mipi_image_framerate:=30.0
```
通过以下命令获取/image_combine_raw的图像,
```
import rclpy-
from rclpy.node import Node-
from sensor_msgs.msg import Image-
import cv2-
import time-
import os-
import numpy as np-
from cv_bridge import CvBridge-
import shutil-
class StereoImageSubscriber(Node):-
def __init__(self):-
super().__init__(‘camera_info’)-
self.bridge = CvBridge()-
# 订阅 `/image_combine_raw` 话题-
self.subscription = self.create_subscription(-
Image,-
‘/image_combine_raw’,-
self.image_callback,-
10)-
# 保存目录-
self.save_dir = “stereo_combined_image”-
if os.path.exists(self.save_dir):-
shutil.rmtree(self.save_dir)-
os.makedirs(self.save_dir)-
# 视频保存路径-
timestamp = time.strftime(“%Y%m%d_%H%M%S”, time.localtime())-
self.video_path = os.path.join(self.save_dir, f"stereo_video_{timestamp}.avi")-
# 视频参数-
self.fps = 30 # 设置帧率-
self.frame_size = None-
self.video_writer = None-
self.fourcc = cv2.VideoWriter_fourcc(*‘FFV1’) # FFV1(无损压缩) 或 cv2.VideoWriter_fourcc(*‘MJPG’)-
def image_callback(self, msg):-
“”" 处理 ROS2 传入的 `image_combine_raw` 并保存为 AVI 视频 “”"-
w, h = msg.width, msg.height-
if msg.encoding == “nv12”:-
temp_h = int(len(msg.data) / (1.5 * w))# 计算 NV12 图像的高度-
img_data = np.frombuffer(msg.data, dtype=np.uint8)-
yuv_image = img_data.reshape((int(temp_h * 1.5), w))-
bgr_image = cv2.cvtColor(yuv_image, cv2.COLOR_YUV2BGR_NV12)-
elif msg.encoding == “mono16”:-
bgr_image = np.frombuffer(msg.data, dtype=np.uint8).reshape((h, w, 3))-
else:-
bgr_image = np.frombuffer(msg.data, dtype=np.uint8).reshape((h, w, 3))-
# 初始化视频写入器-
if self.video_writer is None:-
self.frame_size = (bgr_image.shape[1], bgr_image.shape[0])-
self.video_writer = cv2.VideoWriter(self.video_path, self.fourcc, self.fps, self.frame_size)-
# 写入视频-
self.video_writer.write(bgr_image)-
cv2.imshow(“bgr_image”, bgr_image)-
cv2.waitKey(1)-
def destroy_node(self):-
“”" 关闭视频写入器 “”"-
if self.video_writer is not None:-
self.video_writer.release()-
super().destroy_node()-
def main(args=None):-
rclpy.init(args=args)-
node = StereoImageSubscriber()-
rclpy.spin(node)-
node.destroy_node()-
rclpy.shutdown()-
cv2.destroyAllWindows()-
if __name__ == ‘__main__’:-
main()
```
以下是获取到的相机参数,1280*640分辨率下
height = 640-
width = 1280-
k = [-
476.987060546875, 0.0, 459.9617614746094,-
0.0, 423.9884948730469, 275.3126220703125,-
0.0, 0.0, 1.0-
]-
p = [-
476.987060546875, 0.0, 459.9617614746094, 33.82135389646828,-
0.0, 423.9884948730469, 275.3126220703125, 0.0,-
0.0, 0.0, 1.0, 0.0-
]
现在遇到的问题是当我直接用立体匹配算法对获取的/image_combine_raw图像进行测试效果可以,但是利用相机参数对图像进行矫正反而效果不好了(两边会包含一部分黑边),请问这个话题的图像是矫正过得吗。