我正在开发一个应用程序,主要目标是从图像采集器中获取图像,进行一些处理,然后在GUI上显示图像.帧抓取器连接到PCIe.我正在使用帧抓取器SDK.图像流非常慢10到100张图像/秒我在这里对我的代码以及如何优化它有一些建议.首先,我从一个继承自Qthread的类中获取run()函数,我抓取一个图像并将其放在缓冲区queuecv :: Mat>上.
void ImageIn::run(){ _cam->allocMemory(); _cam->startAquisition(); _runningThread = true; while(_runningThread) { Mat image(_cam.getSizeX(), _cam.getSizeY(), CV_16U, _cam->getImageDMA0()); _ctrl->getMutexIn()->lock(); // Lock BufferIn _ctrl->getBufferIn()->push(image); // store Image in BufferIn _ctrl->getMutexIn()->unlock(); // Unlock bufferIn } }
图像存储在缓冲区中,然后处理线程做一些工作......
void ImageProcessing::run(){ while(_runningThread){ if (_ctrl->getMutexIn()->tryLock()){ while(!_ctrl->getBufferIn()->empty()){ _ctrl->getBufferIn()->front().convertTo(tempConvert, CV_32F); _bufferLocalIn.push(tempConvert); _ctrl->getBufferIn()->pop(); } _ctrl->getMutexIn()->unlock(); } // Do some processing and put image and a buffer for GUI } }
所以,我有一些问题: - 线程1由于阻塞功能而获得图像,因此CPU消耗很低,但线程2连续运行并消耗大量CPU重新评估我可以做些什么来修复它? - 这是编码的正确方法吗?
所以我试过了:
QThread* thread = new QThread; ImageWriter* worker = new ImageWriter(); worker->moveToThread(thread); QTimer* timer = new QTimer(); int msec = 100; timer->setInterval(msec); QObject::connect(thread, SIGNAL(started()), worker, SLOT(process())); QObject::connect(worker, SIGNAL(finished()), thread, SLOT(quit())); QObject::connect(timer, SIGNAL(timeout()), thread, SLOT(start())); timer->start();
好吗?