OpenCV:使用范围设置矩阵值 [英] OpenCV: set matrix values with ranges

查看:198
本文介绍了OpenCV:使用范围设置矩阵值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对视频帧使用以下代码来设置一定范围内的矩阵值:

I am using the following code for video frames to set matrix values with some range:

for ( int row = 0, i = 0; row < srcMat.rows; ++row, i ++ )
{
   uchar* p = medianMat.ptr ( row );
   for ( int col = 0, j = 0; col < srcMat.cols; ++ col, j ++ )
   {
     dstMat ( cv::Range::all(), cv::Range ( i * 5 +1, i * 5 + 4 )).setTo ( *p );
     data++;
   }
}

我需要将* p复制到dstMat的三个连续值,并在它们前后留一个零空间.也就是说,如果我的dstMat全部为零,并且* p值为"1,2,3",那么我的目标是使dstMat看起来像" 01110 02220 03330,等等. 我的问题是:一帧大约需要70到80毫秒,对于我的情况来说太慢了.我最多一帧只有30毫秒.有没有更有效的方法可以做到这一点? 此外,在"for"循环之外,"dstMat"值全为零.你有什么想法?非常感谢你.

I need to copy the *p to three consecutive values of dstMat, and left one zero space before and after them. That is, if my dstMat is all zero, and *p values are "1,2,3", then my goal is to make dstMat look like" "01110 02220 03330", etc. I hope I made this easy to understand. My problem here is: it uses about 70 to 80 milliseconds for one frame, which is too slow for my case. I at most have 30 ms for a single frame. Is there any more efficient way for doing this? Besides, the "dstMat" values are all zero outside the "for" loop. Do you have any ideas? Thank you very much.

推荐答案

类似

for ( int row = 0; row < dstMat.rows; ++row ){
   uchar* pMed = medianMat.ptr ( row );
   uchar* pDest = dstMat.ptr ( row );  // resulting image
   pDest++; // skip the first place '0'

   for ( int col = 0; col < dstMat.cols; col+=5 )    {
     // copy the value from median into the destination in 3 consecutive values
     *pDest++ = *pMed; // copy 1 to dest
     *pDest++ = *pMed; // copy 1 to dest
     *pDest++ = *pMed; // copy 1 to dest

     pDest+=2; // skip the two zeros
     pMed++; // move to the '2'
   }
}

您可以使用cv :: zeros将新的cv :: mat初始化为零

You can use cv::zeros to init a new cv::mat to zero

这篇关于OpenCV:使用范围设置矩阵值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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