opencv_tutorial_code学习——建缩减表、计时、扫描图片

本文档详细介绍了如何使用OpenCV从`how_to_scan_images.cpp`教程代码中构建索引表并实现高效的图片扫描。通过学习,读者将掌握在OpenCV中进行图像扫描和性能计时的关键技巧。
摘要由CSDN通过智能技术生成

From tutorial_code\core\how_to_scan_images\how_to_scan_images.cpp

建缩减表方法:

int divideWith = 0;
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;
t = (double)getTickCount();
/* do something */
t = ((double)getTickCount() - t)/getTickFrequency();

扫描图片方法一:

int channels = I.channels();
int nRows = I.rows;
int nCols = I.cols * channels;

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]];
    }
}

扫描图片方法二:

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]];
        }
    }
}

扫描图片方法三:

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;
    }
}


直接对图像进行查表处理方法:

Mat lookUpTable(1, 256, CV_8U);
uchar* p = lookUpTable.ptr();
for( int i = 0; i < 256; ++i)
    p[i] = table[i];

for (int i = 0; i < times; ++i)
    LUT(I, lookUpTable, J);

输入图像和输出图像结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值