首页 > 其他 > 详细

how is b=a.clone() different than a.copyTo(b)? which is faster?

时间:2017-03-02 19:16:49      阅读:268      评论:0      收藏:0      [点我收藏+]

how is b=a.clone() different than a.copyTo(b)? which is faster?

For an image processing demo I need to squeeze every bit of optimization out of my code to get a higher frame rate, but I‘m familiar with hardware programming (VHDL) and not so much with CPU programming languages. So it‘s not clear to me how much faster code can run by moving statements such as variable declarations outside of loops and functions, i.e., does encountering a type declaration millions of times actually slow down execution? To this end, there also seem to be different methods to performing the same OpenCV array tasks on Mat types. My big question at this time is whether I should use clone or copyTo if I simply want to make a copy of an existing Mat array. Does it matter if the dst array has already been declared with the same size as the src? Thanks.

Do like you want :


clone source is


Mat Mat::clone() const

{

    Mat m;

    copyTo(m);

    return m;

}


if you want to test :

TickMeter chronometer;

Mat a(500,500,CV_8UC1);

RNG rr;

rr.fill(a,CV_8UC1, RNG::UNIFORM, 0, 256);

chronometer.start();

for (int i = 0; i < 1000; i++)

{

    Mat b = a.clone();



}

chronometer.stop();

cout << "Clone " << chronometer.getTimeSec() << endl;

chronometer.reset();

chronometer.start();

for (int i = 0; i < 1000; i++)

{

    Mat b;

    a.copyTo(b);

}

chronometer.stop();

cout << "Copy " << chronometer.getTimeSec() << endl;


 

Comments

Thanks for this code to run. It gives me practically identical results for clone and copyTo. So am I correct to understand that clone is actually just a copyTo operation nested within a function call? Why would this be necessary? Anyhow, I have my answer in that I will just use copyTo.

回顾:
TickMeter 是Contribute中的,简单修改
int main(int argc, char* argv[])
{
    
    Mat a(500,500,CV_8UC1);
    RNG rr;
    rr.fill(a,CV_8UC1, RNG::UNIFORM, 0256);
    CString strOutput;
    double dstart =(double) cv::getTickCount();
    for (int i = 0; i < 1000; i++)
    {
        Mat b = a.clone();
    }
    strOutput.Format("Clone %f s ",(getTickCount() - dstart)/getTickFrequency());
    cout << strOutput << endl;
    dstart =(double) cv::getTickCount();
    for (int i = 0; i < 1000; i++)
    {
        Mat b;
        a.copyTo(b);
    }
    strOutput.Format("Copy %f s ",(getTickCount() - dstart)/getTickFrequency());
    cout << strOutput << endl;
}

技术分享
其中,注意
RNG rr;
    rr.fill(a,CV_8UC1, RNG::UNIFORM, 0256);
技术分享
是生成这样的随机图片。那么从结论上来看,copyto和clone是一样的,那么我也和提问者的结论一下,在以后的代码中只是用CopyTo,因为CopyTO还有一个mask的参数挺有用的。




how is b=a.clone() different than a.copyTo(b)? which is faster?

原文:http://www.cnblogs.com/jsxyhelu/p/6491999.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!