clarkk
clarkk

Reputation: 27679

convert to grayscale when loading file

Does thsese two code examples do the same?

A

cv::Mat gray = cv::imread(input, CV_LOAD_IMAGE_GRAYSCALE);

B

cv::Mat src = cv::imread(input);
cv::Mat gray;
cv::cvtColor(src, gray, CV_BGR2GRAY);

Upvotes: 3

Views: 104

Answers (1)

Humam Helfawi
Humam Helfawi

Reputation: 20264

Try this code:

cv::Mat gray1 = cv::imread(input, CV_LOAD_IMAGE_GRAYSCALE);
cv::Mat src = cv::imread(input);
cv::Mat gray2;
cv::cvtColor(src, gray2, CV_BGR2GRAY);
cv::Mat diff;
cv::absdiff(gray1, gray2, diff);
std::cout << cv::sum(diff)(0);

It prints 0. So from result point of view, yes they are the same.

but does they do the same? I tried to dig into OpenCV code But I could not get luck with it. However, I made a small benchmarking and I found that They seems to be not the same:

auto start_first = clock();
    for (size_t i = 0; i < 1000; i++){
        cv::Mat gray1 = cv::imread(input, CV_LOAD_IMAGE_GRAYSCALE);
    }
    auto stop_first = clock();
    std::cout << "First:" << stop_first - start_first << " clock\n";

    auto start_second = clock();
    for (size_t i = 0; i < 1000; i++){
        cv::Mat src = cv::imread(input);
        cv::Mat gray2;
        cv::cvtColor(src, gray2, CV_BGR2GRAY);
    }
    auto stop_second = clock();
    std::cout << "second:" << stop_second - start_second << " clock\n";

The second approach is a bit slower.

First:4614 clock

second:6051 clock

Windows 8, Visual Studio 2013, OpenCV 2.4.10, Intel Core i7

Upvotes: 4

Related Questions