您好,登錄后才能下訂單哦!
這篇文章主要介紹了C++基于ros怎么將文件夾中的圖像轉換為bag包的相關知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇C++基于ros怎么將文件夾中的圖像轉換為bag包文章都會有所收獲,下面我們一起來看看吧。
創(chuàng)建完成后,文件夾的格式為:
將上圖中的,CMakeLists.txt文件中的內(nèi)容,替換為下面的內(nèi)容
cmake_minimum_required(VERSION 3.0.2) project(create_bag) ## Compile as C++11, supported in ROS Kinetic and newer # add_compile_options(-std=c++11) ## Find catkin macros and libraries ## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz) ## is used, also find other catkin packages # 尋找OpenCV庫 find_package( OpenCV REQUIRED ) # 添加頭文件 include_directories( ${OpenCV_INCLUDE_DIRS} ) find_package(catkin REQUIRED COMPONENTS cv_bridge rosbag roscpp rospy std_msgs ) catkin_package( # INCLUDE_DIRS include # LIBRARIES imgtobag # CATKIN_DEPENDS cv_bridge rosbag roscpp rospy std_msgs # DEPENDS system_lib ) ########### ## Build ## ########### ## Specify additional locations of header files ## Your package locations should be listed before other locations include_directories( # include ${catkin_INCLUDE_DIRS} ${catkin_INCLUDE_DIRS} ${rosbag_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS} ) add_executable(node src/torosbag.cpp) target_link_libraries(node ${catkin_LIBRARIES} ${PCL_LIBRARIES} ${rosbag_LIBRARIES} ${OpenCV_LIBS} )
leedarson@leedarson-desktop:~/catkin_ws/src/create_bag/src$ touch torosbag.cpp
創(chuàng)建一個cpp的文件夾,cpp文件中的內(nèi)容為:
#include <string> #include <ros/console.h> #include <rosbag/bag.h> #include <cv_bridge/cv_bridge.h> #include <iostream> #include <vector> #include <sys/types.h> #include <dirent.h> #include <unistd.h> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> using namespace std; using namespace cv; void GetFileNames(string path,vector<string>& filenames, string con); void GetFileNamesByGlob(cv::String path,vector<cv::String>& filenames, string con); bool read_images(string path, vector<string> &image_files); int main(int argc, char **argv) { //輸入文件和輸出文件路徑 string base_dir = "/home/leedarson/catkin_ws/src/create_bag/data/"; string img_dir = base_dir + "img/"; std::cout<<"image path is"<<img_dir<<std::endl; string output_bag=base_dir +"Human2.bag"; string img_format = ".jpg";//格式 vector<string> img_names; //GetFileNames(img_dir, img_names,".jpg"); read_images(img_dir, img_names); cout<<"圖片讀取完成"<<endl; cout<<"----"<<endl; ros::Time::init(); rosbag::Bag bag; bag.open(output_bag, rosbag::bagmode::Write); int seq = 0; vector<string>::iterator it; for(it = img_names.begin(); it != img_names.end();it++)//todo 之后改成圖片數(shù)量的多少 { string tmp = *it; std::cout<<"tmp path is"<<tmp<<std::endl; //cout<<tmp<<endl; //string strImgFile = img_dir + tmp + img_format; string strImgFile = tmp; usleep(200000);//4hz ros::Time timestamp_ros = ros::Time::now(); // --- for image ---// cv::Mat img = cv::imread(strImgFile); if (img.empty()) cout<<"圖片為空: "<<strImgFile<<endl; cv_bridge::CvImage ros_image; sensor_msgs::ImagePtr ros_image_msg; ros_image.image = img; ros_image.encoding = "bgr8"; //cout<<"debug_______"<<endl; //ros::Time timestamp_ros2 = ros::Time::now(); ros_image_msg = ros_image.toImageMsg(); ros_image_msg->header.seq = seq; ros_image_msg->header.stamp = timestamp_ros; ros_image_msg->header.frame_id = "/image_raw"; bag.write("/camera/color/image_raw", ros_image_msg->header.stamp, ros_image_msg); cout<<"write frame: "<<seq<<endl; seq++; } cout<<"---end---"<<endl; return 0; } //con:文件格式 form:文件命名形式 void GetFileNames(string path,vector<string>& filenames, string con) { DIR *pDir; struct dirent* ptr; string filename, format, name, name2; if(!(pDir = opendir(path.c_str()))) return; int num=0; while((ptr = readdir(pDir))!=0) { //跳過.和..文件 if(strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) continue; filename = ptr->d_name; format = filename.substr(filename.find("."), filename.length()); //name = filename.substr(0, filename.find(".")); name = filename.substr(0, filename.find(".")); cout<<filename<<"\t"<<name<<"\t"<<format<<endl; if(format == con)//也可以添加對文件名的要求 { filenames.push_back(name); num++; } } std::cout<<"file size of:"<<filenames.size()<<"****"<<num<<std::endl; closedir(pDir); } //cv::glob(路徑,要放置路徑下文件定義的容器,false) /*find_first_of()和find_last_of() 執(zhí)行簡單的模式匹配,如在字符串中查找單個字符c:函數(shù)find_first_of() 查找在字符串中第1個出現(xiàn)的字符c,而函數(shù)find_last_of()查找最后一個出現(xiàn)的c。匹配的位置是返回值。如果沒有匹配發(fā)生,則函數(shù)返回-1*/ //復制子字符串substr(所需的子字符串的起始位置,默認值為0 , 復制的字符數(shù)目)返回值:一個子字符串,從其指定的位置開始 //按圖片名升序排列 bool read_images(string path, vector<string> &image_files) { //fn存儲path目錄下所有文件的路徑名稱,如../images/0001.png vector<cv::String> fn; cv::glob(path, fn, false); size_t count_1 = fn.size(); if (count_1 == 0) { cout << "file " << path << " not exits"<<endl; return -1; } //v1用來存儲只剩數(shù)字的字符串 vector<string> v1; for (int i = 0; i < count_1; ++i) { //cout << fn[i] << endl; //1.獲取不帶路徑的文件名,000001.jpg(獲取最后一個/后面的字符串) string::size_type iPos = fn[i].find_last_of('/') + 1; string filename = fn[i].substr(iPos, fn[i].length() - iPos); //cout << filename << endl; //2.獲取不帶后綴的文件名,000001 string name = filename.substr(0, filename.rfind(".")); //cout << name << endl; v1.push_back(name); } //把v1升序排列 sort(v1.begin(), v1.end(),[](string a, string b) {return stoi(a) < stoi(b); }); string v = ".jpg"; size_t count_2 = v1.size(); for(int j = 0; j < count_2; ++j) { string z = path + v1[j] + v; image_files.push_back(z);//把完整的圖片名寫回來 } return true; }
1,首先打開一個終端,輸入roscore,啟動ros
2,打開新的終端,進入工作空間,執(zhí)行以下語句
leedarson@leedarson-desktop:~/catkin_ws$ source devel/setup.bash
leedarson@leedarson-desktop:~/catkin_ws$ rosrun create_bag node
通過以上操作就可以將文件夾中的圖像轉換為bag包。
rostopic list
rostopic echo
關于“C++基于ros怎么將文件夾中的圖像轉換為bag包”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“C++基于ros怎么將文件夾中的圖像轉換為bag包”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。