How to save image obtained by the ZED2 using OpenCV

I have been learning the SDK recently.
When I study the sample/camera_control chapter, I want to save the image obtained by the ZED2 left camera.
But there is something went wrong(see the Pic below). I don’t know why, and I am about to crazy.

int main()
Camera zed;
InitParameters initParameters;
initParameters.sdk_verbose = true;
initParameters.camera_resolution = RESOLUTION::HD1080;
initParameters.depth_mode = DEPTH_MODE::NONE;

auto returnedState =;
if (returnedState != ERROR_CODE::SUCCESS) {
	cout << "Camera Open " << returnedState << " Exit program.";

Mat zedImg;
returnedState = zed.grab();
if (returnedState == ERROR_CODE::SUCCESS)
	zed.retrieveImage(zedImg, VIEW::LEFT);
	cv::Mat cvImage = cv::Mat((int)zedImg.getHeight(), (int)zedImg.getWidth(), CV_8UC4, zedImg.getPtr<sl::uchar1>(MEM::CPU));
	cv::Mat cvtImage((int)zedImg.getHeight(), (int)zedImg.getWidth(), CV_8UC3);
	cv::cvtColor(cvImage, cvtImage, cv::COLOR_BGRA2BGR);
	cv::Mat cvTest((int)zedImg.getHeight(), (int)zedImg.getWidth(), CV_8UC1);
	cv::cvtColor(cvtImage, cvTest, cv::COLOR_BGR2GRAY);
	cout << cvTest.type() << endl;
	cv::imwrite("test.png", cvTest); //cannot save the image

Is this a compiler error? What does it say?

1 Like