Python OpenCV を使用してトラックバーを作成し、BGR 値を変更して画像の色を変更しています。コードを実行するとトラックバーが表示され、BGR の値を変更すると値のみが取得されますが、画像の色は黒のままです。
import numpy as np
import cv2 as cv
def nothing(x):
print(x)
img = np.zeros((300,512,3), np.uint8) # creating a black image
cv.namedWindow('image') # creating a window
cv.createTrackbar('B', 'image', 0, 255, nothing) # add trackbar to image
cv.createTrackbar('G', 'image', 0, 255, nothing)
cv.createTrackbar('R', 'image', 0, 255, nothing)
while(1):
cv.imshow('image', img) # show the image
k = cv.waitKey(0) & 0xFF # checks the input - esc key
if k == 27:
break
b = cv.getTrackbarPos('B', 'image') # get the position value of b
g = cv.getTrackbarPos('G', 'image') # get the position value of g
r = cv.getTrackbarPos('R', 'image') # get the position value of r
img[:] = [b, g, r]
cv.destroyAllWindow()
コードはエラーなしで実行されていますが、実行後に BGR 値を変更しようとしても色が変わりません。私はこのチュートリアル (YouTube) - https://www.youtube.com/watch?v=fM6ff3VEviI&list=PLS1QulWo1RIa7D1O6skqDQ-JZ1GGHKK-K&index=13から学んでいます。
スイッチトラックバーも追加してみましたが、色を1に変更しても色が変わりません
import numpy as np
import cv2 as cv
def nothing(x):
print(x)
img = np.zeros((300,512,3), np.uint8) # creating a black image
cv.namedWindow('image') # creating a window
cv.createTrackbar('B', 'image', 0, 255, nothing) # add trackbar to image
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) # show the image
k = cv.waitKey(0) & 0xFF # checks the input - esc key
if k == 27:
break
b = cv.getTrackbarPos('B', 'image') # get the position value of b
g = cv.getTrackbarPos('G', 'image') # get the position value of g
r = cv.getTrackbarPos('R', 'image') # get the position value of r
s = cv.getTrackbarPos(switch, 'image')
if s ==0:
img[:] = 0
else :
img[:] = [b, g, r]
cv.destroyAllWindow()