OPENCV性能初探

环境:WIN7(32)+VS2012+OPENCV2.4.9

fun1为未做优化,fun2为通过移位避免乘除来优化,fun3为查表优化

#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;


Mat fun1(Mat src1,Mat src2,Mat dst)
{
 CV_Assert(src1.depth() != sizeof(uchar));    

    int channels = src1.channels();

    int nRows = src1.rows * channels;
    int nCols = src1.cols;

    if (src1.isContinuous())
    {
        nCols *= nRows;
        nRows = 1;        
    }

    int i,j;
    uchar* psrc1;
 uchar* psrc2;
 uchar* pdst;
    for( i = 0; i < nRows; ++i)
    {
        psrc1 = src1.ptr<uchar>(i);
  psrc2 = src2.ptr<uchar>(i);
  pdst = dst.ptr<uchar>(i);
        for ( j = 0; j < nCols; ++j)
        {
            pdst[j] = psrc1[j]*0.25+psrc2[j]*0.75; 
        }
    }
    return dst;


}

Mat fun2(Mat src1,Mat src2,Mat dst)
{
 CV_Assert(src1.depth() != sizeof(uchar));    

    int channels = src1.channels();

    int nRows = src1.rows * channels;
    int nCols = src1.cols;

    if (src1.isContinuous())
    {
        nCols *= nRows;
        nRows = 1;        
    }

    int i,j;
    uchar* psrc1;
 uchar* psrc2;
 uchar* pdst;
    for( i = 0; i < nRows; ++i)
    {
        psrc1 = src1.ptr<uchar>(i);
  psrc2 = src2.ptr<uchar>(i);
  pdst = dst.ptr<uchar>(i);
        for ( j = 0; j < nCols; ++j)
        {
            pdst[j] = (psrc1[j]>>2)+((psrc2[j]*3)>>2); 
        }
    }
    return dst;


}

Mat fun3(Mat src1,Mat src2,Mat dst,Mat lut)
{
 CV_Assert(src1.depth() != sizeof(uchar));    

    int channels = src1.channels();

    int nRows = src1.rows * channels;
    int nCols = src1.cols;

    if (src1.isContinuous())
    {
        nCols *= nRows;
        nRows = 1;        
    }

    int i,j;
    uchar* psrc1;
 uchar* psrc2;
 uchar* pdst;
 uchar* p = lut.data;

    for( i = 0; i < nRows; ++i)
    {
        psrc1 = src1.ptr<uchar>(i);
  psrc2 = src2.ptr<uchar>(i);
  pdst = dst.ptr<uchar>(i);
        for ( j = 0; j < nCols; ++j)
        {
   pdst[j] = p[(psrc1[j]*256+psrc2[j])];
        }
    }
    return dst;


}


int main( void )
{
   Mat src1, src2, dst,dst1,dst2,dst3;
   Mat lookUpTable(1,65536,CV_8U);
   uchar* p = lookUpTable.data;
   int m,n;
   for(m=0;m<256;m++)
   {
    for(n=0;n<256;n++)
    {
     p[m*256+n]=m*0.25+n*0.75;
    }
   }

   src1 = imread("../images/Desert.jpg");
   src2 = imread("../images/Tulips.jpg");
   dst = src1.clone();
   dst1 = src1.clone();
   dst2 = src1.clone();
   dst3 = src1.clone();

 

   double t = (double)getTickCount();
   addWeighted( src1, 0.25, src2, 0.75, 0.0, dst);
   t = ((double)getTickCount() - t)/getTickFrequency();
   cout << "OPENCV Times passed in seconds: " << t << endl;

   t = (double)getTickCount();
   fun1( src1,src2, dst1);
   t = ((double)getTickCount() - t)/getTickFrequency();
   cout << "FUN1 Times passed in seconds: " << t << endl;

   t = (double)getTickCount();
   fun2( src1,src2, dst2);
   t = ((double)getTickCount() - t)/getTickFrequency();
   cout << "FUN2 Times passed in seconds: " << t << endl;

   t = (double)getTickCount();
   fun3( src1,src2,dst3,lookUpTable);
   t = ((double)getTickCount() - t)/getTickFrequency();
   cout << "FUN3 Times passed in seconds: " << t << endl;

   namedWindow("Linear Blend", 1);
   imshow( "Linear Blend", dst3 );


   waitKey(0);
   return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值