0

I was trying to write Point2f imagePoints to a Mat image in openCV. I was following the link below.

Create Mat from vector<point2f>

But I am getting 'Assertion failed' error. Please help.

Code:

std::vector<cv::Point3d> objectPoints;
std::vector<cv::Point2d> imagePoints;

cv::Mat intrisicMat(3, 3, cv::DataType<double>::type);
intrisicMat.at<double>(0, 0) = param.focalLength.first;
intrisicMat.at<double>(0, 1) = 0;
intrisicMat.at<double>(0, 2) = param.principalPoint.first;

intrisicMat.at<double>(1, 0) = 0;
intrisicMat.at<double>(1, 1) = param.focalLength.second;
intrisicMat.at<double>(1, 2) = param.principalPoint.second;

intrisicMat.at<double>(2, 0) = 0;
intrisicMat.at<double>(2, 1) = 0;
intrisicMat.at<double>(2, 2) = 1;

cv::Mat rVec(3, 1, cv::DataType<double>::type); // Rotation vector
rVec.at<double>(0) = 0;
rVec.at<double>(1) = 0;
rVec.at<double>(2) = 0;

cv::Mat tVec(3, 1, cv::DataType<double>::type); // Translation vector
tVec.at<double>(0) = 0;
tVec.at<double>(1) = 0;
tVec.at<double>(2) = 0;

cv::Mat distCoeffs(5, 1, cv::DataType<double>::type);   // Distortion vector
distCoeffs.at<double>(0) = param.distortionRadial.at(0);
distCoeffs.at<double>(1) = param.distortionRadial.at(1);
distCoeffs.at<double>(2) = param.distortionTangential.first;
distCoeffs.at<double>(3) = param.distortionTangential.second;
distCoeffs.at<double>(4) = param.distortionRadial.at(2);


projectPoints(objectPoints, rVec, tVec, intrisicMat, distCoeffs, imagePoints);
Mat depthImage = Mat(imagePoints);
imwrite("E:/softwares/1.8.0.71/bin/depthImage.jpg", depthImage);
cout << "depthImage.channels()=" << depthImage.channels() << endl;

Error:

OpenCV Error: Assertion failed (image.channels() == 1 || image.channels() == 3 || image.channels() == 4) in cv::imwrite_, file E:\softwares\opencv-3.1.0\opencv-3.1.0\modules\imgcodecs\src\loadsave.cpp, line 455

My image has 2 channels. So ImWrite() is throwing assertion failed error. How can I create a Mat image using the Image points if not like this?

Community
  • 1
  • 1
SDD123
  • 53
  • 1
  • 12
  • Show your code and full error log please. – Dainius Šaltenis Jul 20 '16 at 16:38
  • OpenCV Error: Assertion failed (image.channels() == 1 || image.channels() == 3 || image.channels() == 4) in cv::imwrite_, file E:\softwares\opencv-3.1.0\opencv-3.1.0\modules\imgcodecs\src\loadsave.cpp, line 455 – SDD123 Jul 20 '16 at 16:44
  • This is my code.. projectPoints(objectPoints, rVec, tVec, intrisicMat, distCoeffs, imagePoints); Mat pointsMat = Mat(imagePoints); imwrite("E:/softwares/1.8.0.71/bin/depthImage.png", pointsMat); – SDD123 Jul 20 '16 at 16:45

1 Answers1

1

With what you have written in the comments, it seems that you're trying to imwrite your Mat to a file. The problem is, a Mat from Vector<Point2f> will give a 2 channels matrix, which is not compatible with any image format (grayscale, RGB or RGBA).

Moreover, please edit your main post to show the code (using markdown) so it is easier to read and then help you.

Cedric
  • 889
  • 1
  • 8
  • 18
  • So how can i create a Mat image using this data? – SDD123 Jul 20 '16 at 18:03
  • Could you point me to the meaning of this 2 channels Mat? What is it suppose to show as an image? If you're just trying to save the values as a serialized Mat object, there's the cv::FileStorage class that has the purpose of saving to XML or YML format (see: http://docs.opencv.org/2.4/modules/core/doc/xml_yaml_persistence.html). – Cedric Jul 20 '16 at 18:12
  • My goal is to convert the 3D clod point into 2D image data. I want to save the 2D image to visualize the 2D depth image. – SDD123 Jul 20 '16 at 18:16
  • If I do understand what you want, the Mat object you're getting links every 3D coordinates of the 3D point cloud to a 2D coordinate in the imagePoints. Therefore, you'll want to go through every 2 channels element of imagePoints, get the (x,y) values and use these as an index on another matrix (which is a binary image (CV_8U), for example) and set the (x,y) element value as 255. – Cedric Jul 20 '16 at 18:23