You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何将MP4视频转换为ROS Bag文件用于ORB_SLAM2?

Fixing ROS Video Stream Publishing from MP4 for ORB_SLAM2

Hey there! Let's get your MP4 video streaming into ROS so you can record it to a bag for ORB_SLAM2. Since you already have a working image publisher node, we just need to tweak it to read and stream the video file instead of a single image. Here's how to do it step by step:

Step 1: Modify Your Publisher Node to Stream the MP4

Replace your existing single-image publishing code with this version that uses OpenCV to read and stream your MP4 file. It'll loop through each frame, convert it to a ROS image message, and publish it continuously:

#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <opencv2/highgui/highgui.hpp>
#include <cv_bridge/cv_bridge.h>

int main(int argc, char** argv) {
  ros::init(argc, argv, "video_stream_publisher");
  ros::NodeHandle nh;
  image_transport::ImageTransport it(nh);
  // Publish to the topic ORB_SLAM2 expects (adjust if needed)
  image_transport::Publisher pub = it.advertise("camera/image_raw", 1);

  // Replace with your actual MP4 file path
  cv::VideoCapture cap("/home/your_username/path/to/your/video.mp4");
  if (!cap.isOpened()) {
    ROS_ERROR("Failed to open the MP4 file! Double-check the file path.");
    return -1;
  }

  // Match the loop rate to the video's native FPS for smooth streaming
  double video_fps = cap.get(cv::CAP_PROP_FPS);
  ros::Rate loop_rate(video_fps);

  cv::Mat frame;
  while (nh.ok()) {
    cap >> frame;
    // Exit loop if we reach the end of the video
    if (frame.empty()) {
      ROS_INFO("Video stream finished.");
      break;
    }

    // Convert OpenCV frame to ROS image message
    sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", frame).toImageMsg();
    pub.publish(msg);

    ros::spinOnce();
    loop_rate.sleep();
  }

  cap.release();
  return 0;
}

Step 2: Update Your CMakeLists.txt for OpenCV

Make sure your package's CMakeLists.txt includes the necessary OpenCV dependencies (since we're using it to read the video):

find_package(catkin REQUIRED COMPONENTS
  roscpp
  image_transport
  cv_bridge
  sensor_msgs
)

# Add OpenCV dependency
find_package(OpenCV REQUIRED)

catkin_package(
  CATKIN_DEPENDS roscpp image_transport cv_bridge sensor_msgs
  DEPENDS OpenCV
)

include_directories(
  ${catkin_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
)

# Replace with your node's executable name and source file
add_executable(video_stream_publisher src/video_stream_publisher.cpp)
target_link_libraries(video_stream_publisher
  ${catkin_LIBRARIES}
  ${OpenCV_LIBRARIES}
)

Step 3: Build and Test the Stream

  1. Compile your package:
    catkin_make
    
  2. Refresh your ROS environment:
    source devel/setup.bash
    
  3. Run the publisher node:
    rosrun your_package_name video_stream_publisher
    
  4. Verify the stream works: Open rqt_image_view, select the /camera/image_raw topic, and you should see your video playing.

Step 4: Record the Stream to a ROS Bag

Once the stream is confirmed working, record it to a bag file for ORB_SLAM2:

rosbag record -O orb_slam_video.bag /camera/image_raw
  • -O sets the output bag filename (feel free to rename it)
  • Let the video finish streaming, then press Ctrl+C to stop recording.

Quick Notes for ORB_SLAM2 Compatibility

  • If your video is grayscale, change "bgr8" in the code to "mono8" and update ORB_SLAM2's config file to expect grayscale images.
  • Ensure the topic name in your node matches what ORB_SLAM2 is configured to listen to (default is usually /camera/image_raw).
  • Keeping the loop rate matched to the video's native FPS will help ORB_SLAM2's tracking performance.

内容的提问来源于stack exchange,提问作者bob

火山引擎 最新活动