2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

cv::waitKeyとstd::this_thread::sleep_for

Last updated at Posted at 2016-12-15

cv::imshowをwhile文のなかでひたすら呼びまくると、画像が更新できない。

while(true){
  if(flag) img = cv::imread("img.png");
  else img = cv::imread("img2.png");
  flag = !flag;
  cv::imshow("window", img);
}

gif画像
https://gyazo.com/84cd0ee729675695a25adee06b04fc18

cv::waitKeyを入れると画像が更新される。

while(true){
  if(flag) img = cv::imread("img.png");
  else img = cv::imread("img2.png");
  flag = !flag;
  cv::imshow("window", img);
  cv::waitKey(33); // new line
}

gif画像
https://gyazo.com/582e14c35762993b9b7fa9ddeb341bde

cv::waitKeyの代わりにstd::this_thread::sleep_forを使うと、また更新できなくなる。

while(true){
  if(flag) img = cv::imread("img.png");
  else img = cv::imread("img2.png");
  flag = !flag;
  cv::imshow("window", img);
  // cv::waitKey(33);
  std::this_thread::sleep_for(std::chrono::milliseconds(33)); // new line
}

https://gyazo.com/0b660432e65b4ce98c220d3313c64461

おそらく、std::this_thread::sleep_for (boost::this_thread::sleep など、その他のthreadをsleepをするようなやつも同じ) がスレッド自体をsleepさせる一方で、cv::waitKeyは、opencvの処理をするためにその先に進まなくしている。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?