打开CV通用Mat函数头 [英] Open CV generic Mat function headers
问题描述
在OpenCV中,通常像这样访问Mat
对象中的像素:
In OpenCV it's common to access a pixel in a Mat
object like so:
float b = A.at<float>(4,5);
问题是,如果您不知道先验数据的类型,则会遇到麻烦.有没有一种方法可以编写接受模板类型为T
的Mat
的泛型函数标头?我想构建用于线性代数计算的函数,并且我不想使用if
子句来分隔double
和float
.像这样:
The problem is, if you don't know the type of data apriori, you're stuck. Is there a way of writing generic function headers that accept a Mat
with template type T
? I'd like to build functions for linear algebra calculations, and I don't want to have an if
clause separating double
and float
. Something like:
void func(Mat <T> a) {
a.at<T>(3,4) = ...
在OpenCV中有可能吗?
Is this possible in OpenCV?
推荐答案
这可以通过简单地对您的函数进行模板操作来实现:
This is possible simply by templating your function :
template<typename T>
void func(Mat a) {
a.at<T>(3,4) = ...
但是请注意,没有简单的方法将T类型限制为double或float,并且您的算法可能不适用于其他类型,但这可能不是实际问题.
But note that you have no easy way to constraint the type T to be only double or float, and your algorithm won't probably work on other types, but it may not be an actual problem.
还要注意使用模板的缺点:使用模板的缺点是什么?
Also note the drawbacks of using templates : What are the disadvantages of using templates?
这篇关于打开CV通用Mat函数头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!