代码没有给我任何错误,但我无法移动跟踪条来查找我喜欢的值。我已经完整阅读了文档,但没有找到任何有用的信息。 我试图使用轨迹栏为我的颜色范围找到一个好的上限值和下限值。它应该很简单。
例如,rh表示红色高,如红色上边界。
这是我的代码:
import cv2
import numpy as np
def nothing(x):
return()
cap = cv2.VideoCapture('video.MOV')
while(1):
#Gets retval and frames from each video
ret ,frame = cap.read()
#Check to see if retval is not None or empty
if not ret:
break;
trackbar = np.zeros((300,512,3), np.uint8)
trackbar2 = np.zeros((300,512,3), np.uint8)
cv2.namedWindow("trackbar")
cv2.namedWindow("trackbar2")
cv2.createTrackbar('RL','trackbar',0,255,nothing)
cv2.createTrackbar('GL','trackbar',0,255,nothing)
cv2.createTrackbar('BL','trackbar',0,255,nothing)
cv2.createTrackbar('RH','trackbar2',0,255,nothing)
cv2.createTrackbar('GH','trackbar2',0,255,nothing)
cv2.createTrackbar('BH','trackbar2',0,255,nothing)
r = cv2.getTrackbarPos('RL','trackbar')
g = cv2.getTrackbarPos('GL','trackbar')
b = cv2.getTrackbarPos('BL','trackbar')
rh = cv2.getTrackbarPos('RH','trackbar2')
gh = cv2.getTrackbarPos('GH','trackbar2')
bh = cv2.getTrackbarPos('BH','trackbar2')
trackbar[:] = [b,g,r]
trackbar2[:] = [bh,gh,rh]
boundaries = [([r,g,b],[rh,gh,bh])]
for (lower,upper) in boundaries:
lower = np.array(lower, dtype = "uint8")
upper = np.array(upper, dtype = "uint8")
mask = cv2.inRange(frame,lower,upper)
output = cv2.bitwise_and(frame, frame, mask = mask)
cv2.imshow("video", np.hstack([frame, output]))
if (cv2.waitKey(1) & 0xFF) == ord("q"):
break
cv2.destroyAllWindows()
答案 0 :(得分:0)
正如烧杯所提到的,我在循环中进行了启动,所以我只是将它们移到了外面并且它有效: 下面的工作代码:
import cv2
import numpy as np
def nothing(x):
return()
cap = cv2.VideoCapture('movie.MOV')
trackbar = np.zeros((300,512,3), np.uint8)
trackbar2 = np.zeros((300,512,3), np.uint8)
cv2.namedWindow("trackbar")
cv2.namedWindow("trackbar2")
cv2.createTrackbar('RL','trackbar',0,255,nothing)
cv2.createTrackbar('GL','trackbar',0,255,nothing)
cv2.createTrackbar('BL','trackbar',0,255,nothing)
cv2.createTrackbar('RH','trackbar2',0,255,nothing)
cv2.createTrackbar('GH','trackbar2',0,255,nothing)
cv2.createTrackbar('BH','trackbar2',0,255,nothing)
while(1):
#Gets retval and frames from each video
ret ,frame = cap.read()
#Check to see if retval is not None or empty
if not ret:
break;
r = cv2.getTrackbarPos('RL','trackbar')
g = cv2.getTrackbarPos('GL','trackbar')
b = cv2.getTrackbarPos('BL','trackbar')
rh = cv2.getTrackbarPos('RH','trackbar2')
gh = cv2.getTrackbarPos('GH','trackbar2')
bh = cv2.getTrackbarPos('BH','trackbar2')
trackbar[:] = [b,g,r]
trackbar2[:] = [bh,gh,rh]
boundaries = [([r,g,b],[rh,gh,bh])]
for (lower,upper) in boundaries:
lower = np.array(lower, dtype = "uint8")
upper = np.array(upper, dtype = "uint8")
mask = cv2.inRange(frame,lower,upper)
output = cv2.bitwise_and(frame, frame, mask = mask)
cv2.imshow("video", np.hstack([frame, output]))
if (cv2.waitKey(1) & 0xFF) == ord("q"):
break
cv2.destroyAllWindows()