从Vector <point2f> Assertion failed错误创建Mat

时间:2016-07-20 16:38:09

标签: c++ opencv

我试图将Point2f imagePoints写入openCV中的Mat图像。我正在关注以下链接。

Create Mat from vector<point2f>

但我得到'断言失败'的错误。请帮忙。

代码:

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;

错误:

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

我的图片有2个频道。所以ImWrite()抛出断言失败错误。如果不是这样的话,如何使用Image点创建Mat图像?

1 个答案:

答案 0 :(得分:1)

根据您在评论中所写的内容,您似乎正在尝试将public class DecryptActivity extends AppCompatActivity { EditText ed1; TextView tv; Button b1,b2; byte[] decrypted; String decryptedText = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.decrypt_activity); ed1 = (EditText) findViewById(R.id.decrypteditText); b1 = (Button) findViewById(R.id.decryptbutton); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d("asd","inside onclick"); try { Log.d("asd","inside try of decryptActivity"); KeyGenerator keygenerator = KeyGenerator.getInstance("Blowfish"); SecretKey secretkey = keygenerator.generateKey(); Cipher cipher2 = Cipher.getInstance("Blowfish"); cipher2.init(Cipher.DECRYPT_MODE,secretkey); Log.d("asd","in decrypt key is"+secretkey.toString()); Log.d("asd","before getBytes"); byte[] encrypted =ed1.getText().toString().getBytes(); Log.d("asd",ed1.getText().toString()); decrypted = cipher2.doFinal(encrypted); tv=(EditText)findViewById((R.id.textView)); tv.setText("Asd"); Log.d("asd",new String(decrypted)); } catch (Exception e) { } Toast.makeText(getApplicationContext(), "Text Pasted", Toast.LENGTH_SHORT).show(); } }); } 写入文件。问题是,来自Mat的{​​{1}}将提供2通道矩阵,这与任何图像格式(灰度,RGB或RGBA)都不兼容。

此外,请编辑您的主要帖子以显示代码(使用降价),以便更容易阅读,然后帮助您。