1

私はジェスチャー認識アプリケーションに取り組んでおり、タイマーを実装したいのですが、方法がわかりません。

これが私がやりたいことです: ユーザーは、次の機能が発生する前に 3 秒間 1 つのジェスチャを表示する必要があります。

今は次のようになります。

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)<- これが 3 秒間 true の場合、{} が発生します。

ご協力ありがとう御座います。

4

4 に答える 4

1

sleep という組み込み関数があります。int xミリ秒の間、プログラムを一時的に停止します

関数を待機させます(int秒):

void wait(long seconds)
{
    seconds = seconds * 1000;
    sleep(seconds);
}

待機 (1); // 1 秒または 1000 ミリ秒待機します

于 2013-11-06T18:58:21.940 に答える
0

また、次のことを試すこともできます。

#include <time.h>

clock_t init, final;

init=clock();
//
// do stuff
//
final=clock()-init;
cout << (double)final / ((double)CLOCKS_PER_SEC);

詳細については、次のリンクを参照してください。

http://www.cplusplus.com/forum/beginner/317/

http://www.cplusplus.com/reference/ctime/

于 2013-11-06T19:02:36.697 に答える
0

アプリが更新ループを実行していると仮定します。

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

これが基本的なロジックです。タイマー クラスを記述し、本体を 1 つの関数にリファクタリングして、アプリの他の部分のためのスペースを作ることができます。

于 2013-11-06T19:30:38.743 に答える
0

次のことを試すことができます。

#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;        
    }
}

の場合、std::chrono代わりに .NET で作業するクラスを含むソリューションをお勧めしtime.hます。

于 2013-11-06T19:16:06.493 に答える