herrDeng網內搜尋

自訂搜尋

Ads

2022年12月1日 星期四

深度攝影機realsense簡易opencv C++去背

 
C++程式如下:
  1. #include <librealsense2/rs.hpp>
  2. #include <opencv2/opencv.hpp>
  3. #include <iostream>
  4. using namespace rs2;
  5. using namespace cv;
  6. using namespace std;
  7.  
  8. int main(int argc, char* argv[])
  9. try
  10. {
  11. cout << "顯示color_grab, depth影格,加上對齊修正align。\n";
  12. colorizer color_map(3);
  13. /*著色的代碼如下
  14. 0 - Jet
  15. 1 - Classic
  16. 2 - WhiteToBlack
  17. 3 - BlackToWhite
  18. 4 - Bio
  19. 5 - Cold
  20. 6 - Warm
  21. 7 - Quantized
  22. 8 - Pattern
  23. 9 - Hue
  24. */
  25. pipeline pipe;
  26. rs2::align align2color(RS2_STREAM_COLOR);
  27. config cfg;
  28. int w = 1280, h = 720, FPS = 30;
  29. cfg.enable_stream(RS2_STREAM_COLOR, w, h, RS2_FORMAT_BGR8, FPS);
  30. cfg.enable_stream(RS2_STREAM_DEPTH, w, h, RS2_FORMAT_Z16, FPS);
  31. auto profile = pipe.start(cfg);
  32.  
  33. // Camera warmup
  34. frameset data= pipe.wait_for_frames();
  35. hole_filling_filter holeFilter;//補洞filter
  36. while (1)
  37. {
  38. frameset data = pipe.wait_for_frames(); // Wait for next set of frames from the camera
  39. data = align2color.process(data);
  40. data.apply_filter(holeFilter);//補洞
  41. frame color = data.get_color_frame();
  42. frame depth = data.get_depth_frame().apply_filter(color_map);
  43.  
  44. // Create OpenCV matrix of size (w,h) from the colorized depth data
  45. Mat color_im(h, w, CV_8UC3, (void*)color.get_data());
  46. Mat depth_im(h, w, CV_8UC3, (void*)depth.get_data());
  47. GaussianBlur(depth_im, depth_im, Size(3, 3), 3, 3);
  48. dilate(depth_im, depth_im, 1);//擴張
  49. erode(depth_im, depth_im, 1);//侵蝕
  50. Mat depth2;
  51. threshold(depth_im, depth2, 120, 255, THRESH_BINARY);
  52. color_im = color_im | depth2 ;
  53.  
  54. // Update the window with new data
  55. imshow("color_grab", color_im);
  56. imshow("depth", depth_im);
  57.  
  58. if (waitKey(1)>0)break;
  59. }
  60. destroyAllWindows();
  61. pipe.stop();
  62. return 0;
  63. }
  64. catch (exception& e) {
  65. cerr << e.what();
  66. return 1;
  67. }

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章