Suggestion on 'How to Scan Images' tutorial page (Patch #4079)


Added by Mark Benjamin about 10 years ago. Updated almost 10 years ago.


Status:New Start date:2014-12-19
Priority:Low Due date:
Assignee:- % Done:

0%

Category:documentation
Target version:3.1
Affected version:branch 'master' (3.0-dev) Operating System:Linux
Difficulty:Easy HW Platform:x64
Pull request:

Description

Hi,

I'm reading http://docs.opencv.org/doc/tutorials/core/how_to_scan_images/how_to_scan_images.html thinking there should be a comparison with bitshifting [even though obviously bitshifting is limited to specific multiples]

as a simple addition to the code http://docs.opencv.org/_downloads/how_to_scan_images.cpp suggests a bitshift function is 3 times faster than the builtin LUT function

    if ((divideWith == 128) || (divideWith == 64) || (divideWith == 32)
            || (divideWith == 16) || (divideWith == 8) || (divideWith == 4)
            || (divideWith == 2)) {
        double power = log2(divideWith);
        int pwr = (int) power;

        cout << "factor = " << divideWith << ", power = " << power <<
                ", pwr = " << pwr << endl;

        t = (double)getTickCount();
        for (int i = 0; i < times; ++i)
            clone_i = I.clone();
            ScanImageAndReduceBitShiftC(clone_i, pwr);

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

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

    }

could be added to the main() function, then

Mat& ScanImageAndReduceBitShiftC(Mat &I, const int shiftval) {
    CV_Assert(I.depth() != sizeof(uchar));

    unsigned int nels = I.cols * I.rows * I.channels();

    uchar *p = I.data;
    for (unsigned int i = 0; i < nels; i++) {
        *(p++) >>= shiftval;
    }

    return I;
}

Related issues

related to Feature #2400: Suggestions on "How to Scan images" Open 2012-09-30

Associated revisions

Revision b866e267
Added by Vadim Pisarevsky almost 10 years ago

Merge pull request #4079 from Dikay900:2_4_to_master

History

Updated by Mark Benjamin about 10 years ago

the test for a power of 2 could be simplified to

if ((divideWith != 0) && !(divideWith & (divideWith - 1)))

while the derivation of the log_2 could be standardized cross-platform to

int powers[8] = {1,2,4,8,16,32,64,128};
int pwr = 0;
while (powers[pwr] < divideWith) pwr++;

then there's no particular need to double check that the value of pwr is correct, so suppress the debugging cout;

replacing that initial load of code, then, with

    if ((divideWith != 0) && !(divideWith & (divideWith - 1))) {
        int powers[8] = {1,2,4,8,16,32,64,128};
        int pwr = 0;
        while (powers[pwr] < divideWith) pwr++;
        t = (double)getTickCount();
        for (int i = 0; i < times; ++i)
            clone_i = I.clone();
            ScanImageAndReduceBitShiftC(clone_i, pwr);

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

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

    }

Although obviously I get that the tutorial is designed to show how quick the builtin LUT function is, it simply seems worthwhile flagging up that a bitshift in an appropriate case is much quicker :-)

Updated by Maksim Shabunin almost 10 years ago

  • Target version set to 3.1

Also available in: Atom PDF