gpt4 book ai didi

c++ - 加载文件时转换为灰度

转载 作者:太空狗 更新时间:2023-10-29 20:55:25 25 4
gpt4 key购买 nike

这两个代码示例是否相同?

一个

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

最佳答案

试试这段代码:

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

它打印0。所以从结果的角度来看,是的,它们是一样的。

但他们也这样做吗?我试图深入研究 OpenCV 代码,但我运气不好。然而,我做了一个小的基准测试,我发现它们似乎不太一样:

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

第二种方法有点慢。

First:4614 clock

second:6051 clock

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

关于c++ - 加载文件时转换为灰度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35722989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com