Skip to content
Search

OpenCV:Troubleshooting

lastmod 2025-11-14created 2017-02-0619 edits

Not found QTKit

After updating macOS, OpenCV 3.1.0 fails with an error saying it cannot find QTKit.

/Users/your/Project/tparty/tmp/build/opencv-3.1.0/3rdparty/zlib/inflate.c:1507:61: warning: shifting a negative signed value is undefined
      [-Wshift-negative-value]
    if (strm == Z_NULL || strm->state == Z_NULL) return -1L << 16;
                                                        ~~~ ^
1 warning generated.
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: ../lib/liblibtiff.a(tif_jbig.c.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: ../lib/liblibtiff.a(tif_jpeg_12.c.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: ../lib/liblibtiff.a(tif_jpeg.c.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: ../lib/liblibtiff.a(tif_lzma.c.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: ../lib/liblibtiff.a(tif_ojpeg.c.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: ../lib/liblibtiff.a(tif_jbig.c.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: ../lib/liblibtiff.a(tif_jpeg_12.c.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: ../lib/liblibtiff.a(tif_jpeg.c.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: ../lib/liblibtiff.a(tif_lzma.c.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: ../lib/liblibtiff.a(tif_ojpeg.c.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: ../lib/libIlmImf.a(IlmThread.cpp.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: ../lib/libIlmImf.a(IlmThreadMutex.cpp.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: ../lib/libIlmImf.a(IlmThreadSemaphore.cpp.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: ../lib/libIlmImf.a(IlmThreadSemaphorePosixCompat.cpp.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: ../lib/libIlmImf.a(IlmThread.cpp.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: ../lib/libIlmImf.a(IlmThreadMutex.cpp.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: ../lib/libIlmImf.a(IlmThreadSemaphore.cpp.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: ../lib/libIlmImf.a(IlmThreadSemaphorePosixCompat.cpp.o) has no symbols
/Users/your/Project/tparty/tmp/build/opencv-3.1.0/modules/videoio/src/cap_qtkit.mm:46:9: fatal error: 'QTKit/QTKit.h' file not found
#import <QTKit/QTKit.h>
        ^
1 error generated.
make[2]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_qtkit.mm.o] Error 1
make[2]: *** Waiting for unfinished jobs....
make[1]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
make: *** [all] Error 2

Update to the latest version.

Unable to stop the stream: Inappropriate ioctl for device

The following error may appear in OpenCV3.

Unable to stop the stream: Inappropriate ioctl for device

This error can occur in VideoCapture and elsewhere; recompile together with FFmpeg.

Add the following CMake options.

-DBUILD_PNG=1
-DBUILD_JPEG=1
-DWITH_CUDA=1
-DWITH_FFMPEG=1

Arrow key input in waitKey

With waitKey(), pressing an arrow key returns 0. Try waitKeyEx().

HIGHGUI ERROR: V4L/V4L2: VIDIOC_S_CROP

== Assertion failed (p.checkVector(2, CV_32S) >= 0) in polylines ==

OpenCV Error: Assertion failed (p.checkVector(2, CV_32S) >= 0) in polylines, file /tmp/buildd/ros-fuerte-opencv2-2.4.2-1precise-20130312-1306/modules/core/src/d rawing.cpp, line 2065

The problem in my case was that numpy.array created int64-bit numbers by default. So I had to explicitly convert it to int32:

points = np.array([[910, 641], [206, 632], [696, 488], [458, 485]])
# points.dtype => 'int64'
cv2.polylines(img, np.int32([points]), 1, (255,255,255))

(Looks like a bug in cv2 python binding, it should've verified dtype)

unsafe use of relative rpath

On MacOSX, the following error message may appear the moment you type import cv2 to use OpenCV-Python.

>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dlopen(/usr/local/lib/python2.7/site-packages/cv2.so, 2): Library not loaded: lib/libopencv_shape.3.0.dylib
  Referenced from: /usr/local/lib/python2.7/site-packages/cv2.so
  Reason: unsafe use of relative rpath lib/libopencv_shape.3.0.dylib in /usr/local/lib/python2.7/site-packages/cv2.so with restricted binary

In this case, recompile as a static library using cmake -DBUILD_SHARED_LIBS=OFF.

libdc1394 error

The following error may occur at import cv;.

root@7be8cca34837:/# python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv
libdc1394 error: Failed to initialize libdc1394

It can be fixed as follows.

sudo ln /dev/null /dev/raw1394

libGL.so.1: cannot open shared object file

If the following error is printed

ImportError: libGL.so.1: cannot open shared object file: No such file or directory

install the OpenGL library.

sudo apt-get install libgl1-mesa-glx

If you are running on a server, install the opencv-python-headless package instead of the opencv-python package.

Symbols not found in PyCharm

There are several solutions.

Downgrade to version 4.5.4.60,

pip install -U opencv-python==4.5.4.60

or use the cv2.cv2 module (with this method symbols are found, but it may not run correctly),

from cv2 import cv2

or create a pyi file yourself and put it in site-packages/cv2. For this method, see PEP 484#Stub Files.

If you want to use a prebuilt pyi file, you can also download it directly from python-type-stubs.

# you can set it to "pipenv" or "poetry"
_pip=poetry
# _pip=pipenv
curl -sSL https://raw.githubusercontent.com/microsoft/python-type-stubs/main/cv2/__init__.pyi \
    -o $($_pip run python -c 'import cv2, os; print(os.path.dirname(cv2.__file__))')/__init__.pyi
unset _pip

A somewhat more useful(?) script version:

#!/usr/bin/env bash

ROOT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" || exit; pwd)

function print_error
{
    # shellcheck disable=SC2145
    echo -e "\033[31m$@\033[0m" 1>&2
}

function print_message
{
    # shellcheck disable=SC2145
    echo -e "\033[32m$@\033[0m"
}

trap 'cancel_black' INT

function cancel_black
{
    print_error "An interrupt signal was detected."
    exit 1
}

if "$ROOT_DIR/python" -m pip show -q opencv-python &> /dev/null; then
    print_error "Not found cv2 package"
    exit 1
fi

DOWNLOAD_URL="https://raw.githubusercontent.com/microsoft/python-type-stubs/main/cv2/__init__.pyi"
DEST_DIR=$("$ROOT_DIR/python" -c 'import cv2, os; print(os.path.dirname(cv2.__file__))')

curl -sSL "$DOWNLOAD_URL" -o "$DEST_DIR/__init__.pyi"

Corrupted images in cv2.VideoCapture

A problem caused by an unstable network. Use a profile with a lower source video resolution, or if it is RTSP, try receiving over TCP instead of UDP.

You can pass options to FFmpeg using an environment variable as follows:

import os

os.environ["OPENCV_FFMPEG_CAPTURE_OPTIONS"] = "rtsp_transport;tcp"

Freezing when calling the High-level GUI

Calling one of OpenCV's High-level GUI functions (e.g. cv2.namedWindow, cv2.imshow, etc ...) can cause the program to freeze.

In this case, (usually) a video-related library may be conflicting.

Known conflicting libraries are:

  • torchvision
  • If you imported pyav, it may be an ffmpeg DLL loading problem.

Try another approach, such as using tkinter. See OpenCV:Example:Tkinter for a related example.

Or just call cv2.namedWindow as soon as the program starts.

import cv2
cv2.namedWindow(WINDOW_TITLE, WINDOW_FLAGS)
## 이후 문제가 되는 라이브러리 import
import torchvision

If the import code is concentrated at the top of the script, it may be invoked without your knowledge because of __init__.py. Be careful.

Screen freezes in imshow

See the parent section, #Freezing when calling the High-level GUI. Kept because other pages reference it.

Hangs when calling cv2.namedWindow()

See the parent section, #Freezing when calling the High-level GUI. Kept because other pages reference it.

Cleaned-up routine

Python code stub for easy copy-paste:

window_title = getattr(args, "window_title", WINDOW_TITLE)
window_flags = getattr(args, "window_flags", WINDOW_FLAGS)

assert isinstance(args.no_preload_highgui, bool)
if not args.headless and not args.no_preload_highgui:
    import cv2

    # noinspection SpellCheckingInspection
    """
    [IMPORTANT]
    There are many libraries that conflict with OpenCV's highgui module.
    In this case, freezing may occur in functions such as `cv2.imshow`.
    To prevent this problem, highgui-related modules are imported first.
    """
    cv2.namedWindow(window_title, window_flags)

Translation of the English part:

[중요]
OpenCV의 highgui 모듈과 충돌하는 라이브러리가 많이 있습니다.
이 경우 `cv2.imshow` 등의 함수에서 프리징 현상이 발생할 수 있습니다.
이 문제를 방지하기 위해 highgui 관련 모듈을 먼저 임포트 합니다.

qt.qpa.xcb: could not connect to display

qt.qpa.xcb: could not connect to display
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/bbb/Project/ddrm/.venv/lib/python3.11/site-packages/cv2/qt/plugins" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: xcb.

./python: line 345: 3747217 Aborted                 (core dumped) PYTHONPATH="$ROOT_DIR:$PYTHONPATH" "$PYTHON_EXE" "$@"

Seems the x11 DISPLAY is not connected.

module 'cv2.cv2' has no attribute 'TrackerCSRT_create'

module 'cv2.cv2' has no attribute 'TrackerCSRT_create'

OpenCV Tracking modules such as SIFT and SURF are not supported in version 4, so you need to downgrade. Therefore, remove version 4 and reinstall an earlier version.

Remove the current version:

pip uninstall opencv-python
pip uninstall opencv-contrib-python

Install an earlier version (it may fail; just pick one of the versions recommended in the warning message)

pip install opencv-python==3.4.11.45
pip install opencv-contrib-python==3.4.11.45

Numpy 1.x installation issue

If the following error occurs:

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
opencv-python 4.12.0.88 requires numpy<2.3.0,>=2; python_version >= "3.9", but you have numpy 1.26.4 which is incompatible.

Try changing the opencv-python version to 4.6.0.66. (Not sure whether it is the last version that supports 1.x)

See also

author Zer0 sitemap