c++ - QT OpenCV multi threading to fetch images and display in GUI Main thread -
the basic idea
i developing application 4 usb cameras, frames must fetched these cameras , displayed in qlabel @ best possible fps.
fetching frames cameras sequentially results in low frame rate, though processor has 20% utilization.
how think frame rate can improved?
with use of multiple threads cpu utilization can increased, , there 4 threads each fetching images individual cameras, signaling , sending images gui thread(main thread) in turn display images appropriate label.
current design: based on https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/
the thread class inherits qobject methods:
class fetchcam1 : public qobject { q_object public: fetchcam1(cv::videocapture cap); ~fetchcam1(); public slots: void process(); cv::mat returncamframe(); signals: void finished(); };
its implementation:
fetchcam1::fetchcam1(cv::videocapture cap) { this->cap = cap; } cv::mat fetchcam1::returncam1frame() { return src; } void fetchcam1::process() { // allocate resources using new here cap.read(src); emit finished(); }
initiating threads , worker roles in main(gui) thread:
captures1..s4, videocapture object, fetchcam1* worker1-4, qthread* thread1-4; defined global variables. thread1 = new qthread; worker1 = new fetchcam1(captures1); worker1->movetothread(thread1); connect(thread1, signal(started()), worker1, slot(process())); connect(worker1, signal(finished()), this, slot(updatecam1())); connect(worker1, signal(finished()), worker1, slot(process())); thread1->start(); thread2 = new qthread; worker2 = new fetchcam1(captures2); worker2->movetothread(thread2); connect(thread2, signal(started()), worker2, slot(process())); connect(worker2, signal(finished()), this, slot(updatecam2())); connect(worker2, signal(finished()), worker2, slot(process())); thread2->start(); thread3 = new qthread; worker3 = new fetchcam1(captures3); worker3->movetothread(thread3); connect(thread3, signal(started()), worker3, slot(process())); connect(worker3, signal(finished()), this, slot(updatecam3())); connect(worker3, signal(finished()), worker3, slot(process())); thread3->start(); thread4 = new qthread; worker4 = new fetchcam1(captures4); worker4->movetothread(thread4); connect(thread4, signal(started()), worker4, slot(process())); connect(worker4, signal(finished()), this, slot(updatecam4())); connect(worker4, signal(finished()), worker4, slot(process())); thread4->start();
each of invoke:
void mainwindow::updatecam1() { cv::mat src = worker1->returncamframe(); qpixmap pixg = convertfrommat(src); ui->label_4s1->setpixmap(pixg); } void mainwindow::updatecam2() { cv::mat src = worker1->returncamframe(); qpixmap pixg = convertfrommat(src); ui->label_4s2->setpixmap(pixg); } void mainwindow::updatecam3() { cv::mat src = worker1->returncamframe(); qpixmap pixg = convertfrommat(src); ui->label_4s3->setpixmap(pixg); } void mainwindow::updatecam4() { cv::mat src = worker1->returncamframe(); qpixmap pixg = convertfrommat(src); ui->label_4s4->setpixmap(pixg); }
please me identify doing wrong? above code start , load first few frames, after program crashes no errors whatsoever.
also if 1 thread used fetch images 1 camera, above code works great.
what right approach implement this? thank you.
update crash:
i believe frames 4 cameras being fetched quite fast, , that's generating way many signals main thread handle, causes overflow causing crash. believe kind of synchronization can how can go it?
Comments
Post a Comment