如何访问/修改OpenCV中的矩阵元素?为什么at()是模板化? [英] How to access/modify matrix element in OpenCV? Why at() is templatized?

查看:363
本文介绍了如何访问/修改OpenCV中的矩阵元素?为什么at()是模板化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该知道 Mat 元素类型在在()使用吗?例如,如果我有

Should I know Mat element type for use at() correctly? For example, if I have

Mat rose = Mat(1,180, CV_64F, 0);

那么我可以调用

rose.at<short>(i,j)++;

如果没有,那么我应该使用哪个模板参数?

If not then which template argument should I use?

为什么 Mat :: at 是模板化的,而 Mat 本身不是?

Why Mat::at is templatized while Mat itself is not?

UPDATE

此问题包含带有另一错误的示例代码,现在位于:如何在OpenCV中使用零填充Matrix?

This question contained sample code with another error, which is now here: How to fill Matrix with zeros in OpenCV?

推荐答案

正如William已经正确指出的那样,你应该只提供正确的类型作为的模板参数。我相信 cv :: Mat 本身不是为了简化而做的模板。然而,OpenCV团队正试图支持C ++的功能,包括模板。

As already properly pointed out by William, you should provide only the correct type as a template argument for at. I believe that cv::Mat itself is not made template only for simplification. However, OpenCV crew are trying to support C++ features, including templates. The interface became slightly heterogeneous in this way.

在运行时你不能从类型变量中推导出编译器类型,这是很明显的。但是,如果你现在知道你的类型变量,可以使用traits类在编译时推导出来:

You could not deduce compiler type from a type variable at runtime for obvious reason. However, you can deduce it at compile time, if your type variable is known at this point, using a traits class:

template<int I>
struct CvType {};

template<>
struct CvType<CV_64F> { typedef double type_t; };
template<>
struct CvType<CV_32F> { typedef float type_t; };
template<>
struct CvType<CV_8U> { typedef unsigned char type_t; };
// Other types go here

void main()
{
  const int type = CV_64F;
  cv::Mat mat(10, 10, type);
  mat.at<CvType<type>::type_t>(1, 1);
}

在这种情况下,您可以更改类型的值,并且不需要手动更改或其他方法调用中的所有类型。

In this case you can change the value of type and wouldn't need to change types manually for all the at or other methods calls.

这篇关于如何访问/修改OpenCV中的矩阵元素?为什么at()是模板化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