使用realsense d415进行拍照,延迟,to_string,string,开机自启
最近有個老師讓我做一下用深度相機拍照,然后合計學一下realsense d415。
這個網站有視頻教程:https://www.intelrealsense.com/zh-hans/videos-and-tutorials/
在linux上安裝:搜索librealsense,我安裝的是源碼,然后我編譯的。
這個網站是https://dev.intelrealsense.com/docs/code-samples有案例的代碼
找到連opencv那個,然后我們寫一下cmakelist.txt
cmake_minimum_required(VERSION 3.0) project(paizhao) find_package(OpenCV REQUIRED) include_directories(${OpenCV_INCLUDE_DIRS} ) include_directories("/usr/local/inlude/") add_executable(paizhao paizhao.cpp) link_directories(paizhao "/usr/local/lib/") target_link_libraries(paizhao ${OpenCV_LIBS}) target_link_libraries(paizhao /usr/local/lib/librealsense2.so)然后看一下代碼,我們之后就在上面二次開發:
實例個顏色圖對象,實例管道對象,然后建立個窗口,然后加個等待,之后獲取深度圖,這行重點,同時應用顏色圖,然后設置圖片大小,之后建立圖片,然后我們就加幾行代碼就行。
#include <librealsense2/rs.hpp> // Include RealSense Cross Platform API #include <opencv2/opencv.hpp> // Include OpenCV APIusing namespace cv;int main(int argc, char * argv[]) try {// Declare depth colorizer for pretty visualization of depth datars2::colorizer color_map;// Declare RealSense pipeline, encapsulating the actual device and sensorsrs2::pipeline pipe;// Start streaming with default recommended configurationpipe.start();const auto window_name = "Display Image";namedWindow(window_name, WINDOW_AUTOSIZE);while (waitKey(1) < 0 && getWindowProperty(window_name, WND_PROP_AUTOSIZE) >= 0){rs2::frameset data = pipe.wait_for_frames(); // Wait for next set of frames from the camerars2::frame depth = data.get_depth_frame().apply_filter(color_map);// Query frame size (width and height)const int w = depth.as<rs2::video_frame>().get_width();const int h = depth.as<rs2::video_frame>().get_height();// Create OpenCV matrix of size (w,h) from the colorized depth dataMat image(Size(w, h), CV_8UC3, (void*)depth.get_data(), Mat::AUTO_STEP);// Update the window with new dataimshow(window_name, image);}return EXIT_SUCCESS; } catch (const rs2::error & e) {std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl;return EXIT_FAILURE; } catch (const std::exception& e) {std::cerr << e.what() << std::endl;return EXIT_FAILURE; }首先我們先看一下保存圖片的函數。
imwrite(name,image);然后我們要設置一下名稱,這時我們學一下string這個庫。這個例子就可以了
#include <iostream> #include <string> using namespace std; int main(){string s1;string s2 = "c plus plus";string s3 = s2;string s4 (5, 's');return 0; }然后還要看一下這個函數:to_string
for(int i=0;i<;i++){s2 = to_string(i);name = s1 + s2+s3;imwrite(name,image); }最后加個延時就ok了:
在linux下?
#include <unistd.h>? sleep(5)//延遲5秒?如果你想延遲一秒以內?
#include <ctime>? void ? Delay(int ? time)//time*1000為秒數? {? clock_t ? now ? = ? clock();?while( ? clock() ? - ? now ? < ? time ? );? }?附上完整代碼:
#include <librealsense2/rs.hpp> // Include RealSense Cross Platform API #include <opencv2/opencv.hpp> // Include OpenCV API #include<iostream> #include"string.h" #include "unistd.h"using namespace cv; using namespace std;string s1 = "/home/nx/圖片/車道數據集/"; string s2,name; string s3 = ".png";int main(int argc, char * argv[]) try {// Declare depth colorizer for pretty visualization of depth datars2::colorizer color_map;// Declare RealSense pipeline, encapsulating the actual device and sensorsrs2::pipeline pipe;// Start streaming with default recommended configurationpipe.start();const auto window_name = "Display Image";namedWindow(window_name, WINDOW_AUTOSIZE);while (waitKey(1) < 0 && getWindowProperty(window_name, WND_PROP_AUTOSIZE) >= 0){rs2::frameset data = pipe.wait_for_frames(); // Wait for next set of frames from the camerars2::frame depth = data.get_depth_frame().apply_filter(color_map);// Query frame size (width and height)const int w = depth.as<rs2::video_frame>().get_width();const int h = depth.as<rs2::video_frame>().get_height();// Create OpenCV matrix of size (w,h) from the colorized depth dataMat image(Size(w, h), CV_8UC3, (void*)depth.get_data(), Mat::AUTO_STEP);// Update the window with new data//imshow(window_name, image);for(int i=1;i<10000;i++){s2 = to_string(i);name = s1 + s2+s3;imwrite(name,image);sleep(1);}}return EXIT_SUCCESS; } catch (const rs2::error & e) {std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl;return EXIT_FAILURE; } catch (const std::exception& e) {std::cerr << e.what() << std::endl;return EXIT_FAILURE; }最后就是開機自啟了:這里我們要用到的就是rc.local腳本,這個是在開機時會執行的,然后我們在倒數第二行就是?exit 0之前寫上命令行命令就可以了。
參考:https://www.cnblogs.com/EasonJim/p/7573292.html
輸入這個命令
sudo vim /etc/rc.local然后在倒數第二行:/home/nx/文檔/paizhao/paizhao就可以了,搞定!后來發現這樣不行,要加兩行:先cd,在./在exit 0前。
其實我試了很多方法最后就這種方法成功了!
還有我把librealreason的那個上傳到了我的網盤上了,想要的留言。
然后我后來想拍rgb的了,但是后來發現寫在一起不好使,然后就想執行兩個(順序),然后:https://blog.csdn.net/x13163303344/article/details/89390144
發現不太行,最后就是在兩個應用程序之間加個&就可以了
?
?
?
?
?
總結
以上是生活随笔為你收集整理的使用realsense d415进行拍照,延迟,to_string,string,开机自启的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 关键字 desc_mysql
- 下一篇: iTunes导入歌曲和铃声到iphone