opencv学习笔记(二)扫描图像,查找表格和时间测量

来源:互联网 发布:大秦铁路 知乎 编辑:程序博客网 时间:2024/05/13 14:32

查找表:

 int divideWith = 0; // convert our input string to number - C++ style    stringstream s;    s << argv[2];    s >> divideWith;    if (!s || !divideWith)    {        cout << "Invalid number entered for dividing. " << endl;        return -1;    }    uchar table[256];    for (int i = 0; i < 256; ++i)       table[i] = (uchar)(divideWith * (i/divideWith));
如何计算时间:
double t = (double)getTickCount();// do something ...t = ((double)getTickCount() - t)/getTickFrequency();cout << "Times passed in seconds: " << t << endl;

当涉及到性能时,你无法击败经典的C风格的操作符[](指针)访问,效率高:

Mat& ScanImageAndReduceC(Mat& I, const uchar* const table){    // accept only char type matrices    CV_Assert(I.depth() == CV_8U);    int channels = I.channels();    int nRows = I.rows;    int nCols = I.cols * channels;    if (I.isContinuous())    {        nCols *= nRows;        nRows = 1;    }    int i,j;    uchar* p;    for( i = 0; i < nRows; ++i)    {        p = I.ptr<uchar>(i);        for ( j = 0; j < nCols; ++j)        {            p[j] = table[p[j]];        }    }    return I;}
ps:(  p=I.ptr<uchar>(i)图像第i行的头指针,通过这个指针结合列的位置(就是你代码中的j)可以很轻松操作图像改行的每一列  )
如果是灰度图像,则更方便,如下:

uchar* p = I.data;for( unsigned int i =0; i < ncol*nrows; ++i)    *p++ = table[*p];
迭代器(安全)方法:

case 1:只有单通道的情况。

case 3:彩色通道的情况。

Mat& ScanImageAndReduceIterator(Mat& I, const uchar* const table){    // accept only char type matrices    CV_Assert(I.depth() == CV_8U);    const int channels = I.channels();    switch(channels)    {    case 1:        {            MatIterator_<uchar> it, end;            for( it = I.begin<uchar>(), end = I.end<uchar>(); it != end; ++it)                *it = table[*it];            break;        }    case 3:        {            MatIterator_<Vec3b> it, end;            for( it = I.begin<Vec3b>(), end = I.end<Vec3b>(); it != end; ++it)            {                (*it)[0] = table[(*it)[0]];                (*it)[1] = table[(*it)[1]];                (*it)[2] = table[(*it)[2]];            }        }    }    return I;}
参考返回的即时地址计算: 其基本用法是指定要访问的项目的行号和列号,为了获取或修改图像中的某种方式的随机元素。

Mat& ScanImageAndReduceRandomAccess(Mat& I, const uchar* const table){    // accept only char type matrices    CV_Assert(I.depth() == CV_8U);    const int channels = I.channels();    switch(channels)    {    case 1:        {            for( int i = 0; i < I.rows; ++i)                for( int j = 0; j < I.cols; ++j )                    I.at<uchar>(i,j) = table[I.at<uchar>(i,j)];            break;        }    case 3:        {         Mat_<Vec3b> _I = I;         for( int i = 0; i < I.rows; ++i)            for( int j = 0; j < I.cols; ++j )               {                   _I(i,j)[0] = table[_I(i,j)[0]];                   _I(i,j)[1] = table[_I(i,j)[1]];                   _I(i,j)[2] = table[_I(i,j)[2]];            }         I = _I;         break;        }    }    return I;}
如果您需要使用此方法对图像进行多次查找,则可能会为每个访问输入类型和at关键字可能会耗费时间。

opencv.core模块给我们提供了现成的函数LUT(look up table):

    Mat lookUpTable(1, 256, CV_8U);    uchar* p = lookUpTable.data;    for( int i = 0; i < 256; ++i)        p[i] = table[i];
调用LUT函数:
        LUT(I, lookUpTable, J);

I为输入,J为输出。









阅读全文
0 0
原创粉丝点击