Pelayo
March 17, 2025, 12:08pm
1
I am using the function
cv::Mat image_ocv = slMat2cvMat(image_zed);
with the goal of using the image in OpenCV, but if I try to acquire 3 channels instead of 4, the resulting image is not displayed correctly.
Mat image_zed(new_width, new_height, MAT_TYPE::U8_C3);
Mat image_zed(new_width, new_height, MAT_TYPE::U8_C4);
Is it possible that the ZED 2 camera always returns a 4-channel matrix even if I only want a 3-channel one?
Hello @Pelayo , welcome on StereoLabs forum !
If I understand well your issue, you are trying to retrieve a 3-channel image, but when you retrieve an image it is always a 4-channel one, so you cannot use it to get a 3-channel cv::Mat.
If this is right, I can answer on two points:
sl::Mat sl_mat_img;
zed.grab();
zed.retrieveImage(sl_mat_img, sl::VIEW::LEFT); // let's take the "sl::VIEW::LEFT" (4 channels)
cv::Mat image_cv = slMat2cvMat(sl_mat_img);
cv::Mat img_CV_8UC3_BGR;
cv::cvtColor(image_cv, img_CV_8UC3_BGR, cv::COLOR_BGRA2BGR);
std::cout << "image_cv: data type=" << image_cv.type() << std::endl; // Should print 24 (=CV_8UC4)
std::cout << "img_CV_8UC3_BGR: data type=" << img_CV_8UC3_BGR.type() << std::endl; // Should print 16 (=CV_8UC3)
I hope it will help you, let us know if it does not.
Best regards,
1 Like