python: cv2.error "NULL window Handler" in linux Mint

Asked

Viewed 8 times

0

I’m running a script to learn how to use trackBar in Opencv within Linux Mint. The problem is that sometimes I run and it works normally, sometimes it gives a "Null window Handler" error and other times it performs bugle. The 3 possibilities for implementation are set out below.

When it runs correctly, the sliders appear to be able to choose the values:

janela funcional

When it runs but with problems, neither appear the sliders to choose the values (I have no idea why): janela nao funcional

And when it doesn’t even run and only release the error on the console: sem janela, so erro

I’d like to understand why sometimes the code works and sometimes it doesn’t.. I am afraid of being a problem with linux Mint or version of Opencv (4.3.0). And the script I am running is this:

import numpy as np
import cv2 as cv

def nothing(x):
    print(x)

# Create a black image, a window
img = np.zeros((300,512,3), np.uint8)
cv.namedWindow('image')

cv.createTrackbar('B', 'image', 0, 255, nothing)
cv.createTrackbar('G', 'image', 0, 255, nothing)
cv.createTrackbar('R', 'image', 0, 255, nothing)

switch = '0 : OFF\n 1 : ON'
cv.createTrackbar(switch, 'image', 0, 1, nothing)

while(1):
    cv.imshow('image',img)
    k = cv.waitKey(1) & 0xFF
    if k == 27:
        break

    b = cv.getTrackbarPos('B', 'image')
    g = cv.getTrackbarPos('G', 'image')
    r = cv.getTrackbarPos('R', 'image')
    s = cv.getTrackbarPos(switch, 'image')

    if s == 0:
       img[:] = 0
    else:
       img[:] = [b, g, r]


cv.destroyAllWindows()
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.