Kernel crashing on clicking trackbar

I’m trying out the openCV tutorial on edge detection from here: OpenCV: Canny Edge Detector and on clicking the crashbar my kernel crashes.

I don’t think it’s an issue with the code since I tried it with some other simple trackbar code I found on stackoverflow, but here’s the main part of my code (just slight changes to the tutorial):

def canny_threshold(low_val, src, src_gray):
    low_threshold = low_val
    img_blur = cv.blur(src_gray, (3,3))
    detected_edges = cv.Canny(img_blur, low_threshold, low_threshold * RATIO)
    mask = detected_edges != 0
    dst = src * (mask[:,:, None].astype(src.dtype))
    return dst


src = cv.imread("magpie_house.jpeg")
src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)

def nothing(x):
    pass


cv.namedWindow(WINDOW_NAME)
cv.startWindowThread()
cv.createTrackbar(TITLE_TRACK_BAR, WINDOW_NAME, 0, MAX_LOW_THRESHOLD, nothing)
cv.setTrackbarPos(TITLE_TRACK_BAR, WINDOW_NAME, 50)


while True:
    COUNTER += 1
    if COUNTER >= 700:
        break
    low_val = cv.getTrackbarPos(TITLE_TRACK_BAR, WINDOW_NAME)
    dst = canny_threshold(low_val, src, src_gray)
    cv.imshow(WINDOW_NAME, dst)
    if cv.waitKey(10) & 0xFF == ord("q"):
        break
    

cv.waitKey(1)
cv.destroyAllWindows()

Can you help with this?

“crashing”, how exactly? what kernel? provide some details about your environment too.

crosspost: OpenCV Python kernel crashes when clicking the trackbar - Stack Overflow

Crashing as in the window crashes, and the jupyter kernel crashes (all state variables are lost).

I’m using a Mac. My opencv-python version is 4.6.0.66 and python ver is 3.10. Anything else might help?

You think it is not caused by your code, but think what is diffrerent with where it works? Your code.

You should start by 1) testing the code without any image processing, and then 2) making it more robust by checking every function call for success etc and 3) finding out by, selective removals, which line causes the crashing

Note that crashing a windowed app may mean the window disappearing or freezing, but more often or not there is some message printed in the console.

No, I have tried copying the exact code from the tutorial and I get the same issue (i have actually done this with multiple different working code samples and still get the issue).

I have tried it without image processing as well, I tried a sample code where there was just a slider that did nothing, and that crashed as well.

Also the issue is not with imshow itself, since that works fine. I’ve tested that.

OpenCV’s trackbars on macos are broken (were at least).

don’t be surprised.

there are issues on the github about this.

it seems like nobody actually does any computer vision on a mac because nobody seems very much interested in fixing this.

Good to know, thanks.