I'm writing a code that uses QTimer to trigger calls to opencv videoCapture to read video frames. I usually read large chunks of videos so, wondering is there any other way to accelerate the process.
here a snapshot of my code that uses QTimer :
timer = new QTimer();
timer->setTimerType(Qt::PreciseTimer);
connect(timer, SIGNAL(timeout()), this, SLOT(read_shape_params()));
//in a loop stop timer and setup the next video stream then start
void next(){
timer->stop();
stream = new video_stream_reader();
stream->setColorGray(grayImage);
stream->set_begin_end(begin_at,end_at);
stream->open(video_base_path+video_path);
timer->start(0);
}
void shape_param_finder::read_shape_params(){
Mat frame;
frame = stream->read_frame();
}
Mat video_stream_reader::read_frame(){
Mat frame;
bool bSuccess = capture->read(frame);
return frame;
}
It has little to do with QTimer. But
timer->start(0);
is an issue. Your video, you input camera has a
frame per second
, which means a period to produce frame. For instance, 25fps means you will get a new frame every time period,40ms
in this case.Short answer: Without a proper hardware synchronization, set the timer timeout to
1000 / expected fps
.Long answer:
A timer with
timeout = 0
will schedule aread_shape_params
as fast as the framework can. which means that the performance bottleneck end up beingcapture->read(frame);
, assuming the other part of the code (display, etc..) work perfectly.There are 3 cases about
capture->read(frame)
:It takes less time that the resolution that time period : Should be good right? Wrong. You read the same image multiple times. Which means at best you waste cpu resource. At worst, things start to behave like in case 1 from your point of view. How is that? Let say it take 30 ms to present a frame (read and show, I assume you do it linearly).
If on top of that you keep queuing item for display, and display is slow, your perceived fps will be lower.
video_stream_reader::read_frame
to be sure.