如何用Opencv掃描圖像,LUT及時(shí)間測(cè)量

目標(biāo)

如何通過圖像的每一個(gè)像素?
OpenCV矩陣值如何存儲(chǔ)?
如何衡量我們的算法的性能?
什么是查找表(LUT),為什么使用它們?

測(cè)試用例

考慮一個(gè)簡(jiǎn)單的顏色空間減少方法。通過對(duì)矩陣項(xiàng)存儲(chǔ)使用unsigned char C和C ++類型,像素通道最多可能有256個(gè)不同的值。對(duì)于三通道圖像,這可以允許形成太多的顏色(1600萬)。使用這么多色調(diào)可能會(huì)對(duì)我們的算法性能造成沉重打擊。然而,有時(shí)候,只要少一些工作能夠獲得相同的最終結(jié)果就足夠了。

在這種情況下,我們常常會(huì)減少色彩空間。這意味著我們將顏色空間當(dāng)前值與新的輸入值分開,以減少顏色。例如,零和九之間的每個(gè)值都將變?yōu)榱?,十到十九值之間的值變?yōu)?0等等。

當(dāng)您使用int值將uchar(unsigned char-aka值在0和255之間)值分隔時(shí),結(jié)果也將是char。這些值只能是char值。因此,任何分?jǐn)?shù)將被向下舍入。利用這一事實(shí),uchar域中的上層操作可以表示為:

公式

簡(jiǎn)單的色彩空間縮小算法將僅包含通過圖像矩陣的每個(gè)像素并應(yīng)用該公式。值得注意的是,我們做一個(gè)除法和乘法運(yùn)算。這些操作對(duì)于系統(tǒng)來說是昂貴的。如果可能,通過使用更便宜的操作(如少量減法,加法或賦值運(yùn)算)來避免這種情況。此外,請(qǐng)注意,我們只有上限操作的輸入值有限。在uchar系統(tǒng)的情況下,這是256。

因此,對(duì)于較大的圖像,通過使用查找表,預(yù)先計(jì)算所有可能的值,并且在分配期間僅僅進(jìn)行分配是明智的。查找表是簡(jiǎn)單的數(shù)組(具有一個(gè)或多個(gè)維),對(duì)于給定的輸入值變量保存最終的輸出值。它的優(yōu)點(diǎn)在于我們不需要進(jìn)行計(jì)算,只需要讀取結(jié)果。

我們的測(cè)試用例程序?qū)?zhí)行以下操作:讀取控制臺(tái)圖像(可以是彩色或灰度圖像),并使用給定的控制臺(tái)行參數(shù)減少整數(shù)值。在OpenCV中,目前有三種主要的逐個(gè)掃描圖像像素的方法。為了使事情更有趣,將使用所有這些方法對(duì)每個(gè)圖像進(jìn)行掃描,并打印出花費(fèi)多長(zhǎng)時(shí)間。

Note:可以在這里下載完整cpp教程代碼。

如何衡量時(shí)間?

那么OpenCV提供了兩個(gè)簡(jiǎn)單的函數(shù)來實(shí)現(xiàn)這個(gè)cv :: getTickCount()cv :: getTickFrequency()。
第一個(gè)從某個(gè)事件返回系統(tǒng)CPU的時(shí)間clock(就像您啟動(dòng)系統(tǒng)一樣)。
第二次返回您的CPU在一秒鐘內(nèi)發(fā)出多少次時(shí)間clock。
所以為了測(cè)量秒數(shù),兩次操作之間的時(shí)間如下:

double t = (double)getTickCount();
// do something ...
t = ((double)getTickCount() - t) / getTickFrequency();
cout << "Times passed in seconds: " << t << endl;

圖像矩陣如何存儲(chǔ)在內(nèi)存中?

正如上一節(jié)的Mat - 基本圖像容器教程,矩陣的大小取決于使用的顏色系統(tǒng)。更準(zhǔn)確地說,它取決于所使用的通道數(shù)量。
在灰度圖像的情況下:

灰度

對(duì)于多通道圖像,列包含與通道數(shù)一樣多的子列。例如在BGR顏色系統(tǒng)的情況下:

