我有一个4 x 1或3 x 1 Mat变量,表示均匀或不均匀的点。我想将这个Mat变量转换为Point2f或Point3f类型,具体取决于它是均匀的还是不均匀的。
例如,如果
Mat point = (Mat_<float>(3, 1) << 6, 4, 2);
如果这是一个同质的表示,那么转换的点是Point2f类型,并且
Point2f pt;
pt.x = 6 / 2; pt.y = 4 / 2;
如果这不是齐次表示,则转换点的类型为Point3f和
Point3f pt;
pt.x = 6; pt.y = 4; pt.z = 2;
下面是我当前的代码,我对方法的调用是
Point3f pt_3d = mat2pt<Point3f>(point, 0);
Point2f pt_2d = mat2pt<Point2f>(point, 1);
错误是no viable conversion from Point3f to Point2f
和no viable conversion from Point2f to Point3f
template<typename T>
T mat2pt(Mat mat, int t = 0)
{
if(t == 1) // homogeneous => inhomogeneous
{
if(mat.size() == Size(3, 1))
{
pt.x = mat.at<float>(0) / mat.at<float>(2);
pt.y = mat.at<float>(1) / mat.at<float>(2);
return pt;
}
else if(mat.size() == Size(4, 1))
{
Point3f pt;
pt.x = mat.at<float>(0) / mat.at<float>(3);
pt.y = mat.at<float>(1) / mat.at<float>(3);
pt.z = mat.at<float>(2) / mat.at<float>(3);
return pt;
}
else
{
cout << "incorrect Mat size" << endl;
return;
}
}
else if(t == 0)
{
if(mat.size() == Size(2, 1))
{
Point2f pt;
pt.x = mat.at<float>(0);
pt.y = mat.at<float>(1);
return pt;
}
else if(mat.size() == Size(3, 1))
{
Point3f pt;
pt.x = mat.at<float>(0);
pt.y = mat.at<float>(1);
pt.z = mat.at<float>(2);
}
else
{
cout << "incorrect Mat size" << endl;
return;
}
}
else
{
cout << "invalid value for parameter t" << endl;
}
}