Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在c++;_C++_Linux_Timer_C++11 - Fatal编程技术网

C++ 如何在c++;

C++ 如何在c++;,c++,linux,timer,c++11,C++,Linux,Timer,C++11,我正在开发手势识别应用程序,我想实现定时器,但我不知道如何实现 下面是我想做的:在下一个功能发生之前,用户应该显示一个手势3秒钟 现在看起来是这样的: if(left_index==1) { putText("correct",Point(95,195),FONT_HERSHEY_COMPLEX_SMALL,0.8,Scalar(0,255,0),1,CV_AA); correct = true; } break; 我希望它是这样的:if(left_index

我正在开发手势识别应用程序,我想实现定时器,但我不知道如何实现

下面是我想做的:在下一个功能发生之前,用户应该显示一个手势3秒钟

现在看起来是这样的:

if(left_index==1)                 
{
putText("correct",Point(95,195),FONT_HERSHEY_COMPLEX_SMALL,0.8,Scalar(0,255,0),1,CV_AA);
correct = true;
}
break;

我希望它是这样的:
if(left_index==1)
有一个名为sleep的内置函数。它会让你的程序在int x毫秒内处于静止状态

我将使函数等待(int秒):


等待(1)//等待1秒或1000毫秒

有一个名为sleep的内置函数。它会让你的程序在int x毫秒内处于静止状态

我将使函数等待(int秒):


等待(1)//等待1秒或1000毫秒。此外,您可以尝试执行以下操作:

#include <time.h>

clock_t init, final;

init=clock();
//
// do stuff
//
final=clock()-init;
cout << (double)final / ((double)CLOCKS_PER_SEC);
#包括
时钟初始,最终;
init=时钟();
//
//做事
//
最终=时钟()-init;

cout此外,您可以尝试执行以下操作:

#include <time.h>

clock_t init, final;

init=clock();
//
// do stuff
//
final=clock()-init;
cout << (double)final / ((double)CLOCKS_PER_SEC);
#包括
时钟初始,最终;
init=时钟();
//
//做事
//
最终=时钟()-init;

cout您可以尝试以下方法:

#include <time.h>
#include <unistd.h>

// Whatever your context is ...
if(left_index==1)
{
    clock_t init, now;

    init=clock();
    now=clock();
    while((left_index==1) && ((now-init) / CLOCKS_PER_SEC) < 3))
    {
        sleep(100); // give other threads a chance!
        now=clock();
    }
    if((left_index==1) && (now-init) / CLOCKS_PER_SEC) >= 3))
    {
        // proceed with stuff
        putText
          ( "correct"
          , Point(95,195)
          , FONT_HERSHEY_COMPLEX_SMALL,0.8,Scalar(0,255,0),1,CV_AA);
        correct = true;        
    }
}
#包括
#包括
//不管你的背景是什么。。。
如果(左_索引==1)
{
时钟开始,现在;
init=时钟();
现在=时钟();
而((左索引==1)和((现在初始化)/每秒时钟数<3))
{
睡眠(100);//给其他线程一个机会!
现在=时钟();
}
如果((左索引==1)和(现在初始化)/时钟每秒>=3))
{
//继续
推杆
(“正确”
,点(95195)
FONT_HERSHEY_COMPLEX_SMALL,0.8,标量(0255,0),1,CV_AA);
正确=正确;
}
}

因为我更喜欢包含
std::chrono
中的类的解决方案,而不是使用
time.h

您可以尝试以下方法:

#include <time.h>
#include <unistd.h>

// Whatever your context is ...
if(left_index==1)
{
    clock_t init, now;

    init=clock();
    now=clock();
    while((left_index==1) && ((now-init) / CLOCKS_PER_SEC) < 3))
    {
        sleep(100); // give other threads a chance!
        now=clock();
    }
    if((left_index==1) && (now-init) / CLOCKS_PER_SEC) >= 3))
    {
        // proceed with stuff
        putText
          ( "correct"
          , Point(95,195)
          , FONT_HERSHEY_COMPLEX_SMALL,0.8,Scalar(0,255,0),1,CV_AA);
        correct = true;        
    }
}
#包括
#包括
//不管你的背景是什么。。。
如果(左_索引==1)
{
时钟开始,现在;
init=时钟();
现在=时钟();
而((左索引==1)和((现在初始化)/每秒时钟数<3))
{
睡眠(100);//给其他线程一个机会!
现在=时钟();
}
如果((左索引==1)和(现在初始化)/时钟每秒>=3))
{
//继续
推杆
(“正确”
,点(95195)
FONT_HERSHEY_COMPLEX_SMALL,0.8,标量(0255,0),1,CV_AA);
正确=正确;
}
}

