【发布时间】:2014-08-02 05:59:42
【问题描述】:
我想根据matB 的值将matA 的每个像素分配给某个值,我的代码是一个嵌套的for循环:
clock_t begint=clock();
for(size_t i=0; i<depthImg.rows; i++){
for(size_t j=0; j<depthImg.cols; j++){
datatype px=depthImg.at<datatype>(i, j);
if(px==0)
depthImg.at<datatype>(i, j)=lastDepthImg.at<datatype>(i, j);
}
}
cout<<"~~~~~~~~time: "<<clock()-begint<<endl;
对于尺寸为 640*480 的垫子,大约需要 40~70 毫秒。
我可以在 python numpy 中使用精美的索引轻松做到这一点:
In [18]: b=np.vstack((np.ones(3), np.arange(3)))
In [19]: b
Out[19]:
array([[ 1., 1., 1.],
[ 0., 1., 2.]])
In [22]: a=np.vstack((np.arange(3), np.zeros(3)))
In [23]: a=np.tile(a, (320, 160))
In [24]: a.shape
Out[24]: (640, 480)
In [25]: b=np.tile(b, (320, 160))
In [26]: %timeit a[a==0]=b[a==0]
100 loops, best of 3: 2.81 ms per loop
这比我手写for循环要快得多。
那么opencv c++ api中有这样的操作吗?
【问题讨论】:
-
您是否在优化发布模式下编译?那么应该很快。
-
这里是不同openCV访问方式的比较:docs.opencv.org/doc/tutorials/core/how_to_scan_images/…