简体   繁体   English

将 opencv waitkey() 与 C++ 一起使用

[英]Use of opencv waitkey() with C++

I'm trying to display the webcam feed with a timer but it doesn't render smoothly.我正在尝试使用计时器显示网络摄像头提要,但渲染不流畅。 Here is the code:这是代码:

    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);

    }
}

You might want to use std::chrono instead, to keep calling cv::imshow() + cv::waitKey for 10s and end the loop.您可能想改用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM