opencv: error: (-2:Unspecified error) Can't initialize GTK backend in function 'cvInitSystem'

System information (version)
  • OpenCV => 3.4
  • Operating System / Platform => aarch64 (debian based OS)
  • Compiler => g++ 8.3.0
Detailed description

I have opencv as an external dependency that I compile from source like this:

ExternalProject_Add(opencv
  GIT_REPOSITORY https://github.com/opencv/opencv
  GIT_TAG "3.4"
  SOURCE_DIR opencv
  BINARY_DIR opencv-build
  CMAKE_ARGS
    -DCMAKE_BUILD_TYPE=RELEASE 
    -DFORCE_VTK=ON
    -DWITH_TBB=ON
    -DWITH_V4L=ON
    -DWITH_QT=OFF
    -DWITH_GTK_2_x=ON
    -DWITH_OPENGL=ON
    -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/opencv_install
)
set(OPENCV_INCLUDE_DIR ${CMAKE_BINARY_DIR}/opencv_install/include/)
set(OPENCV_LIBRARY_DIR ${CMAKE_BINARY_DIR}/opencv_install/lib/)
set(OPENCV_LIBRARIES opencv_imgproc opencv_core opencv_highgui opencv_video opencv_videoio opencv_imgcodecs opencv_features2d)
if(EXISTS "${CMAKE_BINARY_DIR}/opencv_install/share/OpenCV/OpenCVConfig.cmake")
    include(${CMAKE_BINARY_DIR}/opencv_install/share/OpenCV/OpenCVConfig.cmake)
    add_custom_target(rerun)
else()
    add_custom_target(rerun ${CMAKE_COMMAND} ${CMAKE_SOURCE_DIR} DEPENDS opencv)
endif()

Everything compiles just fine until I run the program, an error like this appears:

terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(3.4.12-pre) /media/sd/nam-detect/edgetpu-detection-camera/build/opencv/modules/highgui/src/window_gtk.cpp:624: error: (-2:Unspecified error) Can't initialize GTK backend in function 'cvInitSystem'

Aborted

libgtk2 is definitely installed:

$ sudo apt search libgtk2.0-dev
Sorting... Done
Full Text Search... Done
libgtk2.0-dev/eagle,now 2.24.32-3 arm64 [installed]
  development files for the GTK+ library
Steps to reproduce

This code, which used to works without issue: https://github.com/Namburger/edgetpu-detection-camera We are only seeing this behavior on one of the system o_0

Any suggestion would be highly appreciated!

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 3
  • Comments: 29 (3 by maintainers)

Most upvoted comments

I managed to solve it by making sure when I ssh into aarch64 that I use ssh -Y user@host. This gives authorization to allow cv2.imshow() to display the window. gtk_init_check error solved hope that helps

To help others, I found that my .bashrc needed to contain the following: export DISPLAY=:0.0 previously, it said export DISPLAY=localhost:0.0

For what it’s worth, I’m using xvfb-run -a <my program>.

I also had the same issue when running inside of a docker container. To fix the issue and gain access to the X server and the display I found this tutorial very helpful.

http://wiki.ros.org/docker/Tutorials/GUI

I managed to solve it by making sure when I ssh into aarch64 that I use ssh -Y user@host. This gives authorization to allow cv2.imshow() to display the window. gtk_init_check error solved hope that helps

I solved the problem through this method. There is no GUI on the server side, so I need to comment out imshow.

If you use SSH to connect you divice or your progress is a server ,and nowhere to display any image.So please remove the code related windows or display.for example imshow()

ashrc

where do you get that .bashrc file? when i nano ~/.bashrc i didnt see anything about display

For me the export DISPLAY=localhost:0.0 also wasn’t there in the .bashrc file before. Still adding export DISPLAY=:0.0 to the end of the .bashrc file of the user calling the software, in my case pi, worked fine.

I managed to solve it by making sure when I ssh into aarch64 that I use ssh -Y user@host. This gives authorization to allow cv2.imshow() to display the window. gtk_init_check error solved hope that helps

I tried this on Ubuntu 20.04 but still no luck

To help others, I found that my .bashrc needed to contain the following: export DISPLAY=:0.0 previously, it said export DISPLAY=localhost:0.0

this method is effective