C++ opencv 方法比 python 慢
C++ opencv method is slower than python
尝试将 opencv 与 c++ 结合使用。 C++ 函数 cvtColor 的运行速度比 python 慢 16 倍。
我创建了 c++ 和 python 程序来测试 cvtColor 函数的效率。
cvtcolor s return 在 python 和 c++ 上的值相同。 Python 循环每秒运行 650-750 次。 C++ 执行 25-35 次。如何解决?
我已经尝试过 opencv 4.5.3 和 4.2.0 for c++。
python :
frame = cv2.imread(path)
fps = 0
clock = time.perf_counter()
while clock + 10 > time.perf_counter():
fps += 1/10
res = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
print(fps)
c++:
Mat frame, frame_HSV;
frame = imread(path);
frame_HSV = frame.clone();
int fps = 0;
unsigned __int64 clock_ = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
unsigned __int64 now;
clock_t start, end;
while (true) {
fps += 1;
now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
if (now - clock_ > 1000) {
clock_ = now;
cout << fps << endl;
fps = 0;
}
cvtColor(frame, frame_HSV, COLOR_BGR2HSV);
}
你的方法有问题。我无法正确指出,但我展示了我是如何做到的以及我的结果。
Python
(版本:4.5.1,IDE:VS 代码)
import cv2
import time as t
img = cv2.imread("18JE0646.jpg")
t1 = t.time()
for _ in range(10000):
hsv_image = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
t2 = t.time()
print(t2-t1)
输出:10.5 秒
C++
(版本:4.5.2,IDE:Visual Studio 2019(发布模式))
#include <iostream>
#include <ctime>
#include <opencv2/opencv.hpp>
using namespace std;
int main()
{
cv::Mat img, hsvImg;
img = cv::imread("18JE0646.jpg");
time_t start, finish;
time(&start);
for (int i = 0 ; i < 10000 ; i++)
cv::cvtColor(img, hsvImg, cv::COLOR_BGR2HSV);
time(&finish);
cout << difftime(finish, start) << " sec";
return 0;
}
输出:4.4 秒
注意:
在 C++ 中,在 运行 代码之前检查模式 (Debug/Release),尤其是在计算算法所用时间时。 Debug模式比Release模式慢大约40-50倍。
上面的 C++ 代码在发布模式下花费了 4.4 秒,在调试模式下花费了 237 秒。
尝试将 opencv 与 c++ 结合使用。 C++ 函数 cvtColor 的运行速度比 python 慢 16 倍。 我创建了 c++ 和 python 程序来测试 cvtColor 函数的效率。 cvtcolor s return 在 python 和 c++ 上的值相同。 Python 循环每秒运行 650-750 次。 C++ 执行 25-35 次。如何解决? 我已经尝试过 opencv 4.5.3 和 4.2.0 for c++。
python :
frame = cv2.imread(path)
fps = 0
clock = time.perf_counter()
while clock + 10 > time.perf_counter():
fps += 1/10
res = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
print(fps)
c++:
Mat frame, frame_HSV;
frame = imread(path);
frame_HSV = frame.clone();
int fps = 0;
unsigned __int64 clock_ = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
unsigned __int64 now;
clock_t start, end;
while (true) {
fps += 1;
now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
if (now - clock_ > 1000) {
clock_ = now;
cout << fps << endl;
fps = 0;
}
cvtColor(frame, frame_HSV, COLOR_BGR2HSV);
}
你的方法有问题。我无法正确指出,但我展示了我是如何做到的以及我的结果。
Python
(版本:4.5.1,IDE:VS 代码)
import cv2
import time as t
img = cv2.imread("18JE0646.jpg")
t1 = t.time()
for _ in range(10000):
hsv_image = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
t2 = t.time()
print(t2-t1)
输出:10.5 秒
C++
(版本:4.5.2,IDE:Visual Studio 2019(发布模式))
#include <iostream>
#include <ctime>
#include <opencv2/opencv.hpp>
using namespace std;
int main()
{
cv::Mat img, hsvImg;
img = cv::imread("18JE0646.jpg");
time_t start, finish;
time(&start);
for (int i = 0 ; i < 10000 ; i++)
cv::cvtColor(img, hsvImg, cv::COLOR_BGR2HSV);
time(&finish);
cout << difftime(finish, start) << " sec";
return 0;
}
输出:4.4 秒
注意:
在 C++ 中,在 运行 代码之前检查模式 (Debug/Release),尤其是在计算算法所用时间时。 Debug模式比Release模式慢大约40-50倍。 上面的 C++ 代码在发布模式下花费了 4.4 秒,在调试模式下花费了 237 秒。