Skip to main content

Retrieving Depth Grayscale Images

Application Introduction​

This application utilizes the MPSizectorS_SDK library to process and generate depth images from MPDat formatted data from a SizectorS 3D camera.

Application Process​

  • Reference OpenCV and SizectorS 3D camera related headers and libraries;

  • Use the MPSizectorS_Utils::Load() function to load mpdat-float data;

  • Convert the data to MPSizectorS_DataFrameFloat3DStruct type using MPSizectorS_Utils::ConvertToFloat3DFrame(), facilitating depth value computations;

  • Retrieve the depth information of the image using the MPSizectorS_Utils::GetAutoColorDepthRange_Float3D() function;

  • Modify each pixel's value, normalizing the depth values to a 0~255 range, and store these in the depthImg matrix;

  • Use OpenCV's imshow() function to display the depth image and the imwrite() function to save the depth image;

  • Use the waitKey() function to wait for a key press and exit.

Code Invocation​

This application mainly includes the following functions:

Loading Data​

This step involves loading an mpdat-float formatted data file and reading its data, converting it to the MPSizectorS_DataFrameUndefinedStruct type.

MPSizectorS_DataFrameUndefinedStruct data;
bool dev = MPSizectorS_Utils::Load(&data, "E:/SizectorS_SDK_Gray/test/SizectorS_DataExport5.mpdat");

Here, &data is a pointer variable to the MPSizectorS_DataFrameUndefinedStruct type.

Format Conversion​

This step converts the MPSizectorS_DataFrameUndefinedStruct type data into MPSizectorS_DataFrameFloat3DStruct type data, facilitating subsequent depth value computation operations.

MPSizectorS_DataFrameFloat3DStruct float3DData = MPSizectorS_Utils::ConvertToFloat3DFrame(data);

Retrieving Image Depth​

This step retrieves the depth information of the image, returning a MPSizectorS_ColorDepthRangeStruct structure type data.

MPSizectorS_ColorDepthRangeStruct colorDepth;
MPSizectorS_Utils::GetAutoColorDepthRange_Float3D(&colorDepth, data.FrameInfo, float3DData.Data);

Generating Depth Image​

// Create a matrix of the same size as the original image
cv::Mat depthImg(yPixResolution, xPixResolution, CV_8UC1);
// Modify the value of each pixel in the matrix
for (int i = 0; i < yPixResolution; i++){
for (int j = 0; j < xPixResolution; j++){
MPSizectorS_DataPointFloat3DStruct float3DStruct = float3DData.Data[j + i * float3DData.FrameInfo.DataInfo.XPixResolution];
if ((float3DStruct.Mask & 0x7) == 0) {
depthImg.at<uchar>(i, j) = (float3DStruct.Z - colorDepth.Min) / (colorDepth.Max - colorDepth.Min) * 255;
}
else {
depthImg.at<uchar>(i, j) = 0;
}
}
}

In this step, each pixel in the depthImg matrix is traversed, then its corresponding three-dimensional coordinate point's depth value is obtained, normalized to a grayscale value, and stored in the depthImg matrix.

Saving Depth Image​

cv::imwrite("depthImg.png", depthImg);

Displaying Depth Image​

cv::namedWindow("depthImg", cv::WINDOW_NORMAL);
imshow("depthImg", depthImg);
cv::resizeWindow("depthImg", 800, 600);
cv::waitKey();

Here, the namedWindow() function is used to create a window and specify the window name, then the imshow() function displays the depth image in this window.

Conclusion​

This application achieves the conversion of SizectorS 3D camera data into depth images and the display and storage of these depth images.