[英]Use of opencv waitkey() with C++
我正在嘗試使用計時器顯示網絡攝像頭提要,但渲染不流暢。 這是代碼:
int timer = 10;
VideoCapture cap(0);
Mat img;
while (true) {
cap.read(img);
if (timer == 0) {
cout << "TIME'S UP !!!" << endl;
}
else if (timer <= 20) {
imshow("Image", img);
waitKey(1);
}
system("cls");
cout << timer-- << " seconds left" << endl;
Sleep(1000);
}
}
您可能想改用std:: chrono 來繼續調用cv::imshow()
+ cv::waitKey
10 秒並結束循環。
#include <chrono>
#include <iostream>
#include <opencv2/opencv.hpp>
namespace ch = std::chrono;
using namespace std::literals;
int main()
{
cv::VideoCapture cap(0);
cv::Mat img;
auto t0 = ch::system_clock::now();
for (decltype(t0)::duration elapsed; elapsed < 10s; elapsed = ch::system_clock::now() - t0)
{
if (cap >> img) {
imshow("Image", img);
}
waitKey(1);
}
std::cout << "TIME'S UP !!!" << std::endl;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.