简体   繁体   English

为什么向量cv :: mat总是返回第一个图像?

[英]why a vector of cv::mat always return the first image?

I'm reading video frames from a webcam and storing them in a std::vector < cv::mat > variable. 我正在从网络摄像头读取视频帧,并将其存储在std :: vector <cv :: mat>变量中。

Each time I want to calculate the frame difference between two consecutive frames, I use the following code but the result is always zero and I get a zero matrix !! 每次我想计算两个连续帧之间的帧差时,我都使用以下代码,但结果始终为零,并且得到零矩阵! I have two threads in my program, one for reading video frames and one for processing the video frames. 我的程序中有两个线程,一个用于读取视频帧,一个用于处理视频帧。 whenever I'm going to write into the vector I use a mutex in order to prevent any further problems in pushing back or removing frames. 每当我要写入向量时,我都会使用互斥量,以防止在推回或删除帧时出现任何其他问题。

Here is my pseudo code : 这是我的伪代码:

std::vector<cv::Mat> IMAGE_VEC;
Qmutex IMAGE_VEC_MUTEX;
Main_Thread()
{

while(1)
{
 cv::Mat Frame,Reserved_Frame;
 Camera.read(Frame);
 Frame.copyTo(Reserved_Frame);
 QMutexLocker Locker(&IMAGE_VEC_MUTEX);
 IMAGE_VEC.pushback(Reserved_Frame);
 Locker.unlock();
 cv::imshow("Frame",Frame);
 cv::waitKey();
}
}

And My process Thread is: 我的进程线程是:

Process_Thread()
{

  for (; IMAGE_VEC.size() > 1 ;)
{
      cv::Mat frame1;
      IMAGE_VEC[0].copyTo(frame1);
      cv::Mat frame2;
      IMAGE_VEC[1].copyTo(frame2);
      cv::subtract(frame1,frame2,result);
      QMutexLocker Locker(&IMAGE_VEC_MUTEX);
      IMAGE_VEC[0].release();
      //qDebug()<<"IMAGE_VEC Step2 size  is: "<<IMAGE_VEC.size()<<"\n";
      IMAGE_VEC.erase(IMAGE_VEC.begin());
      Locker.unlock();
}
}

I calculated the mean and standard deviation of each frame in Process thread and they were always the same ! 我计算了Process线程中每帧的平均值和标准偏差,它们始终相同! I could solve this problem by changing the thread process code to this code 我可以通过将线程处理代码更改为此代码来解决此问题

 Process_Thread()
{

  for (; IMAGE_VEC.size() > 1 ;)
{
      cv::Mat frame1;
      IMAGE_VEC[0].copyTo(frame1);
      QMutexLocker Locker(&IMAGE_VEC_MUTEX);
      IMAGE_VEC[0].release();
      //qDebug()<<"IMAGE_VEC Step2 size  is: "<<IMAGE_VEC.size()<<"\n";
      IMAGE_VEC.erase(IMAGE_VEC.begin());
      Locker.unlock();
      delay_ms(1000); // more than 1 second delay !!!
      cv::Mat frame2;
      IMAGE_VEC[0].copyTo(frame2);
      cv::subtract(frame1,frame2,result);

}
}

why this happens ? 为什么会这样? why a delay more than 1 second should be used to make this code work ? 为什么要使用超过1秒的延迟才能使此代码正常工作? I tried it with a delay less than 1 second and the result were as previous step ... 我尝试了不到1秒的延迟,结果如上一步所示...

I'd appreciate your comments on this. 非常感谢您对此发表评论。

You may need to copy or clone your frame to another Mat, then push to your vector, change your code like 您可能需要将框架复制或克隆到另一个Mat,然后推入向量,更改代码,例如

 cv::Mat Frame;
 Camera.read(Frame);

 Mat tmp=Frame.clone(); //clone frame to new Mat
 IMAGE_VEC.pushback(tmp);

Otherwise you are passing same pointer on each pushback. 否则,您将在每次推回传递相同的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM