opencv_contrib
opencv_contrib copied to clipboard
viz3D spinOnce method broken with latest stable version of VTK
System information (version)
- OpenCV => 4.5.4
- VTK => 9.1.0
- Python => 3.9.7
- Operating System / Platform => Arch Linux x64
- Compiler => :grey_question:
Detailed description
spinOnce method used to work great in while loops to enable animation in a Viz window by doing:
while not v.wasStopped():
v.spinOnce(1, True)
while (v.wasStopped())
{
v.spinOnce(1, true);
}
Now the window freezes and no mouse nor keyboard event is recognized thus impossible to cleanly close the windows or rotate the camera in the scene. This happens both in Python and C++.
Sometimes it leads to a X server error:
X Error of failed request: BadWindow (invalid Window parameter)
Major opcode of failed request: 38 (X_QueryPointer)
Resource id in failed request: 0x5800002
Serial number of failed request: 1036
Current serial number in output stream: 1036
Steps to reproduce
import cv2
v = cv2.viz.Viz3d_create("Viz Demo")
v.showWidget("axes", cv2.viz_WCameraPosition(1))
while not v.wasStopped():
v.spinOnce(1, True)
#include <opencv4/opencv2/viz.hpp>
using namespace cv;
int main()
{
viz::Viz3d win("Test");
win.showWidget("Coords", viz::WCoordinateSystem());
while (!win.wasStopped())
{
win.spinOnce(1, true);
}
return 0;
}
Issue submission checklist
- [ x ] I report the issue, it's not a question
- [ x ] I checked the problem with documentation, FAQ, open issues, answers.opencv.org, Stack Overflow, etc and have not found solution
- [ x ] I updated to latest OpenCV version and the issue is still there
- [ x ] There is reproducer code and related data files: videos, images, onnx, etc
I am also facing this problem. I keep getting a segmentation fault. Are there any updates?
I have the same issue, using the current latest version as I'm writing this.
- Using C++
- VTK 9.2.0_rc2
- OpenCV 4.6.x
- OpenCV_contrib 4.6.x
Errors in terminal (copied by hand so i've omitted some unhelpful stuff, such as data, time and instance pointer location) : vtkXOpenGLRenderWindow.:266 : Could not find a decent config vtkXOpenGLRenderWindow.:484 : Could not find a decent visual
Doing some detective work, I found that SpinOnce works with the following setup :
- OpenCV 4.5.5
- OpenCV_contrib 4.5.5
- VTK 9.1.0
In the PCL Library, the same problem occured. A description of the issue may be found, https://github.com/PointCloudLibrary/pcl/pull/5252.
With following changes in vizimpl.cpp, the spinOnce example above seem to work with current VTK 9.2. The threads standard library need to be included to compile the workaround below.
void cv::viz::Viz3d::VizImpl::spinOnce(int time, bool force_redraw)
{
...
// Call initialize before creating timer event, see https://kitware.github.io/vtk-examples/site/Cxx/Utilities/Timer/
interactor_->Initialize();
interactor_->AddObserver(vtkCommand::TimerEvent, timer_callback_);
interactor_->AddObserver(vtkCommand::ExitEvent, exit_callback_);
...
#if VTK_MAJOR_VERSION >= 9 && (VTK_MINOR_VERSION != 0 || VTK_BUILD_VERSION != 0) && (VTK_MINOR_VERSION != 0 || VTK_BUILD_VERSION != 1)
// All VTK 9 versions, except 9.0.0 and 9.0.1
// see https://github.com/PointCloudLibrary/pcl/blob/master/visualization/src/pcl_visualizer.cpp
if(local->IsA("vtkXRenderWindowInteractor")) {
local->ProcessEvents ();
std::this_thread::sleep_for (std::chrono::milliseconds (time));
}
else
#endif
{
timer_callback_->timer_id = local->CreateRepeatingTimer(std::max(1, time));
local->Start();
local->DestroyTimer(timer_callback_->timer_id);
}
}
Running OpenCV 4.7.0 and VTK 9.2.3 on ArchLinux causes spinOnce to recreate the Viz3d window on every call. I can confirm that the suggestion from @roderick-koehle fixes the problem.
There seems also to be an issue with the spin method which segfaults if it is called after spineOnce. I see the following stacktrace:
Thread 1 "app" received signal SIGSEGV, Segmentation fault.
0x00007fffe63de088 in vtkXRenderWindowInteractor::ProcessEvents() () from /home/sergiu/Projects/devel/vtk-9.2.3/lib/libvtkRenderingUI-9.2.so.1
Both problems are possibly related.
I also observe an issue with the window update which is redrawn only if I move the mouse cursor over the window. It is however not clear whether this is a side effect of the changes proposed above or a completely separate issue.