OpenCV Polylines 过度 CPU 使用
OpenCV Polylines excessive CPU usage
我正在通过 UDP 接收视频流 (640x480p) 并使用 OpenCV 的 imdecode() 解码同一线程中的每一帧。如果解码正确,帧将传递给新启动的线程进行图像处理(findChessboardCorners() 和 polylines()),然后线程被分离。
接收和解码部分工作完美,但我记录了 polylines() 的执行时间,它从大约 5 毫秒开始,并且随着程序运行时间的延长(高达 4000 毫秒或更多)变得更糟。 Visual Studio 的性能分析器报告说,polylines() 使用了 ~98% 的 CPU。使用 polylines() 绘制点的矢量由 40 个点组成。
即使我分离了每个线程,什么会导致这种性能损失? (甚至用 Intel Xeon 测试过)
void decode(Mat videoFrame) {
Mat rotationMat;
Mat translationMat;
Mat chessboard;
resize(videoFrame, chessboard, Size(), resizeFactor, resizeFactor);
Size patternSize(chessboardSize.front(), chessboardSize.back());
vector<Point2f> corners;
vector<Point2f> imagePoints;
bool patternFound = findChessboardCorners(chessboard, patternSize, corners, CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_FAST_CHECK);
if (patternFound) {
solvePnP(objectPoints, corners, cameraMatrix, distCoeffs, rotationMat, translationMat);
vector<Point3d> path_3d = fahrspur.computePath(steeringAngle);
vector<Point2d> path_2d;
projectPoints(path_3d, rotationMat, translationMat, cameraMatrix, distCoeffs, path_2d);
Mat curve(path_2d, true);
curve.convertTo(curve, CV_32S);
double t4 = getCurrentTime();
polylines(chessboard, curve, false, Scalar(0, 255, 0), 10, CV_AA);
double t5 = getCurrentTime();
cout << "time to execute polylines: " << t5-t4 << "ms" << endl;
assignFrameVideo(chessboard);
}
使用此解码方法的新线程从另一个线程启动,用于在 while 循环中接收帧:
Mat frameVideo;
while(1) {
//code for receiving a single frame, decode it and store it in frameVideo.
thread decodeThread = thread(decode, frameVideo);
decodeThread.detach();
}
我还使用了第二个选项以这种方式使用 polylines():
const Point *pts = (const Point*)Mat(path_2d).data;
int npts = Mat(path_2d).rows;
polylines(chessboard, &pts, &npts, 1, false, Scalar(0, 255, 0), 5);
但这根本不起作用,显示的图像没有任何线条。
我通过将 CV_AA 替换为 LINE_4 作为 polylines() 中的参数来解决它。
显然画线的抗锯齿是最重要的部分,现在它在 0-1 毫秒内运行。
我正在通过 UDP 接收视频流 (640x480p) 并使用 OpenCV 的 imdecode() 解码同一线程中的每一帧。如果解码正确,帧将传递给新启动的线程进行图像处理(findChessboardCorners() 和 polylines()),然后线程被分离。
接收和解码部分工作完美,但我记录了 polylines() 的执行时间,它从大约 5 毫秒开始,并且随着程序运行时间的延长(高达 4000 毫秒或更多)变得更糟。 Visual Studio 的性能分析器报告说,polylines() 使用了 ~98% 的 CPU。使用 polylines() 绘制点的矢量由 40 个点组成。
即使我分离了每个线程,什么会导致这种性能损失? (甚至用 Intel Xeon 测试过)
void decode(Mat videoFrame) {
Mat rotationMat;
Mat translationMat;
Mat chessboard;
resize(videoFrame, chessboard, Size(), resizeFactor, resizeFactor);
Size patternSize(chessboardSize.front(), chessboardSize.back());
vector<Point2f> corners;
vector<Point2f> imagePoints;
bool patternFound = findChessboardCorners(chessboard, patternSize, corners, CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_FAST_CHECK);
if (patternFound) {
solvePnP(objectPoints, corners, cameraMatrix, distCoeffs, rotationMat, translationMat);
vector<Point3d> path_3d = fahrspur.computePath(steeringAngle);
vector<Point2d> path_2d;
projectPoints(path_3d, rotationMat, translationMat, cameraMatrix, distCoeffs, path_2d);
Mat curve(path_2d, true);
curve.convertTo(curve, CV_32S);
double t4 = getCurrentTime();
polylines(chessboard, curve, false, Scalar(0, 255, 0), 10, CV_AA);
double t5 = getCurrentTime();
cout << "time to execute polylines: " << t5-t4 << "ms" << endl;
assignFrameVideo(chessboard);
}
使用此解码方法的新线程从另一个线程启动,用于在 while 循环中接收帧:
Mat frameVideo;
while(1) {
//code for receiving a single frame, decode it and store it in frameVideo.
thread decodeThread = thread(decode, frameVideo);
decodeThread.detach();
}
我还使用了第二个选项以这种方式使用 polylines():
const Point *pts = (const Point*)Mat(path_2d).data;
int npts = Mat(path_2d).rows;
polylines(chessboard, &pts, &npts, 1, false, Scalar(0, 255, 0), 5);
但这根本不起作用,显示的图像没有任何线条。
我通过将 CV_AA 替换为 LINE_4 作为 polylines() 中的参数来解决它。 显然画线的抗锯齿是最重要的部分,现在它在 0-1 毫秒内运行。