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'
According to the documentation, you need to give the same window name which you specified in
namedWindowSo you need to change windowname or
createTrackbarparameter name.