因为我更喜欢包含
std::chrono
类的解决方案,而不是使用
time.h

假设您的应用程序正在运行更新循环

bool gesture_testing = false;
std::chrono::time_point time_start;

while(app_running) {
  if (! gesture_testing) {
    if (left_index == 1) {
      gesture_testing = true;
      time_start = std::chrono::high_resolution_clock::now();
    }
  } else {
    if (left_index != 1) {
      gesture_testing = false;
    } else {
      auto time_end = std::chrono::high_resolution_clock::now();
      auto duration = std::chrono::duration_cast<std::chrono::seconds>(time_end - time_start);
      if (duration.count() == 3) {
        // do gesture function
        gesture_testing = false;
      }
    }
  }
}
bool手势测试=false;
std::chrono::time\u point time\u start;
当(应用程序运行时){
如果(!手势_测试){
如果(左_索引==1){
手势测试=正确;
time_start=std::chrono::高分辨率_clock::now();
}
}否则{
如果(左索引!=1){
手势测试=假;
}否则{
自动时间结束=标准::时钟::高分辨率时钟::现在();
自动持续时间=标准::计时::持续时间\u转换(时间\u结束-时间\u开始);
if(duration.count()==3){
//做手势功能
手势测试=假;
}
}
}
}

这是基本逻辑,您可以编写一个计时器类并将主体重构为一个函数,为应用程序的其他部分腾出空间。

假设您的应用程序正在运行更新循环

bool gesture_testing = false;
std::chrono::time_point time_start;

while(app_running) {
  if (! gesture_testing) {
    if (left_index == 1) {
      gesture_testing = true;
      time_start = std::chrono::high_resolution_clock::now();
    }
  } else {
    if (left_index != 1) {
      gesture_testing = false;
    } else {
      auto time_end = std::chrono::high_resolution_clock::now();
      auto duration = std::chrono::duration_cast<std::chrono::seconds>(time_end - time_start);
      if (duration.count() == 3) {
        // do gesture function
        gesture_testing = false;
      }
    }
  }
}
bool手势测试=false;
std::chrono::time\u point time\u start;
当(应用程序运行时){
如果(!手势_测试){
如果(左_索引==1){
手势测试=正确;
time_start=std::chrono::高分辨率_clock::now();
}
}否则{
如果(左索引!=1){
手势测试=假;
}否则{
自动时间结束=标准::时钟::高分辨率时钟::现在();
自动持续时间=标准::计时::持续时间\u转换(时间\u结束-时间\u开始);
if(duration.count()==3){
//做手势功能
手势测试=假;
}
}
}
}

这是基本逻辑,您可以编写一个计时器类并将主体重构为一个函数,为应用程序的其他部分腾出空间。

节省时间、比较时间、清除时间。最重要的是:google/search。你能把你是如何检查样本中显示的手势的吗?我将从视频中识别的手势与通过示例学习的手势进行比较。if(left_index==1)比较。保存时间、比较时间、清除时间。最重要的是:google/search。你能把你是如何检查样本中显示的手势的吗?我将从视频中识别的手势与通过示例学习的手势进行比较。if(left_index==1)将其与之进行比较。睡眠不正常。我需要应用程序运行无论如何。。。据我所知,sleep只是暂停了这个过程。我会稍微更改一下代码,以便更清楚地知道
sleep()
需要几毫秒的时间。另外,在堆栈上本地重用参数也不是很好的编程风格@用户2961826您能解释一下原因吗?你的问题没有准确地解释你的需求,那么IMHO.if语句必须在行动发生前的整整3秒钟内为真。在睡眠中,这是真的,只是第一刻,然后暂停3秒钟。对不起,我的英语不好…:)这是视频中的实时手势识别。@user2961826那又怎样?您可以第一次检查,睡眠3秒,然后第二次检查,如果第二次检查中的条件仍然为真,则可以继续。还是我误解了s.th。?你不想在3秒结束前取消手势吗?看看我的答案……睡眠不好。我需要应用程序运行无论如何。。。据我所知,sleep只是暂停了这个过程。我会稍微更改一下代码,以便更清楚地知道
sleep()
需要几毫秒的时间。另外,在堆栈上本地重用参数也不是很好的编程风格@用户2961826您能解释一下原因吗?你的问题没有准确地解释你的需求,那么IMHO.if语句必须在行动发生前的整整3秒钟内为真。有了睡眠,这是真的