2023-12-19 23:34:21 +08:00
|
|
|
/**
|
2024-03-29 16:23:41 +08:00
|
|
|
* 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.
|
2023-12-19 23:34:21 +08:00
|
|
|
*/
|
|
|
|
|
2022-07-26 19:35:34 +08:00
|
|
|
#include <opencv2/videoio.hpp>
|
|
|
|
#include <opencv2/highgui.hpp>
|
|
|
|
#include <opencv2/imgproc.hpp>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
VideoCapture obsensorCapture(0, CAP_OBSENSOR);
|
|
|
|
if(!obsensorCapture.isOpened()){
|
|
|
|
std::cerr << "Failed to open obsensor capture! Index out of range or no response from device";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
double fx = obsensorCapture.get(CAP_PROP_OBSENSOR_INTRINSIC_FX);
|
|
|
|
double fy = obsensorCapture.get(CAP_PROP_OBSENSOR_INTRINSIC_FY);
|
|
|
|
double cx = obsensorCapture.get(CAP_PROP_OBSENSOR_INTRINSIC_CX);
|
|
|
|
double cy = obsensorCapture.get(CAP_PROP_OBSENSOR_INTRINSIC_CY);
|
|
|
|
std::cout << "obsensor camera intrinsic params: fx=" << fx << ", fy=" << fy << ", cx=" << cx << ", cy=" << cy << std::endl;
|
2022-08-22 11:14:59 +08:00
|
|
|
|
2022-07-26 19:35:34 +08:00
|
|
|
Mat image;
|
|
|
|
Mat depthMap;
|
|
|
|
Mat adjDepthMap;
|
2023-12-19 23:34:21 +08:00
|
|
|
|
|
|
|
// Minimum depth value
|
|
|
|
const double minVal = 300;
|
|
|
|
// Maximum depth value
|
|
|
|
const double maxVal = 5000;
|
2022-07-26 19:35:34 +08:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
// Grab depth map like this:
|
|
|
|
// obsensorCapture >> depthMap;
|
|
|
|
|
|
|
|
// Another way to grab depth map (and bgr image).
|
|
|
|
if (obsensorCapture.grab())
|
|
|
|
{
|
|
|
|
if (obsensorCapture.retrieve(image, CAP_OBSENSOR_BGR_IMAGE))
|
|
|
|
{
|
|
|
|
imshow("RGB", image);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obsensorCapture.retrieve(depthMap, CAP_OBSENSOR_DEPTH_MAP))
|
|
|
|
{
|
2023-12-19 23:34:21 +08:00
|
|
|
depthMap.convertTo(adjDepthMap, CV_8U, 255.0 / (maxVal - minVal), -minVal * 255.0 / (maxVal - minVal));
|
2022-07-26 19:35:34 +08:00
|
|
|
applyColorMap(adjDepthMap, adjDepthMap, COLORMAP_JET);
|
|
|
|
imshow("DEPTH", adjDepthMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
// depth map overlay on bgr image
|
|
|
|
static const float alpha = 0.6f;
|
|
|
|
if (!image.empty() && !depthMap.empty())
|
|
|
|
{
|
2023-12-19 23:34:21 +08:00
|
|
|
depthMap.convertTo(adjDepthMap, CV_8U, 255.0 / (maxVal - minVal), -minVal * 255.0 / (maxVal - minVal));
|
2022-07-26 19:35:34 +08:00
|
|
|
cv::resize(adjDepthMap, adjDepthMap, cv::Size(image.cols, image.rows));
|
|
|
|
for (int i = 0; i < image.rows; i++)
|
|
|
|
{
|
|
|
|
for (int j = 0; j < image.cols; j++)
|
|
|
|
{
|
|
|
|
cv::Vec3b& outRgb = image.at<cv::Vec3b>(i, j);
|
|
|
|
uint8_t depthValue = 255 - adjDepthMap.at<uint8_t>(i, j);
|
|
|
|
if (depthValue != 0 && depthValue != 255)
|
|
|
|
{
|
|
|
|
outRgb[0] = (uint8_t)(outRgb[0] * (1.0f - alpha) + depthValue * alpha);
|
|
|
|
outRgb[1] = (uint8_t)(outRgb[1] * (1.0f - alpha) + depthValue * alpha);
|
|
|
|
outRgb[2] = (uint8_t)(outRgb[2] * (1.0f - alpha) + depthValue * alpha);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
imshow("DepthToColor", image);
|
|
|
|
}
|
|
|
|
image.release();
|
|
|
|
depthMap.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pollKey() >= 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
2023-12-19 23:34:21 +08:00
|
|
|
}
|