Opencv-python trackbar crash when I slide

Hi everyone,
I created a palette using trackbar,
When I slide, the window closes,

error message:

[1]    87735 segmentation fault  python

this is my code:

import cv2
import numpy as np

def callback(x):

cv2.namedWindow('trackbar', cv2.WINDOW_NORMAL)

cv2.createTrackbar('R', 'trackbar', 0, 255, callback)
cv2.createTrackbar('G', 'trackbar', 0, 255, callback)
cv2.createTrackbar('B', 'trackbar', 0, 255, callback)

img = np.zeros((480, 460, 3), np.uint8)

while True:
    r = cv2.getTrackbarPos('R', 'trackbar')
    g = cv2.getTrackbarPos('G', 'trackbar')
    b = cv2.getTrackbarPos('B', 'trackbar')
    img[:] = [b, g, r]
    cv2.imshow('trackbar', img)

    key = cv2.waitKey(0)
    if key & 0xff == ord('q'):


Macbook air M1
Python 3.10.4 

Does anyone know how to solve it :sob::sob:

please tell us, which gui toolkit is used here

First you obviously need to debug your code and find out the line and expression that segfaults.

It may not even have anything to do with sliding.

General configuration for OpenCV 4.6.0 =====================================
  Version control:               4.6.0

    Timestamp:                   2022-06-07T10:22:17Z
    Host:                        Darwin 20.2.0 arm64
    CMake:                       3.22.3
    CMake generator:             Unix Makefiles
    CMake build tool:            /usr/bin/make
    Configuration:               Release

  CPU/HW features:
    Baseline:                    NEON FP16

    Built as dynamic libs?:      NO
    C++ standard:                11
    C++ Compiler:                /Applications/  (ver
    C++ flags (Release):         -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections    -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
    C++ flags (Debug):           -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections    -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    C Compiler:                  /Applications/
    C flags (Release):           -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections    -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
    C flags (Debug):             -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections    -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    Linker flags (Release):      -Wl,-dead_strip  
    Linker flags (Debug):        -Wl,-dead_strip  
    ccache:                      YES
    Precompiled headers:         NO
    Extra dependencies:          -framework OpenCL /Applications/ -lm -ldl -framework Cocoa -framework AppKit
    3rdparty dependencies:       libprotobuf ade ittnotify libjpeg-turbo libwebp libpng libtiff libopenjp2 IlmImf zlib quirc tegra_hal

  OpenCV modules:
    To be built:                 calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo python3 stitching video videoio
    Disabled:                    world
    Disabled by dependency:      -
    Unavailable:                 java python2 ts
    Applications:                -
    Documentation:               NO
    Non-free algorithms:         NO

  GUI:                           COCOA
    Cocoa:                       YES
    VTK support:                 NO

  Media I/O: 
    ZLib:                        build (ver 1.2.12)
    JPEG:                        build-libjpeg-turbo (ver 2.1.2-62)
    WEBP:                        build (ver encoder: 0x020f)
    PNG:                         build (ver 1.6.37)
    TIFF:                        build (ver 42 - 4.2.0)
    JPEG 2000:                   build (ver 2.4.0)
    OpenEXR:                     build (ver 2.3.0)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:
    DC1394:                      NO
    FFMPEG:                      YES
      avcodec:                   YES (58.134.100)
      avformat:                  YES (58.76.100)
      avutil:                    YES (56.70.100)
      swscale:                   YES (5.9.100)
      avresample:                YES (4.0.0)
    GStreamer:                   NO
    AVFoundation:                YES

  Parallel framework:            GCD

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Lapack:                      YES (/Applications/ -lm -ldl)
    Eigen:                       NO
    Custom HAL:                  YES (carotene (ver 0.0.1))
    Protobuf:                    build (3.19.1)

  OpenCL:                        YES (no extra features)
    Include path:                NO
    Link libraries:              -framework OpenCL

  Python 3:
    Interpreter:                 /Users/xperience/.pyenv/versions/3.7.10/bin/python3.7 (ver 3.7.10)
    Libraries:                   /Users/xperience/.pyenv/versions/3.7.10/lib/libpython3.7m.a (ver 3.7.10)
    numpy:                       /Users/xperience/.pyenv/versions/3.7.10/lib/python3.7/site-packages/numpy/core/include (ver 1.21.0)
    install path:                python/cv2/python-3

  Python (for build):            /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7

    ant:                         NO
    JNI:                         NO
    Java wrappers:               NO
    Java tests:                  NO

  Install to:                    /Users/xperience/actions-runner/_work/opencv-python/opencv-python/_skbuild/macosx-11.0-arm64-3.7/cmake-install

I use pip to install

I use single step debugging
When the code runs to
key=cv2. waitKey (0)
it will crash

yeah, highgui trackbars are buggy on apple

let the devs know, and raise an issue

better yet, check for existing issues.

This macOs ARM related issue has been solved with the rolling built packages:

Use pip install opencv-contrib-python-rolling for example. (v at the time of writing).

Issue: OpenCV Trackbar Crash on ARM Mac · Issue #22561 · opencv/opencv · GitHub

1 Like