如何使用opencv中的UpdateMotionhistory函数修复此错误

How can i fix this error with my UpdateMotionhistory function in opencv?

本文关键字:错误 函数 UpdateMotionhistory 何使用 opencv 中的      更新时间:2023-10-16

我了解了opencv中的updatemotionhistory函数,并想过尝试一下。但由于某种原因,我收到了这个错误"opencv错误:错误标志(参数或结构字段)(无法识别或不支持ed数组类型),文件C:\builds\2_4_PackSlave-win64-vc11-shared\opencv\modules\core\src\array.cpp,第2482行。

我注意到这个错误发生在我实现updatemotionhistory函数之后,这让我认为我可能在updatemotionhistory函数中输入参数时犯了错误。

这是我的代码:-

Mat frame,dst,tst;
double timestamp = (double)clock()/CLOCKS_PER_SEC; // I found this in the Opencv/samples  motempl.cpp
double duration = 1; //same as above , found this value in the opencv/samples/motempl.cpp
videocapture cap(0);
while(1)
    {
        cap.read(frame);
        cvtColor(frame,frame,CV_BGR2GRAY);
        cap.read(dst);
        cvtColor(dst,dst,CV_BGR2GRAY);
        absdiff(frame,dst,frame2);
        imshow("absolute frame difference",frame2);
        threshold(frame2,frame2,60,255,THRESH_BINARY);
        imshow("threshold",frame2);
        updateMotionHistory(frame2,tst,cap_timestamp,duration);
           waitkey(30);

}

我有两个问题:-如何准确地获得updatemotionhistory函数的时间戳值和持续时间值?我已经了解到,我们可以将时间戳值设置为(int cap_timestamp=cap.get(CV_cap_PROP_POS_MSEC),以及如何设置持续时间值?

让我们看看文档:

void updateMotionHistory(InputArray silhouette, 
                         InputOutputArray mhi, 
                         double timestamp, 
                         double duration)

mhi

单通道、32位浮点

因此,用CV_32FC1声明您的tst,例如Mat tst(frameHeight, frameWidth CV_32FC1);,可以解决OpenCV错误。

timestampduration定义了应用程序的"历史记录"。timestamp的意思是"现在",这是一个随着时间的推移而增长的数字;duration表示"要存储在mhi(运动历史图像)中的历史长度",这是一个常数。

让我们来看一个简单的一维移动球(B):

   B
---|---|---|---|---|--->
   0   1   2   3   4    x

假设球每次向右移动一个。在时间1,球在:

       B
---|---|---|---|---|--->
   0   1   2   3   4    x

时间(stamp)1的剪影(absdiff,与您的代码中相同)为[True,True,False,False,False]。假设mhi的初始值为[0,0,0,0],duration为2:

timestamp     silhouette             mhi
 1          [T, T, F, F, F]  -->  [1, 1, 0, 0, 0]
 2          [F, T, T, F, F]  -->  [1, 2, 2, 0, 0]
 3          [F, F, T, T, F]  -->  [1, 2, 3, 3, 0]
 4          [F, F, F, T, T]  -->  [0, 2, 3, 4, 4]
 ...

CCD_ 11存储时间戳的历史。例如,mhi[x] == t表示"当时间戳为t时,球在位置x移动。"

根据您在应用程序中对"历史"的定义,您可以定义自己的timestampduration。如果你不知道如何确定这两个参数,这里有两个简单的例子:

  • timestamp = clock(); duration = 5000 ms;使用实时作为时间戳,存储最后5秒的历史记录
  • timestamp = frameNumber(); duration = 50 frames;使用视频帧号作为时间戳,存储最后50帧的历史。frameNumber()只是在while(1)循环中返回timestamp + 1.0