| OpenCV 5.0.0alpha Open Source Computer Vision

Loading...
Searching...
No Matches
Launching Viz

Next Tutorial: Pose of a widget

# Goal

In this tutorial you will learn how to

- Open a visualization window.
- Access a window by its name.
- Start event loop.
- Start event loop for a given amount of time.

# Code

You can download the code from here.

#include <opencv2/viz.hpp>
#include <iostream>
using namespace cv;
using namespace std;
static void help()
{
cout
<< "--------------------------------------------------------------------------" << endl
<< "This program shows how to launch a 3D visualization window. You can stop event loop to continue executing. "
<< "You can access the same window via its name. You can run event loop for a given period of time. " << endl
<< "Usage:" << endl
<< "./launching_viz" << endl
<< endl;
}
int main()
{
help();
viz::Viz3d myWindow("Viz Demo");
myWindow.spin();
cout << "First event loop is over" << endl;
viz::Viz3d sameWindow = viz::getWindowByName("Viz Demo");
sameWindow.spin();
cout << "Second event loop is over" << endl;
sameWindow.spinOnce(1, true);
while(!sameWindow.wasStopped())
{
sameWindow.spinOnce(1, true);
}
cout << "Last event loop is over" << endl;
return 0;
}
cv::viz::Viz3d
The Viz3d class represents a 3D visualizer window. This class is implicitly shared.
Definition viz3d.hpp:68
cv::viz::Viz3d::spin
void spin()
The window renders and starts the event loop.
cv::viz::Viz3d::spinOnce
void spinOnce(int time=1, bool force_redraw=false)
Starts the event loop for a given time.
cv::viz::Viz3d::wasStopped
bool wasStopped() const
Returns whether the event loop has been stopped.
main
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3
cv
Definition core.hpp:107
std
STL namespace.
viz.hpp

# Explanation

Here is the general structure of the program:

- Create a window.
  viz::Viz3d myWindow("Viz Demo");
- Start event loop. This event loop will run until user terminates it by pressing e, E, q, Q.
  myWindow.spin();
- Access same window via its name. Since windows are implicitly shared, sameWindow is exactly the same with myWindow. If the name does not exist, a new window is created.
  viz::Viz3d sameWindow = viz::getWindowByName("Viz Demo");
- Start a controlled event loop. Once it starts, wasStopped is set to false. Inside the while loop, in each iteration, spinOnce is called to prevent event loop from completely stopping. Inside the while loop, user can execute other statements including those which interact with the window.
  sameWindow.spinOnce(1, true);
  while(!sameWindow.wasStopped())
  {
  sameWindow.spinOnce(1, true);
  }

# Results

Here is the result of the program.