今日做跟踪,每次要处理两帧图片,处理完要将curFrame复制给preFrame,然后再读一幅新图片。
以下测试了3种方式数据拷贝的效率,详见代码。
int main(int argc, char* argv[])
{
Mat preFrame;
Mat curFrame;
preFrame = imread("E:\\001.bmp");
curFrame = imread("E:\\005.jpg");
cout<<"========直接用swap=========\n";
int64 st1,et1;
st1 = cvGetTickCount();
for (int i=0;i<1001;i++)
{
swap(preFrame,curFrame);
}
et1 = cvGetTickCount();
printf("swap 1000 times cost: %f milliseconds\n\n", (et1-st1)/(double)cvGetTickFrequency()/1000.0);
imshow("frame 1",preFrame);
imshow("frame 2",curFrame);
cvWaitKey(-1);
cout<<"========通过第三者交换=========\n";
Mat tmp;
st1 = cvGetTickCount();
for (int i=0;i<1001;i++)
{
tmp = preFrame;
preFrame = curFrame;
curFrame = tmp;
}
et1 = cvGetTickCount();
printf("swap 1000 times cost: %f milliseconds\n\n", (et1-st1)/(double)cvGetTickFrequency()/1000.0);
imshow("frame 1",preFrame);
imshow("frame 2",curFrame);
cvWaitKey(-1);
cout<<"========强行拷贝=========\n";
st1 = cvGetTickCount();
for (int i=0;i<1001;i++)
{
preFrame.copyTo(tmp);
curFrame.copyTo(preFrame);
tmp.copyTo(curFrame);
}
et1 = cvGetTickCount();
printf("swap 1000 times cost: %f milliseconds\n", (et1-st1)/(double)cvGetTickFrequency()/1000.0);
imshow("frame 1",preFrame);
imshow("frame 2",curFrame);
cvWaitKey(-1);
getchar();
return 0;
}
结果为: