opencv跟踪和匹配
opencv实现了八种单目标跟踪算法,对于比较简单的场景,可以用来做目标跟踪,简单记录下学习过程。
跟踪器简介
- BOOSTING Tracker: Based on the same algorithm used to power the machine learning behind Haar cascades (AdaBoost), but like Haar cascades, is over a decade old. This tracker is slow and doesn’t work very well. Interesting only for legacy reasons and comparing other algorithms. (minimum OpenCV 3.0.0)
- MIL Tracker: Better accuracy than BOOSTING tracker but does a poor job of reporting failure. (minimum OpenCV 3.0.0)
- KCF Tracker: Kernelized Correlation Filters. Faster than BOOSTING and MIL. Similar to MIL and KCF, does not handle full occlusion well. (minimum OpenCV 3.1.0)
- CSRT Tracker: Discriminative Correlation Filter (with Channel and Spatial Reliability). Tends to be more accurate than KCF but slightly slower. (minimum OpenCV 3.4.2)
- MedianFlow Tracker: Does a nice job reporting failures; however, if there is too large of a jump in motion, such as fast moving objects, or objects that change quickly in their appearance, the model will fail. (minimum OpenCV 3.0.0)
- TLD Tracker: I’m not sure if there is a problem with the OpenCV implementation of the TLD tracker or the actual algorithm itself, but the TLD tracker was incredibly prone to false-positives. I do not recommend using this OpenCV object tracker. (minimum OpenCV 3.0.0)
- MOSSE Tracker: Very, very fast. Not as accurate as CSRT or KCF but a good choice if you need pure speed. (minimum OpenCV 3.4.1)
- GOTURN Tracker: The only deep learning-based object detector included in OpenCV. It requires additional model files to run (will not be covered in this post). My initial experiments showed it was a bit of a pain to use even though it reportedly handles viewing changes well (my initial experiments didn’t confirm this though). I’ll try to cover it in a future post, but in the meantime, take a look at Satya’s writeup. (minimum OpenCV 3.2.0)
文章建议如下:
使用CSRT: 当需要更高的准确率的时候采用,这个更耗时一点.
使用KCF: 这个更加高速(帧),单准确率差一点.
使用MOSSE: 只最求高速的话,就选它吧.
跟踪器使用
上面8个跟踪器的使用步骤差不多,主要包括三部分:
1. 创建跟踪器
tracker = cv2.TrackerBoosting_create() # 创建Boosting跟踪器, 返回跟踪器对象 tracker = cv2.TrackerMIL_create() # 创建MIL跟踪器, 返回跟踪器对象 tracker = cv2.TrackerKCF_create() # 创建KCF跟踪器, 返回跟踪器对象 tracker = cv2.TrackerCSRT_create() # 创建CSRT跟踪器, 返回跟踪器对象 tracker = cv2.TrackerMedianFlow_create() # 创建MedianFlo跟踪器, 返回跟踪器对象 tracker = cv2.TrackerTLD_create() # 创建TLD跟踪器, 返回跟踪器对象 tracker = cv2.TrackerMOSSE_create() # 创建MOSSE跟踪器, 返回跟踪器对象 tracker = cv2.TrackerGOTURN_create() # 创建GOTURN跟踪器, 返回跟踪器对象
2. 用第一帧图片和目标的box初始化跟踪器
tracker.init(image, boundingBox) #初始化tracker
image: array,图片矩阵
boundingBox: array, 跟踪目标的矩形框box, 格式为[xmin, ymin, width, height](左上角坐标,宽,高)
返回值:
初始化成功返回True,否则False
3. 跟踪器接收下一帧图片,返回目标的box
retval, boundingBox = tracker.update(image)
image: array, 当前帧的图片矩阵
返回值:
retval:跟踪成功时返回True,失败时返回False
boundingBox:返回跟踪到的目标矩形框,格式为[xmin, ymin, width, height](左上角坐标,宽,高)
KCF跟踪器的使用示例代码如下:
class VideoReader(): def __init__(self, video_path): self.video_path = video_path if not os.path.exists(video_path): raise Exception("Unexist video path {}".format(video_path)) self.cap = cv2.VideoCapture(self.video_path) def __next__(self): flag, frame = self.cap.read() if not flag: raise StopIteration # current_time = self.cap.get(cv2.CAP_PROP_POS_MSEC) #当前位置在视频中是多少毫秒 return frame def __iter__(self): return self if __name__ == "__main__": # tracker = cv2.TrackerTLD_create() tracker = cv2.TrackerKCF_create() # 1. 创建KCF跟踪器 video_frames = VideoReader(r"F:\data\video20201117202801.mkv") for ind, frame in enumerate(video_frames): if ind == 0: gt_bbox = cv2.selectROI("image", frame, False, False) xmin, ymin, xmax, ymax = gt_bbox[0], gt_bbox[1], gt_bbox[0]+gt_bbox[2], gt_bbox[1]+gt_bbox[3] tracker.init(frame, gt_bbox) # 2.视频第一帧图片frame,图片中需要跟踪目标的box (用两者来初始化跟踪器) else: flag, pred_bbox = tracker.update(frame) # 3.接收下一帧图片,跟踪器返回状态flag,跟踪目标box (flag为True表示跟踪成功,为False表示跟踪失败) print(flag) if flag: pred_bbox = list(map(int, pred_bbox)) xmin, ymin, xmax, ymax = pred_bbox[0], pred_bbox[1], pred_bbox[0] + pred_bbox[2], pred_bbox[1] + pred_bbox[3] cv2.rectangle(frame, (pred_bbox[0], pred_bbox[1]), (pred_bbox[0]+pred_bbox[2], pred_bbox[1]+pred_bbox[3]), (0, 255, 255), 3) cv2.imshow("image", frame) cv2.waitKey(0)
跟踪器原理
使用起来都很简单,但是算法底层的原理很复杂,浅显的学习下。
1. KCFtracker
KCF(Kernelized Correlation Filters)算法是在2015年的论文 High Speed Tracking with Kernelized Correlation Filters中提出,
https://www.cnblogs.com/YiXiaoZhou/p/5925019.html
https://zhuanlan.zhihu.com/p/55157416
https://www.pyimagesearch.com/2018/07/30/opencv-object-tracking/
2. SIFT
https://www.cnblogs.com/lzq116/p/11836657.html
https://segmentfault.com/a/1190000015735549
https://www.jianshu.com/p/ed57ee1056ab

浙公网安备 33010602011771号