Hello. I’ve weird problem with many codes, for example, I wrote simple program to bitwise, and program to show figures, trackbar and i had dark screen like this in photo. I reinstalled opencv and numpy, but they didn’t helped. I also tried python reinstall.
I’ll be very thankful for any help.
Here is a code for trackbar program:
import cv2
import numpy as np
def showcolour(x):
print(x)
img = np.zeros((300,512,3), np.uint8)
cv2.namedWindow('img')
cv2.createTrackbar('b', 'blue', 0, 255, showcolour)
cv2.createTrackbar('g', 'green', 0, 255, showcolour)
cv2.createTrackbar('r', 'red', 0, 255, showcolour)
while(1):
cv2.imshow('image', img)
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
b = cv2.getTrackbarPos('b', 'blue')
g = cv2.getTrackbarPos('g', 'green')
r = cv2.getTrackbarPos('r', 'red')
img[:] = [b,g,r]
cv2.imshow('img', img)
cv2.destroyAllWindows()
Thank you very much. I added code to loop, wrote waitKey, make variable with window name, and used it, and code worked. I think I’ll should learn from documentation, not from other sources.
import cv2
import numpy as np
# --- functions ---
def showcolour(value):
print(value)
# --- main ---
# create black image at start
img = np.zeros((300,512,3), np.uint8)
# create window with name `img`
cv2.namedWindow('img')
# add trackbars to window `img` - and assign function `showcolour` to trackbars
cv2.createTrackbar('b', 'img', 0, 255, showcolour)
cv2.createTrackbar('g', 'img', 0, 255, showcolour)
cv2.createTrackbar('r', 'img', 0, 255, showcolour)
# run main loop
while True:
# get values from trackbars in window `img`
b = cv2.getTrackbarPos('b', 'img')
g = cv2.getTrackbarPos('g', 'img')
r = cv2.getTrackbarPos('r', 'img')
# replace all pixels with new value
img[:] = [b, g, r]
# display new image in window `img`
cv2.imshow('img', img)
# check if pressed key (wait for key 1ms)
k = cv2.waitKey(1) & 0xFF
# exit when pressed `ESC
if k == 27:
break
# destroy window `img`
cv2.destroyWindow('img')
# OR: destroy all windows
#cv2.destroyAllWindows()