Matlab到OpenCV的转换-优化
Matlab to OpenCV conversion - optimization?
我正在将Matlab代码转换为OpenCV"C/cpp"代码。我有一些疑虑如下。
A = [ 2; 10; 7; 1; 3; 6; 10; 10; 2; 10];
ind = [10; 5; 9; 2];
B是a的子矩阵;矩阵B的元素是在ind
中指定的位置处的A的元素。
B = [10 3; 2; 10];
在Matlab中,我只使用
B = A(ind);
在C中使用OpenCV、
for ( int i = 0; i < ind.rows; i++) {
B.at<int>(i,0) = A.at<int>(ind.at<int>(i,0), 0);
}
有没有一种不使用for
循环的方法?
如果您正在寻找一种整洁的复制方式。我建议你定义一个函数以下是在给定位置复制的类似STL的实现
#include<vector>
#include<cstdio>
using namespace std;
/**
* Copies the elements all elements of B to A at given positions.
* indexing array ranges [ind_first, ind_lat)
* e.g
* int A[]= {0, 2, 10, 7, 1, 3, 6, 10, 10, 2, 10};
* int B[] = {-1, -1, -1, -1};
* int ind[] = {10, 5, 9, 2};
* copyat(B, A, &ind[0], &ind[3]);
* // results: A:[0,2,10,7,1,-1,6,10,10,-1,-1]
*/
template<class InputIterator, class OutputIterator, class IndexIterator>
OutputIterator copyat (InputIterator src_first,
OutputIterator dest_first,
IndexIterator ind_first,
IndexIterator ind_last)
{
while(ind_first!=ind_last) {
*(dest_first + *ind_first) = *(src_first);
++ind_first;
}
return (dest_first+*ind_first);
}
int main()
{
int A[]= {0, 2, 10, 7, 1, 3, 6, 10, 10, 2, 10};
int B[] = {-1, -1, -1, -1};
int ind[] = {10, 5, 9, 2};
copyat(B, A, &ind[0], &ind[3]);
printf("A:[");
for(int i=0; i<10; i++)
printf("%d,", A[i]);
printf("%d]n",A[10]);
}
相关文章:
- 将空基类优化对象强制转换为另一种类型是否会破坏严格的别名?
- C++优化从 int 到 float 的数组转换
- 关于使用类型转换优化除法代码的问题
- SSE2优化用于从RGB565转换为RGB888(无alpha通道)
- Arm GNU编译器:通过多余的强制转换优化的三进制生成的程序集
- 现代编译器优化如何将递归转换为返回常数
- 在代码优化过程中,C++11编译器是否会在可能的情况下将局部变量转换为右值
- 在turbo c++中,可以将一个普通递归函数转换为尾递归函数来对其进行优化
- 使用SSE2优化RGB565到RGB888的转换
- 通过将"goto"转换为分支机构进行优化
- Visual Studio 编译器优化,用于 C++ 中的类型转换
- F2C 转换的代码中断,当由编译器优化时C++
- 如何优化 YUV 到 RGB 颜色转换代码
- 编译器如何优化不正确C++分层向下转换以导致真正的未定义行为
- C++转换优化
- 为什么将条件写转换为无条件写不是线程安全的优化?
- VS是否优化了对相同类型的强制转换?
- 隐式构造函数转换的编译器优化
- 优化转换算法
- Matlab到OpenCV的转换-优化