videocapture_obsensor.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include <opencv2/videoio.hpp>
  2. #include <opencv2/highgui.hpp>
  3. #include <opencv2/imgproc.hpp>
  4. #include <iostream>
  5. using namespace cv;
  6. int main()
  7. {
  8. VideoCapture obsensorCapture(0, CAP_OBSENSOR);
  9. if(!obsensorCapture.isOpened()){
  10. std::cerr << "Failed to open obsensor capture! Index out of range or no response from device";
  11. return -1;
  12. }
  13. double fx = obsensorCapture.get(CAP_PROP_OBSENSOR_INTRINSIC_FX);
  14. double fy = obsensorCapture.get(CAP_PROP_OBSENSOR_INTRINSIC_FY);
  15. double cx = obsensorCapture.get(CAP_PROP_OBSENSOR_INTRINSIC_CX);
  16. double cy = obsensorCapture.get(CAP_PROP_OBSENSOR_INTRINSIC_CY);
  17. std::cout << "obsensor camera intrinsic params: fx=" << fx << ", fy=" << fy << ", cx=" << cx << ", cy=" << cy << std::endl;
  18. Mat image;
  19. Mat depthMap;
  20. Mat adjDepthMap;
  21. while (true)
  22. {
  23. // Grab depth map like this:
  24. // obsensorCapture >> depthMap;
  25. // Another way to grab depth map (and bgr image).
  26. if (obsensorCapture.grab())
  27. {
  28. if (obsensorCapture.retrieve(image, CAP_OBSENSOR_BGR_IMAGE))
  29. {
  30. imshow("RGB", image);
  31. }
  32. if (obsensorCapture.retrieve(depthMap, CAP_OBSENSOR_DEPTH_MAP))
  33. {
  34. normalize(depthMap, adjDepthMap, 0, 255, NORM_MINMAX, CV_8UC1);
  35. applyColorMap(adjDepthMap, adjDepthMap, COLORMAP_JET);
  36. imshow("DEPTH", adjDepthMap);
  37. }
  38. // depth map overlay on bgr image
  39. static const float alpha = 0.6f;
  40. if (!image.empty() && !depthMap.empty())
  41. {
  42. normalize(depthMap, adjDepthMap, 0, 255, NORM_MINMAX, CV_8UC1);
  43. cv::resize(adjDepthMap, adjDepthMap, cv::Size(image.cols, image.rows));
  44. for (int i = 0; i < image.rows; i++)
  45. {
  46. for (int j = 0; j < image.cols; j++)
  47. {
  48. cv::Vec3b& outRgb = image.at<cv::Vec3b>(i, j);
  49. uint8_t depthValue = 255 - adjDepthMap.at<uint8_t>(i, j);
  50. if (depthValue != 0 && depthValue != 255)
  51. {
  52. outRgb[0] = (uint8_t)(outRgb[0] * (1.0f - alpha) + depthValue * alpha);
  53. outRgb[1] = (uint8_t)(outRgb[1] * (1.0f - alpha) + depthValue * alpha);
  54. outRgb[2] = (uint8_t)(outRgb[2] * (1.0f - alpha) + depthValue * alpha);
  55. }
  56. }
  57. }
  58. imshow("DepthToColor", image);
  59. }
  60. image.release();
  61. depthMap.release();
  62. }
  63. if (pollKey() >= 0)
  64. break;
  65. }
  66. return 0;
  67. }