我正在编写一个函数,在其中需要访问的元素Mat
,但是该函数可以接收Mat
不同的类型。所以,如果我有:
filtered.at<TypeofMat>(i) = (typeofmat) somevalue;
我只是想做这样的事情filtered.at<myMat.type()>(i)
,但显然这是行不通的,因为type
返回一个int
我被困住了,有人可以给我点灯吗?
您可以将源矩阵转换为double(type CV_64F
)矩阵。这样,您就不会因为强制转换而丢失任何数据。然后,由于知道矩阵的类型,因此可以照常处理此矩阵。最后一步是将目标图像转换回源类型。
但是,您需要知道矩阵的通道数。ACV_assert
将确保您使用的是正确的类型。
#include <opencv2/opencv.hpp>
using namespace cv;
void foo(const Mat& src, Mat& dst)
{
// Assert number of channels
CV_Assert(src.channels() == 3);
// Convert to CV64F
Mat3d _src, _dst;
src.convertTo(_src, CV_64F);
_dst.create(_src.size());
// Work on _src and _dst (you know the type)
_dst(0,0) = _src(0,0) + Vec3d(1,2,3);
// Convert _dst to src type
_dst.convertTo(dst, src.type());
}
int main()
{
Mat3b img(10,10,Vec3b(0,0,0));
Mat res;
foo(img, res);
// res will be CV_8UC3
return 0;
}
此方法也有替代方法:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句