我正在通过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);
但这根本不起作用,显示的图像没有任何行。
答案 0 :(得分:1)