Ask Your Question
0

Performance comparison of horizontal & vertical flipping

asked 2015-10-24 10:02:07 -0600

updated 2015-10-24 14:00:07 -0600

cv::flip function internally calls flipHoriz or flipVert functions.

When i compared the passing time of vertical flipping and horizontal flipping with the code below i get following results.

  • flipVert process time (averaged for 500 runs): 0.286388 milliseconds.
  • flipHoriz process time (averaged for 500 runs): 1.09012 milliseconds.

i thought that flipVert has different algorihtm (i did not understand deeper) and faster than flipHoriz .

i wonder is it possible to apply flipVert's algorihtm to flipHoriz.

what is your remarks?

( i edited the code adding more processes to compare )

  • src.clone process time (averaged for 500 runs): 0.793516 milliseconds.
  • src * 2 .. process time (averaged for 500 runs) : 0.39996 milliseconds.

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

int main()
{
    cv::Mat SrcImg = cv::imread("lena.jpg");
    cv::Mat DstImg;

    const int times = 500;
    double t;

    t = (double)cv::getTickCount();

    for (int i = 0; i < times; ++i)
    {
        flip(SrcImg, DstImg, 0);
    }

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

    std::cout << "flipVert  process time (averaged for "
              << times << " runs): " << t << " milliseconds."<< std::endl;

    t = (double)cv::getTickCount();

    for (int i = 0; i < times; ++i)
    {
        flip(SrcImg, DstImg, 1);
    }

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

    std::cout << "flipHoriz process time (averaged for "
              << times << " runs): " << t << " milliseconds."<< std::endl;

    t = (double)cv::getTickCount();

    for (int i = 0; i < times; ++i)
    {
        DstImg = SrcImg.clone();
    }

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

    std::cout << "src.clone process time (averaged for "
              << times << " runs): " << t << " milliseconds."<< std::endl;

    t = (double)cv::getTickCount();

    for (int i = 0; i < times; ++i)
    {
        DstImg = SrcImg * 2;
    }

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

    std::cout << "src * 2   process time (averaged for "
              << times << " runs): " << t << " milliseconds."<< std::endl;
    return 0;
}
edit retag flag offensive close merge delete

Comments

Think about how the image is laid out in memory. A horizontal flip is a reordering of columns, which are scalar data (not contiguous in memory over rows), whereas vertical flipping is a reordering of the rows which are vectors (large chunks) of data. Operating on large chunks will be much faster. The horizontal flip may be vectorized, but it will have the additional step of a remapping or a permutation of the elements and so will be slower.

Der Luftmensch 2 gravatar imageDer Luftmensch 2 ( 2015-10-24 10:37:16 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2015-10-24 11:05:37 -0600

updated 2015-10-24 11:07:43 -0600

IMO, the flip function's performance is depend on several factors:

  • The OpenCV build: because OpenCV can be built with OpenCL support, and IPP support, so when running, your program can call the normal version (no OpenCL or IPP support), or the OpenCL version, or the IPP version.
  • The image resolution: if the width is much larger than then the height (or vice versa) then the result is different.
  • And the memory access mechanism (as @Der Luftmensch mentioned): with horizontal flip, it should be faster because accessing matrix's elements row by row is faster than column by column.

My reproduced results when running your code on my machine (Duo Core 2x2.4 Ghz, OpenCV 3 with IPP no OpenCL, VS 2013 64 bit) are different from yours:

Image 1 (500x800):

  • flipVert process time (averaged for 500 runs): 0.66 ms
  • flipHoriz process time (averaged for 500 runs): 0.43 ms

Image 2 (500x500):

  • flipVert process time (averaged for 500 runs): 0.66 ms
  • flipHoriz process time (averaged for 500 runs): 0.25 ms

Image 3 (800x500):

  • flipVert process time (averaged for 500 runs): 0.74 ms
  • flipHoriz process time (averaged for 500 runs): 0.37 ms
edit flag offensive delete link more

Comments

thanks for the answer. but i think i need some more responses to understand what happens here.

sturkmen gravatar imagesturkmen ( 2015-10-24 12:00:42 -0600 )edit

Question Tools

Stats

Asked: 2015-10-24 10:02:07 -0600

Seen: 1,916 times

Last updated: Oct 24 '15