Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用L1范数获取一组图像中最远的图像_Python_Numpy_Opencv - Fatal编程技术网

Python 使用L1范数获取一组图像中最远的图像

Python 使用L1范数获取一组图像中最远的图像,python,numpy,opencv,Python,Numpy,Opencv,我有一个包含约100个RGB图像的短视频序列。我将它们存储在一个numpy数组中,现在我想根据L1范数获得两个最远的图像 我的第一个想法是浏览集合,将每个图像与其他图像进行比较,并将每个距离存储在一个矩阵中,然后找到最大值。然而,它需要2个循环,我觉得可能有一种更好的方法来实现这一点 import numpy as np import cv2 # Store the sequence in an array seq = [] cap = cv2.VideoCapture('my_video.

我有一个包含约100个RGB图像的短视频序列。我将它们存储在一个numpy数组中,现在我想根据L1范数获得两个最远的图像

我的第一个想法是浏览集合,将每个图像与其他图像进行比较,并将每个距离存储在一个矩阵中,然后找到最大值。然而,它需要2个循环,我觉得可能有一种更好的方法来实现这一点

import numpy as np
import cv2

# Store the sequence in an array
seq = []
cap =  cv2.VideoCapture('my_video.avi')
while(cap.isOpened()) :
    ret, frame = cap.read()
    if (ret == False) :
        break
    seq.append(frame)
# Possibly convert list to np.array here

# Finding the most distant images
distance_matrix = np.zeros( (len(seq), len(seq)) )
for i in range(len(seq)) :
    for j in range(i+1,len(seq)) :
        distance_matrix[i][j] = cv2.norm(seq[i], seq[j], cv2.NORM_L1)

# Get max index (method given on scipy doc)
im1, im2 = np.unravel_index(np.argmax(distance_matrix), distance_matrix.shape)
print("The most distant frames are n°", im1, "and n°", im2)
输出类似于
>>最远的帧是n°0和n°17

似乎我们总是被迫在某一点生成每一对,但这是一种繁重的方法。
有什么更有效的办法吗?

我想你可以看看。它使用L2范数测量距离,但我相信L1范数也可以通过只考虑凸包来完成。但是,由于高维数据(如图像)总是会导致一些问题,例如,我不确定这是否适用。除此之外,更直接的方法是使用
scipy.space.distance.pdist
。在计算两两距离时,它至少比double for loop好(使用快速广义矩阵乘法算法可能更快),因为它只有100幅图像。双倍循环。不要存储整个距离矩阵。仅当距离大于之前的距离时才存储索引。因此,当双循环进行时,只存储到目前为止最大的距离,并将索引存储到创建它的seq中。看起来像O(n**2)…所以是双循环。就像上面的评论,就这么做吧。但无论你在做什么,都需要一段时间,当你开始往里面投更多的图片时。如果发生这种情况,库达可能是你的朋友。而且,如果你想要的话,L1规范不会给你任何语义距离的概念。