OpenCV: error: (-2:Unspecified error) Can’t initialize GTK backend in function ‘cvInitSystem’

  c++, gtk, opencv, raspberry-pi

I’ve compiled a simple c++ program (pretty much off the opencv website) on my pi4 that opens a web cam for video streaming. Program compiles, however on execute I receive the following error:

terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(4.5.2) /home/cam/dev/opencv/modules/highgui/src/window_gtk.cpp:624: error: (-2:Unspecified error) Can't initialize GTK backend in function 'cvInitSystem'

Aborted

Yes I have libgtk2.0-dev and pkg-config installed.

My CMake file:

cmake_minimum_required(VERSION 3.0.0)
project(opencv_tests VERSION 0.1.0)

include(CTest)
enable_testing()

add_executable(opencv_tests main.cpp)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

find_package( OpenCV REQUIRED )

if(OpenCV_FOUND)
    target_include_directories(opencv_tests PUBLIC "${OpenCV_INCLUDE_DIRS}")
    target_link_libraries(opencv_tests PUBLIC "${OpenCV_LIBS}")
endif(OpenCV_FOUND)

Source: Windows Questions C++

LEAVE A COMMENT