嗨,我正在尝试通过openCV从不同的IP摄像机捕获多个帧,我有一个i7 12核心CPU,目前我可以流式传输4个高清IP摄像机并在2 x 2矩阵叠加窗口中显示每个摄像机,但是我的问题是流式传输除非我在每个线程中使用睡眠,否则这4台摄像机将占用我20%的CPU,这会从那里降低真实帧速率的显示。我正在使用计时器来触发每个调用opencv的线程,以读取每个帧以将图像调整为每个矩阵帧的大小,并将每个Mat缓冲区传递给Directx 11覆盖。如果我将摄像头设置为以25 FPS的速度流,则将触发每个线程事件的计时器设置为40毫秒,即每秒25个事件,但这使我的CPU瘫痪,这是我在线程中使用的一些代码
void mCam1(void * p)
{
COpenCVROIDlg* pThis = (COpenCVROIDlg*)p;
v_Mat[pThis->m_MatrixView].copyTo(v_MatPre[pThis->m_MatrixView]);
cv::Point pt;
cv::Mat m_img, imgResized, m_matrix;
if (!pThis->camera0.isOpened()) {
// Send not connected to list control
}else{
pThis->camera0.read(m_img);
if (m_img.empty()) {
//send no signal to list control
}
else {
cv::resize(m_img, imgResized, cv::Size(v_Cams[pThis->m_MatrixView][0].mPT.rc.right, v_Cams[pThis->m_MatrixView][0].mPT.rc.bottom), 0, 0, INTER_LINEAR_EXACT);
Rect roi(v_Cams[pThis->m_MatrixView][0].mPT.pt.x, v_Cams[pThis->m_MatrixView][0].mPT.pt.y, imgResized.cols, imgResized.rows);
v_Mat[pThis->m_MatrixView].copyTo(m_matrix);
imgResized.copyTo(m_matrix(roi));
m_matrix.copyTo(v_Mat[pThis->m_MatrixView]);
cv::waitKey(1);
}
}
cv::waitKey(84);// 12 frames per second
}
void CreateThreads(){
m_hEventStream0 = CreateEvent(NULL, FALSE, FALSE, 0);`
ghOnvifThread[2] = CreateThread(
NULL, // default security
0, // default stack size
(LPTHREAD_START_ROUTINE)ThreadCamera1, // name of the thread function
LPVOID(this), // no thread parameters
THREAD_TERMINATE | THREAD_SET_CONTEXT, // default startup flags
&dwThreadID[2]);
SetTimer(TM_CAMERA0, 10, NULL);
}
static LPTHREAD_START_ROUTINE ThreadCamera1(LPVOID _pParam)
{
COpenCVROIDlg* pThis = (COpenCVROIDlg*)_pParam;
HANDLE hEvent;
hEvent = m_hEventStream0;
DWORD dwEventObject;
__try
{
while (dwThreadID[2])
{
dwEventObject = WaitForSingleObject(hEvent, INFINITE);
if (dwEventObject == (WAIT_OBJECT_0))
{
mCam1(pThis);
}
}
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
// ExitThread(0x2222E000);
return 0;
}
ExitThread(0x22220000);
return 0;
}
任何人都可以给我展示一个示例或为我提供一种方法,使我可以运行多个线程而不会导致过多的CPU开销。似乎在opencv中,我无法获得每个视频捕获来告诉我是否准备好新图像,我必须继续通过线程对其进行探测并以这种方式读取每个帧。 谢谢