-2 opencv build_error ai_generated true

cv2.error: OpenCV(4.7.0) /tmp/opencv-4.7.0/modules/highgui/src/window.cpp:589: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvNamedWindow'

ID: opencv/highgui-window-already-exists

Also available as: JSON · Markdown · 中文
85%Fix Rate
92%Confidence
1Evidence
2023-02-28First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
4.5.5 active
4.6.0 active
4.7.0 active
4.8.0 active
4.9.0 active

Root Cause

OpenCV was built without GUI backend (GTK, Cocoa, or Windows) support, so highgui functions like imshow, namedWindow are unavailable.

generic

中文

OpenCV 在构建时没有包含 GUI 后端(GTK、Cocoa 或 Windows)支持,因此 imshow、namedWindow 等高 GUI 函数不可用。

Official Documentation

https://docs.opencv.org/4.x/d7/dfc/group__highgui.html

Workarounds

  1. 85% success Install opencv-python with GUI support: `pip uninstall opencv-python-headless && pip install opencv-python`
    Install opencv-python with GUI support: `pip uninstall opencv-python-headless && pip install opencv-python`
  2. 80% success Install GTK development libraries and rebuild OpenCV from source: `sudo apt-get install libgtk2.0-dev pkg-config`, then `cmake -DWITH_GTK=ON .. && make`
    Install GTK development libraries and rebuild OpenCV from source: `sudo apt-get install libgtk2.0-dev pkg-config`, then `cmake -DWITH_GTK=ON .. && make`
  3. 95% success Use matplotlib to display images instead of OpenCV's highgui: `plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)); plt.show()`
    Use matplotlib to display images instead of OpenCV's highgui: `plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)); plt.show()`

中文步骤

  1. Install opencv-python with GUI support: `pip uninstall opencv-python-headless && pip install opencv-python`
  2. Install GTK development libraries and rebuild OpenCV from source: `sudo apt-get install libgtk2.0-dev pkg-config`, then `cmake -DWITH_GTK=ON .. && make`
  3. Use matplotlib to display images instead of OpenCV's highgui: `plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)); plt.show()`

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Installing opencv-python-headless doesn't include GUI support; it's designed for headless environments.

  2. 80% fail

    Setting environment variables like DISPLAY doesn't add missing GUI backend code.

  3. 70% fail

    Using cv2.imshow in a Jupyter notebook still requires the GUI backend; the error persists.