1 Star 0 Fork 1

ahhaos/kalman-filter-in-single-object-tracking

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
utils.py 2.63 KB
一键复制 编辑 原始数据 按行查看 历史
liuchangji 提交于 2021-09-09 17:32 . Add files via upload
import cv2
def xyxy_to_xywh(xyxy):
center_x = (xyxy[0] + xyxy[2]) / 2
center_y = (xyxy[1] + xyxy[3]) / 2
w = xyxy[2] - xyxy[0]
h = xyxy[3] - xyxy[1]
return (center_x, center_y, w, h)
def plot_one_box(xyxy, img, color=(0, 200, 0), target=False):
xy1 = (int(xyxy[0]), int(xyxy[1]))
xy2 = (int(xyxy[2]), int(xyxy[3]))
if target:
color = (0, 0, 255)
cv2.rectangle(img, xy1, xy2, color, 1, cv2.LINE_AA) # filled
def updata_trace_list(box_center, trace_list, max_list_len=50):
if len(trace_list) <= max_list_len:
trace_list.append(box_center)
else:
trace_list.pop(0)
trace_list.append(box_center)
return trace_list
def draw_trace(img, trace_list):
"""
更新trace_list,绘制trace
:param trace_list:
:param max_list_len:
:return:
"""
for i, item in enumerate(trace_list):
if i < 1:
continue
cv2.line(img,
(trace_list[i][0], trace_list[i][1]), (trace_list[i - 1][0], trace_list[i - 1][1]),
(255, 255, 0), 3)
def cal_iou(box1, box2):
"""
:param box1: xyxy 左上右下
:param box2: xyxy
:return:
"""
x1min, y1min, x1max, y1max = box1[0], box1[1], box1[2], box1[3]
x2min, y2min, x2max, y2max = box2[0], box2[1], box2[2], box2[3]
# 计算两个框的面积
s1 = (y1max - y1min + 1.) * (x1max - x1min + 1.)
s2 = (y2max - y2min + 1.) * (x2max - x2min + 1.)
# 计算相交部分的坐标
xmin = max(x1min, x2min)
ymin = max(y1min, y2min)
xmax = min(x1max, x2max)
ymax = min(y1max, y2max)
inter_h = max(ymax - ymin + 1, 0)
inter_w = max(xmax - xmin + 1, 0)
intersection = inter_h * inter_w
union = s1 + s2 - intersection
# 计算iou
iou = intersection / union
return iou
def cal_distance(box1, box2):
"""
计算两个box中心点的距离
:param box1: xyxy 左上右下
:param box2: xyxy
:return:
"""
center1 = ((box1[0] + box1[2]) // 2, (box1[1] + box1[3]) // 2)
center2 = ((box2[0] + box2[2]) // 2, (box2[1] + box2[3]) // 2)
dis = ((center1[0] - center2[0]) ** 2 + (center1[1] - center2[1]) ** 2) ** 0.5
return dis
def xywh_to_xyxy(xywh):
x1 = xywh[0] - xywh[2]//2
y1 = xywh[1] - xywh[3]//2
x2 = xywh[0] + xywh[2] // 2
y2 = xywh[1] + xywh[3] // 2
return [x1, y1, x2, y2]
if __name__ == "__main__":
box1 = [100, 100, 200, 200]
box2 = [100, 100, 200, 300]
iou = cal_iou(box1, box2)
print(iou)
box1.pop(0)
box1.append(555)
print(box1)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/s235735/kalman-filter-in-single-object-tracking.git
git@gitee.com:s235735/kalman-filter-in-single-object-tracking.git
s235735
kalman-filter-in-single-object-tracking
kalman-filter-in-single-object-tracking
main

搜索帮助