Home > Software design >  Open CV Trackbar on Python cannot run
Open CV Trackbar on Python cannot run

Time:12-15

I am trying to write a program to see an image in real time from a webcam based off of an hsv value range. When I run the program I am able to get the webcam to work (it shows a black screen as expected) but the trackbars to adjust the hsv range are not showing for some reason.


import numpy as np
import cv2

webcam = cv2.VideoCapture(0)

def nothing(x):
    pass

cv2.namedWindow("Trackbar")

cv2.createTrackbar("L-H", "Track", 0, 180, nothing)
cv2.createTrackbar("L-S", "Track", 0, 255, nothing)
cv2.createTrackbar("L-V", "Track", 0, 255, nothing)
cv2.createTrackbar("U-H", "Track", 180, 180, nothing)
cv2.createTrackbar("U-S", "Track", 255, 255, nothing)
cv2.createTrackbar("U-V", "Track", 255, 255, nothing)

#webcam=cv2.imread('macet1.jpg',0)

while(1):
    # webcam in image frames
    _, imageFrame = webcam.read()

    # color space
    hsvFrame = cv2.cvtColor(imageFrame, cv2.COLOR_BGR2HSV)
    l_h = cv2.getTrackbarPos("L-H", "Track")
    l_s = cv2.getTrackbarPos("L-S", "Track")
    l_v = cv2.getTrackbarPos("L-V", "Track")
    u_h = cv2.getTrackbarPos("U-H", "Track")
    u_s = cv2.getTrackbarPos("U-S", "Track")
    u_v = cv2.getTrackbarPos("U-V", "Track")

    lower_red = np.array([l_h, l_s, l_v])#ubah l_h, l_s, l_v sesuai nilai
    upper_red = np.array([u_h, u_s, u_v])
    mask = cv2.inRange(hsvFrame, lower_red, upper_red)

    kernel = np.ones((5,5), np.uint8)
    mask = cv2.erode(mask, kernel)

    cv2.imshow("Mask",mask)
``
    cv2.imshow("Multiple Color Detection in Real-TIme", imageFrame)
    if cv2.waitKey(10) & 0xFF == ord('q'):
        cap.release()
        cv2.destroyAllWindows()
        break

File "/home/pi/skripsi/FindupperlowerHSV.py", line 14, in cv2.createTrackbar("L-H", "Track", 0, 180, nothing) cv2.error: OpenCV(4.6.0) /io/opencv/modules/highgui/src/window_QT.cpp:495: error: (-27:Null pointer) NULL window handler in function 'icvFindTrackBarByName'

CodePudding user response:

According to the documentation, you need to give the same window name which you specified in namedWindow

winname: Name of the window that will be used as a parent of the created trackbar.

So you need to change windowname or createTrackbar parameter name.

cv2.namedWindow("Track")

CodePudding user response:

Does this will help. Using RPi 4B. Look in line 61 - 68.

import cv2
import numpy as np

def nothing(x):
    # any operation
    pass

cap = cv2.VideoCapture(0)

cv2.namedWindow("TRACK")
cv2.createTrackbar("L-H","TRACK",118,180,nothing)
cv2.createTrackbar("L-S","TRACK",97,255,nothing)
cv2.createTrackbar("L-V","TRACK",0,255,nothing)
cv2.createTrackbar("U-H","TRACK",180,180,nothing)
cv2.createTrackbar("U-S","TRACK",255,255,nothing)
cv2.createTrackbar("U-V","TRACK",255,255,nothing)

font = cv2.FONT_HERSHEY_SIMPLEX

while True :
    _, frame = cap.read()

    hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)

    l_h = cv2.getTrackbarPos("L-H","TRACK")
    l_s = cv2.getTrackbarPos("L-S", "TRACK")
    l_v = cv2.getTrackbarPos("L-V", "TRACK")
    u_h = cv2.getTrackbarPos("U-H", "TRACK")
    u_s = cv2.getTrackbarPos("U-S", "TRACK")
    u_v = cv2.getTrackbarPos("U-V", "TRACK")


    lower_red = np.array([l_h,l_s,l_v])
    upper_red = np.array([u_h,u_s,u_v])

    mask = cv2.inRange(hsv,lower_red, upper_red)
    kernal = np.ones((2,2), np.uint8)
    mask = cv2.erode(mask, kernal)

    contours,_ = cv2.findContours(mask, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

    for cnt in contours :
        area = cv2.contourArea(cnt)
        approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)

        x = approx.ravel()[0]
        y = approx.ravel()[1]

        if area > 400 :
            cv2.drawContours(frame, [approx], 0, (0, 0, 255), 5)

            if len(approx) == 4:
                cv2.putText(mask, "rectangle",(x,y), font,1,(255,255,255),2)


        print(len(approx))

    cv2.imshow("Frame", frame)
    cv2.imshow("mask", mask)

    if cv2.waitKey(1) &0xFF == ord('q'):
        break


cap.release ()
cv2.destroyAllWindows()
  • Related