如何找到五个点之间的最大距离?
How to find the maximum distance between five points?
我在视频中做过物体检测,发现一帧有5到6个坐标。在每一帧中,坐标之间的距离因移动物体而异。我的文本文件有 13 列,第一列是帧数和 6 个坐标,如下所示。我必须找到坐标之间最大 space 到最小的帧号。每个坐标之间的最小任意距离不应相互交叉,因为我想根据坐标之间的间距对视频进行分段。
是否有任何预先编写的算法来进行这种类型的分割,或者我应该循环吗?
301.467 0.713235 0.852451 0.752451 0.753922 0.380392 0.373039 0.282843 0.291667 0.262255 0.283824 0.236765 0.218627
301.6 0.717647 0.864706 0.752941 0.761275 0.753431 0.743627 0.388235 0.383333 0.286765 0.294608 0.237745 0.207353
301.667 0.722059 0.876471 0.756863 0.763726 0.395098 0.391667 0.284314 0.289216 0.238725 0.198529 nan nan
301.867 0.72451 0.884314 0.762745 0.771078 0.40098 0.401961 0.290686 0.292647 0.240196 0.207843 0.241176 0.190686
302 0.731373 0.897059 0.760784 0.771569 0.413725 0.422549 0.302941 0.292157 0.25098 0.179412 nan nan
302.067 0.743627 0.917157 0.765686 0.777941 0.430392 0.444608 0.305392 0.293137 0.257843 0.17402 nan nan
302.2 0.754902 0.928431 0.768627 0.782843 0.440686 0.458333 0.316667 0.294608 0.266176 0.17451 nan nan
302.267 0.768137 0.937745 0.770098 0.789216 0.454902 0.476961 0.323039 0.296569 0.276471 0.186275 nan nan
302.467 0.783824 0.939706 0.769608 0.794608 0.47598 0.498039 0.328431 0.29951 0.281863 0.194118 nan nan
302.8 0.792647 0.940686 0.768627 0.8 0.486765 0.510294 0.337745 0.305392 0.285294 0.2 nan nan
预期输出类似于
视频片段中的第一个最大值 space : 301.467 to 301.867
视频片段中的第 2 个最大值 space : 302.2 to 302.8
以下脚本首先将您的数据转换为行列表。对于每一行,初始值被丢弃,然后创建包含所有对的排列列表。 maths.hypot
然后用于计算距离。 max_distance
为按距离排序的每个集合显示。
import math, itertools
data = """301.467 0.713235 0.852451 0.752451 0.753922 0.380392 0.373039 0.282843 0.291667 0.262255 0.283824 0.236765 0.218627
301.6 0.717647 0.864706 0.752941 0.761275 0.753431 0.743627 0.388235 0.383333 0.286765 0.294608 0.237745 0.207353
301.667 0.722059 0.876471 0.756863 0.763726 0.395098 0.391667 0.284314 0.289216 0.238725 0.198529 nan nan
301.867 0.72451 0.884314 0.762745 0.771078 0.40098 0.401961 0.290686 0.292647 0.240196 0.207843 0.241176 0.190686
302 0.731373 0.897059 0.760784 0.771569 0.413725 0.422549 0.302941 0.292157 0.25098 0.179412 nan nan
302.067 0.743627 0.917157 0.765686 0.777941 0.430392 0.444608 0.305392 0.293137 0.257843 0.17402 nan nan
302.2 0.754902 0.928431 0.768627 0.782843 0.440686 0.458333 0.316667 0.294608 0.266176 0.17451 nan nan
302.267 0.768137 0.937745 0.770098 0.789216 0.454902 0.476961 0.323039 0.296569 0.276471 0.186275 nan nan
302.467 0.783824 0.939706 0.769608 0.794608 0.47598 0.498039 0.328431 0.29951 0.281863 0.194118 nan nan
302.8 0.792647 0.940686 0.768627 0.8 0.486765 0.510294 0.337745 0.305392 0.285294 0.2 nan nan"""
def calc_distance(c1, c2):
return math.hypot(c2[0] - c1[0], c2[1] - c1[1])
results = []
for row in data.split("\n"):
cols = [float(col) for col in row.split(" ") if len(col)]
max_distance = 0
for c1,c2 in itertools.permutations(zip(cols[1:], cols[2:]), 2):
max_distance = max(max_distance, calc_distance(c1,c2))
results.append((max_distance, cols[0]))
results.sort()
for distance, frame in results:
print "%-15s %f" % (frame, distance)
这会产生以下结果,显示按最大距离顺序排序的帧:
301.467 0.814885
301.6 0.831112
301.667 0.847618
301.867 0.860743
302.0 0.869144
302.067 0.887828
302.8 0.897788
302.267 0.898022
302.2 0.898471
302.467 0.898814
这是使用 Python 2.7.9 完成的。我没有手动验证结果。
根据您的评论更新
我仍然不完全确定我理解了这个问题,但以下解决方案计算了所有帧中的最小和最大坐标,并显示了在哪个帧中找到了每个异常坐标。这将为您提供适合自动裁剪的信息。
# Hold the frame number and the value for each side
# Where 100 is greater than the expected height and width of the frame
# Assuming top left is (0,0)
frame_top = (0.0, 100.0)
frame_left = (0.0, 100.0)
frame_bottom = (0.0, 0.0)
frame_right = (0.0, 0.0)
for row in data.split("\n"):
cols = [float(col) for col in row.split(" ") if len(col)]
icols = iter(cols[1:])
for x in icols:
y = next(icols)
if x and y: # Skip any nan values
if x < frame_left[1]:
frame_left = (cols[0], x)
if x > frame_right[1]:
frame_right = (cols[0], x)
if y < frame_top[1]:
frame_top = (cols[0], y)
if y > frame_bottom[1]:
frame_bottom = (cols[0], y)
print "Frame %f has top %f" % frame_top
print "Frame %f has left %f" % frame_left
print "Frame %f has bottom %f" % frame_bottom
print "Frame %f has right %f" % frame_right
这给出了以下输出:
Frame 302.067000 has top 0.174020
Frame 301.467000 has left 0.236765
Frame 302.800000 has bottom 0.940686
Frame 302.800000 has right 0.792647
实际上它正在计算所有点的最小边界矩形。您当然可以提交较小范围的帧。
我在视频中做过物体检测,发现一帧有5到6个坐标。在每一帧中,坐标之间的距离因移动物体而异。我的文本文件有 13 列,第一列是帧数和 6 个坐标,如下所示。我必须找到坐标之间最大 space 到最小的帧号。每个坐标之间的最小任意距离不应相互交叉,因为我想根据坐标之间的间距对视频进行分段。
是否有任何预先编写的算法来进行这种类型的分割,或者我应该循环吗?
301.467 0.713235 0.852451 0.752451 0.753922 0.380392 0.373039 0.282843 0.291667 0.262255 0.283824 0.236765 0.218627
301.6 0.717647 0.864706 0.752941 0.761275 0.753431 0.743627 0.388235 0.383333 0.286765 0.294608 0.237745 0.207353
301.667 0.722059 0.876471 0.756863 0.763726 0.395098 0.391667 0.284314 0.289216 0.238725 0.198529 nan nan
301.867 0.72451 0.884314 0.762745 0.771078 0.40098 0.401961 0.290686 0.292647 0.240196 0.207843 0.241176 0.190686
302 0.731373 0.897059 0.760784 0.771569 0.413725 0.422549 0.302941 0.292157 0.25098 0.179412 nan nan
302.067 0.743627 0.917157 0.765686 0.777941 0.430392 0.444608 0.305392 0.293137 0.257843 0.17402 nan nan
302.2 0.754902 0.928431 0.768627 0.782843 0.440686 0.458333 0.316667 0.294608 0.266176 0.17451 nan nan
302.267 0.768137 0.937745 0.770098 0.789216 0.454902 0.476961 0.323039 0.296569 0.276471 0.186275 nan nan
302.467 0.783824 0.939706 0.769608 0.794608 0.47598 0.498039 0.328431 0.29951 0.281863 0.194118 nan nan
302.8 0.792647 0.940686 0.768627 0.8 0.486765 0.510294 0.337745 0.305392 0.285294 0.2 nan nan
预期输出类似于
视频片段中的第一个最大值 space : 301.467 to 301.867
视频片段中的第 2 个最大值 space : 302.2 to 302.8
以下脚本首先将您的数据转换为行列表。对于每一行,初始值被丢弃,然后创建包含所有对的排列列表。 maths.hypot
然后用于计算距离。 max_distance
为按距离排序的每个集合显示。
import math, itertools
data = """301.467 0.713235 0.852451 0.752451 0.753922 0.380392 0.373039 0.282843 0.291667 0.262255 0.283824 0.236765 0.218627
301.6 0.717647 0.864706 0.752941 0.761275 0.753431 0.743627 0.388235 0.383333 0.286765 0.294608 0.237745 0.207353
301.667 0.722059 0.876471 0.756863 0.763726 0.395098 0.391667 0.284314 0.289216 0.238725 0.198529 nan nan
301.867 0.72451 0.884314 0.762745 0.771078 0.40098 0.401961 0.290686 0.292647 0.240196 0.207843 0.241176 0.190686
302 0.731373 0.897059 0.760784 0.771569 0.413725 0.422549 0.302941 0.292157 0.25098 0.179412 nan nan
302.067 0.743627 0.917157 0.765686 0.777941 0.430392 0.444608 0.305392 0.293137 0.257843 0.17402 nan nan
302.2 0.754902 0.928431 0.768627 0.782843 0.440686 0.458333 0.316667 0.294608 0.266176 0.17451 nan nan
302.267 0.768137 0.937745 0.770098 0.789216 0.454902 0.476961 0.323039 0.296569 0.276471 0.186275 nan nan
302.467 0.783824 0.939706 0.769608 0.794608 0.47598 0.498039 0.328431 0.29951 0.281863 0.194118 nan nan
302.8 0.792647 0.940686 0.768627 0.8 0.486765 0.510294 0.337745 0.305392 0.285294 0.2 nan nan"""
def calc_distance(c1, c2):
return math.hypot(c2[0] - c1[0], c2[1] - c1[1])
results = []
for row in data.split("\n"):
cols = [float(col) for col in row.split(" ") if len(col)]
max_distance = 0
for c1,c2 in itertools.permutations(zip(cols[1:], cols[2:]), 2):
max_distance = max(max_distance, calc_distance(c1,c2))
results.append((max_distance, cols[0]))
results.sort()
for distance, frame in results:
print "%-15s %f" % (frame, distance)
这会产生以下结果,显示按最大距离顺序排序的帧:
301.467 0.814885
301.6 0.831112
301.667 0.847618
301.867 0.860743
302.0 0.869144
302.067 0.887828
302.8 0.897788
302.267 0.898022
302.2 0.898471
302.467 0.898814
这是使用 Python 2.7.9 完成的。我没有手动验证结果。
根据您的评论更新
我仍然不完全确定我理解了这个问题,但以下解决方案计算了所有帧中的最小和最大坐标,并显示了在哪个帧中找到了每个异常坐标。这将为您提供适合自动裁剪的信息。
# Hold the frame number and the value for each side
# Where 100 is greater than the expected height and width of the frame
# Assuming top left is (0,0)
frame_top = (0.0, 100.0)
frame_left = (0.0, 100.0)
frame_bottom = (0.0, 0.0)
frame_right = (0.0, 0.0)
for row in data.split("\n"):
cols = [float(col) for col in row.split(" ") if len(col)]
icols = iter(cols[1:])
for x in icols:
y = next(icols)
if x and y: # Skip any nan values
if x < frame_left[1]:
frame_left = (cols[0], x)
if x > frame_right[1]:
frame_right = (cols[0], x)
if y < frame_top[1]:
frame_top = (cols[0], y)
if y > frame_bottom[1]:
frame_bottom = (cols[0], y)
print "Frame %f has top %f" % frame_top
print "Frame %f has left %f" % frame_left
print "Frame %f has bottom %f" % frame_bottom
print "Frame %f has right %f" % frame_right
这给出了以下输出:
Frame 302.067000 has top 0.174020
Frame 301.467000 has left 0.236765
Frame 302.800000 has bottom 0.940686
Frame 302.800000 has right 0.792647
实际上它正在计算所有点的最小边界矩形。您当然可以提交较小范围的帧。