videocapture_obsensor.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * attention: Astra2 cameras currently only support Windows and Linux kernel versions no higher than 4.15, and higher versions of Linux kernel may have exceptions.
  3. */
  4. #include <opencv2/videoio.hpp>
  5. #include <opencv2/highgui.hpp>
  6. #include <opencv2/imgproc.hpp>
  7. #include <iostream>
  8. using namespace cv;
  9. int main(int argc, char** argv)
  10. {
  11. cv::CommandLineParser parser(argc, argv,
  12. "{help h ? | | help message}"
  13. "{dw | | depth width }"
  14. "{dh | | depth height }"
  15. "{df | | depth fps }"
  16. "{cw | | color width }"
  17. "{ch | | color height }"
  18. "{cf | | depth fps }"
  19. );
  20. if (parser.has("help"))
  21. {
  22. parser.printMessage();
  23. return 0;
  24. }
  25. std::vector<int> params;
  26. if (parser.has("dw"))
  27. {
  28. params.push_back(CAP_PROP_OBSENSOR_DEPTH_WIDTH);
  29. params.push_back(parser.get<int>("dw"));
  30. }
  31. if (parser.has("dh"))
  32. {
  33. params.push_back(CAP_PROP_OBSENSOR_DEPTH_HEIGHT);
  34. params.push_back(parser.get<int>("dh"));
  35. }
  36. if (parser.has("df"))
  37. {
  38. params.push_back(CAP_PROP_OBSENSOR_DEPTH_FPS);
  39. params.push_back(parser.get<int>("df"));
  40. }
  41. if (parser.has("cw"))
  42. {
  43. params.push_back(CAP_PROP_FRAME_WIDTH);
  44. params.push_back(parser.get<int>("cw"));
  45. }
  46. if (parser.has("ch"))
  47. {
  48. params.push_back(CAP_PROP_FRAME_HEIGHT);
  49. params.push_back(parser.get<int>("ch"));
  50. }
  51. if (parser.has("cf"))
  52. {
  53. params.push_back(CAP_PROP_FPS);
  54. params.push_back(parser.get<int>("cf"));
  55. }
  56. VideoCapture obsensorCapture;
  57. if (params.empty())
  58. obsensorCapture.open(0, CAP_OBSENSOR);
  59. else
  60. obsensorCapture.open(0, CAP_OBSENSOR, params);
  61. if(!obsensorCapture.isOpened()) {
  62. std::cerr << "Failed to open obsensor capture! Index out of range or no response from device";
  63. return -1;
  64. }
  65. double fx = obsensorCapture.get(CAP_PROP_OBSENSOR_INTRINSIC_FX);
  66. double fy = obsensorCapture.get(CAP_PROP_OBSENSOR_INTRINSIC_FY);
  67. double cx = obsensorCapture.get(CAP_PROP_OBSENSOR_INTRINSIC_CX);
  68. double cy = obsensorCapture.get(CAP_PROP_OBSENSOR_INTRINSIC_CY);
  69. double k1 = obsensorCapture.get(CAP_PROP_OBSENSOR_COLOR_DISTORTION_K1);
  70. double k2 = obsensorCapture.get(CAP_PROP_OBSENSOR_COLOR_DISTORTION_K2);
  71. double k3 = obsensorCapture.get(CAP_PROP_OBSENSOR_COLOR_DISTORTION_K3);
  72. double k4 = obsensorCapture.get(CAP_PROP_OBSENSOR_COLOR_DISTORTION_K4);
  73. double k5 = obsensorCapture.get(CAP_PROP_OBSENSOR_COLOR_DISTORTION_K5);
  74. double k6 = obsensorCapture.get(CAP_PROP_OBSENSOR_COLOR_DISTORTION_K6);
  75. double p1 = obsensorCapture.get(CAP_PROP_OBSENSOR_COLOR_DISTORTION_P1);
  76. double p2 = obsensorCapture.get(CAP_PROP_OBSENSOR_COLOR_DISTORTION_P1);
  77. std::cout << "obsensor camera intrinsic params: fx=" << fx << ", fy=" << fy << ", cx=" << cx << ", cy=" << cy << std::endl;
  78. std::cout << "obsensor camera distortion params: k,p=" << k1 << ", " << k2 << ", " << k3 << ", "
  79. << k4 << ", " << k5 << ", " << k6 << ", "
  80. << p1 << ", " << p2 << std::endl;
  81. Mat image;
  82. Mat depthMap;
  83. Mat adjDepthMap;
  84. // Minimum depth value
  85. const double minVal = 300;
  86. // Maximum depth value
  87. const double maxVal = 5000;
  88. while (true)
  89. {
  90. // Grab depth map like this:
  91. // obsensorCapture >> depthMap;
  92. // Another way to grab depth map (and bgr image).
  93. if (obsensorCapture.grab())
  94. {
  95. if (obsensorCapture.retrieve(image, CAP_OBSENSOR_BGR_IMAGE))
  96. {
  97. imshow("RGB", image);
  98. }
  99. if (obsensorCapture.retrieve(depthMap, CAP_OBSENSOR_DEPTH_MAP))
  100. {
  101. depthMap.convertTo(adjDepthMap, CV_8U, 255.0 / (maxVal - minVal), -minVal * 255.0 / (maxVal - minVal));
  102. applyColorMap(adjDepthMap, adjDepthMap, COLORMAP_JET);
  103. imshow("DEPTH", adjDepthMap);
  104. }
  105. // depth map overlay on bgr image
  106. static const float alpha = 0.6f;
  107. if (!image.empty() && !depthMap.empty())
  108. {
  109. depthMap.convertTo(adjDepthMap, CV_8U, 255.0 / (maxVal - minVal), -minVal * 255.0 / (maxVal - minVal));
  110. cv::resize(adjDepthMap, adjDepthMap, cv::Size(image.cols, image.rows));
  111. for (int i = 0; i < image.rows; i++)
  112. {
  113. for (int j = 0; j < image.cols; j++)
  114. {
  115. cv::Vec3b& outRgb = image.at<cv::Vec3b>(i, j);
  116. uint8_t depthValue = 255 - adjDepthMap.at<uint8_t>(i, j);
  117. if (depthValue != 0 && depthValue != 255)
  118. {
  119. outRgb[0] = (uint8_t)(outRgb[0] * (1.0f - alpha) + depthValue * alpha);
  120. outRgb[1] = (uint8_t)(outRgb[1] * (1.0f - alpha) + depthValue * alpha);
  121. outRgb[2] = (uint8_t)(outRgb[2] * (1.0f - alpha) + depthValue * alpha);
  122. }
  123. }
  124. }
  125. imshow("DepthToColor", image);
  126. }
  127. image.release();
  128. depthMap.release();
  129. }
  130. if (pollKey() >= 0)
  131. break;
  132. }
  133. return 0;
  134. }