Hi,
Thank you very much for you answers.
I read in other forum about the thread way but I want to try without thread.
what you do with
waitKey
there is a bad idea because of that . it’s best to use the lowest wait time possible here, which is 1 (or usepollKey()
). you don’t have to spend all idle time inwaitKey
. it only has to be entered periodically to keep the GUI window somewhat responsive.
Can you be more precise ?I did not understand everything this part cleary.
I modify my code to do something like this instead of using waitKey()
#include <chrono>
#include <ctime>
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/videoio.hpp>
#include <iostream>
int main(int argc, const char ** argv)
{
cv::VideoCapture cap(0,200);
int fpsCamera = 30;
int fpsCapture = 10;
double fps2 = cap.get(cv::CAP_PROP_FPS);
std::cout << "fps2 : " << fps2 << std::endl;
if (!cap.isOpened())
{
cap.release();
return -1;
}
std::chrono::time_point<std::chrono::high_resolution_clock>
prev_frame_time(std::chrono::high_resolution_clock::now());
std::chrono::time_point<std::chrono::high_resolution_clock>
new_frame_time;
cv::Mat frame;
while (true)
{
if (videoCapture.read(frame) == false)
{
if (frame.empty())
{
break;
}
}
new_frame_time = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> duration1(new_frame_time - prev_frame_time);
double fps = 1/duration1.count();
std::cout << "fps : " << fps << std::endl;
if(duration1.count() > 1/fpsCapture)
{
prev_frame_time = new_frame_time;
imshow("Frame", frame);
}
int key = cv::waitKey(1000/fpsCamera );
if (key % 256 == key_esc)
{
break; // (27)escape key
}
}
cap.release();
return 0;
}
I don’t know if it’s the write way.