- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的问题可能在 Stackoverflow 的某个地方,我找不到,如果是的话,你能给我链接吗。
基本上,我想编写一个可用于所有矩阵类型的方法。我的方法如下:
Mat my_func(Mat in){
Mat out(in.rows, in.cols, in.type());
for(int i =0; i < x; i++)
for(int j =0; j < x; j++)
out.at<in.type()>(i,j) = in.at<in.type()>(j,i); //this is just an example,
//consider that I need to use
//out.at<in.type()>(i,j) part
}
out.at<in.type()>(i,j)
给出错误,因为它不接受 <in.type>
, 它需要 <double>, <float>
等等
我解决了这个问题
if(in.type() == 5)
out.at<float>(i,j) = //do something for float
else if (in.type() == 6)
out.at<double>(i,j) = //do something for double
应该有更好的方法,但是没找到。我搜索了与 typedef
相关的内容但我无法从我的发现中了解太多。
请注意:in.type() = 5
表示名为 in
的 Matrix是 mat of floats
, 同样 6
是mat of doubles
.
提前致谢
最佳答案
m.at<type>(i,j)
是模板表达式。编译器必须在编译时解析类型。
所以,任何在运行时作弊的尝试,如 m1.at<m2.type()>(i,j)
注定了。
好消息是,还有模板 Mat,例如 Mat_<float>
您可以像 m(i,j)
一样访问它(没有讨厌的模板括号)。
因此,您的示例代码甚至可能如下所示:
template<class T>
Mat_<T> my_func(Mat_<T> in){
Mat_<T> out(in.cols, in.rows);
for(int i =0; i < in.cols; i++)
for(int j =0; j < in.rows; j++)
out(j,i) = in(i,j);
return out;
}
int main()
{
// called like :
Mat_<int> in;
Mat_<int> res = my_func(in);
return 0;
}
老实说,这是一种非常罕见的情况,您在编译时不知道 Mat 的类型,对于那些您仍然需要代码,如 if type==5 do_something,由 typeid 或类似索引的函数指针数组。
关于c++ - 如何在 opencv c++ 中为不同的 Matrix 类型编写方法而不明确指定 Mat 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21268869/
我在 linux 上工作。我对windows没有太多想法。 windows中文件的权限是如何组织的?我们在unix中是否有像chmod这样的api来更改权限? 最佳答案 对于 Windows,有一个名
应用程序编程接口(interface) (API) 是一组用于访问基于 Web 的软件应用程序的编程指令和标准。 如果出现 ,有人可以向我解释一下吗?谷歌地图 或 优酷 这是API哪个是softwar
我有两个应用程序,A 和 B,它们使用 android 库 C。B 有一个服务 A 想通过 C 使用,例如 在我的库中有一个类试图将它绑定(bind)到服务,
我正在正常或安全模式下启动相机应用程序,具体取决于使用我的应用程序执行的手势,但一旦用户选择应用程序并点击始终,则没有选项可以更改默认值,即使是从 Android 的设置菜单中也是如此. camera
我有一个数据集,本质上是一个稀疏二进制矩阵,表示两个集合的元素之间的关系。例如,让第一组是人(用他们的名字表示),例如像这样的东西: people = set(['john','jane','mike
何为pythonic? pythonic如果翻译成中文的话就是很python。很+名词结构的用法在中国不少,比如:很娘,很国足,很CCTV等等。 我的理解为,很+名词表达了一种特殊和强调的意味。
某些 Prolog 目标的确定性成功问题已经一次又一次地出现在 - 至少 - 以下问题: Reification of term equality/inequality Intersection an
我指的是 DateTime.TryParse(string s, out DateTime result) 重载,它尝试从字符串中解析 DateTime - 没有特定的格式正在指定。 我可以从http
2020 年 04 月 10 日,《中共中央国务院关于构建更加完善的要素市场化配置体制机制的意见》正式公布,将数据确立为五大生产要素(土地、资本、劳动力以及技术)之
有人可以解释一下 NSNotification 的 addObserver 函数中 notificationSender 的用途吗? 这是 Apple 文档的解释: notificationSende
我是一名优秀的程序员,十分优秀!