'Incompatibility issues between OpenCV and GTK+ 3

I am developing a real-time screen-capturing program using Python 3.10.4 with OpenCV and GTK+ 3. I ran into an issue I cannot solve - GTK+ 3 seems to be incompatible with OpenCV. To give a bit more context to my problem, I am using GTK+ 3 to capture each frame from any selected window (minimized or not) using GdkPixbuf and I am using OpenCV to efficiently process and show each captured frame in real-time.

Here's the issue. Consider the following simple example:

import numpy as np
import cv2
import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

img = np.zeros((100,100,3))
cv2.imshow('test', img) # ERROR happens here
cv2.waitKey(0)

The following code produces a segmentation fault (SIGSEGV) (exit code 139, signal 11). The segmentation fault happens exactly when cv2.imshow() is called. If, however, I were to change GTK version to 2, by writting gi.require_version("Gtk", "2.0") or remove it altogether, the SIGSEGV does not occur. This is not a solution, since I need GTK+ 3 at the very least.

So far, I tried downgrading OpenCV, but no other version worked. What I found to be working is PyGTK in place of GTK+ 3, but PyGTK is very old and depreciated - I do not want to use it.

How can I make GTK+ 3 work with OpenCV? Is it even possible?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source