彩色圖

注意,通道的順序是反向的:BGR而不是RGB。因?yàn)樵谠S多情況下,存儲(chǔ)器足夠大以便以連續(xù)的方式存儲(chǔ)行,所以行可以一個(gè)接一個(gè)地跟隨,創(chuàng)建一個(gè)長(zhǎng)行。因?yàn)橐磺卸荚谝粋€(gè)接一個(gè)的地方,這可能有助于加快掃描過程。
可以使用cv :: Mat :: isContinuous()函數(shù)來詢問矩陣是否是這種情況。

完整代碼

#include <opencv2/core.hpp>
#include <opencv2/core/utility.hpp>
#include "opencv2/imgcodecs.hpp"
#include <opencv2/highgui.hpp>
#include <iostream>
#include <sstream>

using namespace std;
using namespace cv;

static void help()
{
    cout
        << "\n--------------------------------------------------------------------------" << endl
        << "This program shows how to scan image objects in OpenCV (cv::Mat). As use case"
        << " we take an input image and divide the native color palette (255) with the " << endl
        << "input. Shows C operator[] method, iterators and at function for on-the-fly item address calculation." << endl
        << "Usage:" << endl
        << "./how_to_scan_images <imageNameToUse> <divideWith> [G]" << endl
        << "if you add a G parameter the image is processed in gray scale" << endl
        << "--------------------------------------------------------------------------" << endl
        << endl;
}

Mat& ScanImageAndReduceC(Mat& I, const uchar* table);
Mat& ScanImageAndReduceIterator(Mat& I, const uchar* table);
Mat& ScanImageAndReduceRandomAccess(Mat& I, const uchar * table);

int main(int argc, char* argv[])
{
    help();
    if (argc < 3)
    {
        cout << "Not enough parameters" << endl;
        return -1;
    }

    Mat I, J;
    if (argc == 4 && !strcmp(argv[3], "G"))
        I = imread(argv[1], IMREAD_GRAYSCALE);
    else
        I = imread(argv[1], IMREAD_COLOR);

    if (I.empty())
    {
        cout << "The image" << argv[1] << " could not be loaded." << endl;
        return -1;
    }

    //! [dividewith]
    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));
    //! [dividewith]

    const int times = 100;
    double t;

    t = (double)getTickCount();

    for (int i = 0; i < times; ++i)
    {
        cv::Mat clone_i = I.clone();
        J = ScanImageAndReduceC(clone_i, table);
    }

    t = 1000 * ((double)getTickCount() - t) / getTickFrequency();
    t /= times;

    cout << "Time of reducing with the C operator [] (averaged for "
        << times << " runs): " << t << " milliseconds." << endl;

    t = (double)getTickCount();

    for (int i = 0; i < times; ++i)
    {
        cv::Mat clone_i = I.clone();
        J = ScanImageAndReduceIterator(clone_i, table);
    }

    t = 1000 * ((double)getTickCount() - t) / getTickFrequency();
    t /= times;

    cout << "Time of reducing with the iterator (averaged for "
        << times << " runs): " << t << " milliseconds." << endl;

    t = (double)getTickCount();

    for (int i = 0; i < times; ++i)
    {
        cv::Mat clone_i = I.clone();
        ScanImageAndReduceRandomAccess(clone_i, table);
    }

    t = 1000 * ((double)getTickCount() - t) / getTickFrequency();
    t /= times;

    cout << "Time of reducing with the on-the-fly address generation - at function (averaged for "
        << times << " runs): " << t << " milliseconds." << endl;

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

    t = (double)getTickCount();

    for (int i = 0; i < times; ++i)
        //! [table-use]
        LUT(I, lookUpTable, J);
    //! [table-use]

    t = 1000 * ((double)getTickCount() - t) / getTickFrequency();
    t /= times;

    cout << "Time of reducing with the LUT function (averaged for "
        << times << " runs): " << t << " milliseconds." << endl;
    return 0;
}

//! [scan-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;
}
//! [scan-c]

//! [scan-iterator]
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;
}
//! [scan-iterator]

//! [scan-random]
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;
}
//! [scan-random]

用法

用法
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容