如何在不选择 roi 的情况下使用 opencv Tracker 参数
how to use opencv Tracker parameters without selecting a roi
谁能告诉我在检测到行人后应该将哪个第二个参数传递给 tracker.init()?互联网上只有已选择 ROI 的参数。我试图传递一个变量 rects,但它给了我一个错误
import numpy as np
import cv2
import sys
from imutils.object_detection import non_max_suppression
cap = cv2.VideoCapture("video.mp4")
hog_descriptor = cv2.HOGDescriptor()
hog_descriptor.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
tracker = cv2.TrackerKCF_create()
ret, frame = cap.read()
while (1):
ret, frame = cap.read()
(rects, weights) = hog_descriptor.detectMultiScale(frame, winStride=(16, 16),
padding=(8, 8), scale=1.05)
rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects])
pick = non_max_suppression(rects, probs=None, overlapThresh=0.65)
for (xA, yA, xB, yB) in rects:
cv2.rectangle(frame, (xA, yA), (xB, yB), (0, 255, 0), 2)
ret = tracker.init(frame)
ret, = tracker.update(frame)
cv2.imshow("video", frame)
if cv2.waitKey(1) == 27:
break
cap.release()
cv2.destroyAllWindows()
根据opencv示例代码https://github.com/opencv/opencv_contrib/blob/master/modules/tracking/samples/tracker.py,tracker.init的第二个参数是边界框:
cv.namedWindow("tracking")
camera = cv.VideoCapture(sys.argv[1])
ok, image=camera.read()
...
bbox = cv.selectROI("tracking", image)
tracker = cv.TrackerMIL_create()
...
ok = tracker.init(image, bbox)
因此,如果您想使用检测到的行人作为 ROI,您应该使用:
bbox = rects[index]
tracker.init(image, bbox)
其中索引是您要跟踪的检测到的行人数量
谁能告诉我在检测到行人后应该将哪个第二个参数传递给 tracker.init()?互联网上只有已选择 ROI 的参数。我试图传递一个变量 rects,但它给了我一个错误
import numpy as np
import cv2
import sys
from imutils.object_detection import non_max_suppression
cap = cv2.VideoCapture("video.mp4")
hog_descriptor = cv2.HOGDescriptor()
hog_descriptor.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
tracker = cv2.TrackerKCF_create()
ret, frame = cap.read()
while (1):
ret, frame = cap.read()
(rects, weights) = hog_descriptor.detectMultiScale(frame, winStride=(16, 16),
padding=(8, 8), scale=1.05)
rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects])
pick = non_max_suppression(rects, probs=None, overlapThresh=0.65)
for (xA, yA, xB, yB) in rects:
cv2.rectangle(frame, (xA, yA), (xB, yB), (0, 255, 0), 2)
ret = tracker.init(frame)
ret, = tracker.update(frame)
cv2.imshow("video", frame)
if cv2.waitKey(1) == 27:
break
cap.release()
cv2.destroyAllWindows()
根据opencv示例代码https://github.com/opencv/opencv_contrib/blob/master/modules/tracking/samples/tracker.py,tracker.init的第二个参数是边界框:
cv.namedWindow("tracking")
camera = cv.VideoCapture(sys.argv[1])
ok, image=camera.read()
...
bbox = cv.selectROI("tracking", image)
tracker = cv.TrackerMIL_create()
...
ok = tracker.init(image, bbox)
因此,如果您想使用检测到的行人作为 ROI,您应该使用:
bbox = rects[index]
tracker.init(image, bbox)
其中索引是您要跟踪的检测到的行人数量