用python将等效图片分组

用python将等效图片分组,python,numpy,Python,Numpy,我有几张照片描绘了几个点的坐标,通常是3-10点。我想把有相同排列点的图片分组。通过移动一个向量0,1,该向量由3个点组成,坐标为1,12,1和3,1,我们得到了分别为1,2,2和3,2的集合d。类似地,在通过向量2,0进行移位之后,集合a成为1,3,2,3和3,3的集合g。然后,我们可以将集合A、D和G组合成相同的排列并考虑等价。 你能帮我一下吗 def is_shift(set1, set2): shift = None # will store a tuple of delta

我有几张照片描绘了几个点的坐标,通常是3-10点。我想把有相同排列点的图片分组。通过移动一个向量0,1,该向量由3个点组成,坐标为1,12,1和3,1,我们得到了分别为1,2,2和3,2的集合d。类似地,在通过向量2,0进行移位之后,集合a成为1,3,2,3和3,3的集合g。然后,我们可以将集合A、D和G组合成相同的排列并考虑等价。 你能帮我一下吗

def is_shift(set1, set2): 
    shift = None  # will store a tuple of delta_x, delta_y
    for (x1, y1), (x2, y2) in zip(set1, set2): 
        cur_shift = x1 - x1, y1 - y2 
        if not shift:  # the first pair of points
            shift = cur_shift 
        elif shift != cur_shift: # shifted the same way as the first one?
            return False 
    return True 

matrices = array([
    [(1, 1), (2, 1), (3, 1)],
    [(1, 2), (2, 1), (3, 1)],
    [(1, 3), (2, 1), (2, 2)], 
    [(1, 2), (2, 2), (3, 2)], 
    [(1, 3), (2, 2), (3, 2)], 
    [(2, 3), (3, 1), (3, 2)],
    [(1, 3), (2, 3), (3, 3)], 
    [(2, 2), (3, 1), (4, 4)],
    ])
输出:

[[(1, 1), (2, 1), (3, 1)], [(1, 2), (2, 2), (3, 2)], [(1, 3), (2, 3), (3, 3)]]
[[(1, 2), (2, 1), (3, 1)], [(1, 3), (2, 2), (3, 2)]]
[[(1, 3), (2, 1), (2, 2)], [(2, 3), (3, 1), (3, 2)]]
[(2, 2), (3, 1), (4, 4)]

这里的想法是通过构造一个键,使用字典对图像进行分组。如果我考虑图像被描绘的方式,我可以想到X轴和Y轴上的坐标的移位对于相似的图像是相似的,如果我把移位看成是[X-MxxO-CORDEX用于XX-COORDS ]和[Y-Myyy-CODRADY在YyCOORDS中]。< /P> 例如,对于图像1,x_coords=[1,2,3]和y_coords=[2,2,2],因此位移将为0、1、2和0,0,0,因为x和y coords的最小值分别为1和2。这些移动的组合现在可以用作将不同图像分组的键,如下所示

import collections

def group_matrices():

    matrices = [
        [(1, 1), (2, 1), (3, 1)],
        [(1, 2), (2, 1), (3, 1)],
        [(1, 3), (2, 1), (2, 2)],
        [(1, 2), (2, 2), (3, 2)],
        [(1, 3), (2, 2), (3, 2)],
        [(2, 3), (3, 1), (3, 2)],
        [(1, 3), (2, 3), (3, 3)],
        [(2, 2), (3, 1), (4, 4)],
    ]

    # Dictionary to group images
    groups = collections.defaultdict(list)

    # Iterate over the matrices
    for image in matrices:

        # Extract x and y coordinates from the image
        x_coords, y_coords = zip(*image)

        # Compute minimum of x and y coordinates
        min_x = min(x_coords)
        min_y = min(y_coords)

        # Compute the shifts
        key_x = tuple(x - min_x for x in x_coords)
        key_y = tuple(y - min_y for y in y_coords)

        # Create the key combining the shifts and add image
        # to corresponding key
        key = (key_x, key_y)
        groups[key].append(image)

    # Return the list of lists of grouped images
    return [value for value in groups.values()]

for group in group_matrices():
    print(group)
输出将是

[[(1, 1), (2, 1), (3, 1)], [(1, 2), (2, 2), (3, 2)], [(1, 3), (2, 3), (3, 3)]]
[[(1, 2), (2, 1), (3, 1)], [(1, 3), (2, 2), (3, 2)]]
[[(1, 3), (2, 1), (2, 2)], [(2, 3), (3, 1), (3, 2)]]
[[(2, 2), (3, 1), (4, 4)]]

这里的想法是通过构造一个键,使用字典对图像进行分组。如果我考虑图像被描绘的方式,我可以想到X轴和Y轴上的坐标的移位对于相似的图像是相似的,如果我把移位看成是[X-MxxO-CORDEX用于XX-COORDS ]和[Y-Myyy-CODRADY在YyCOORDS中]。< /P> 例如,对于图像1,x_coords=[1,2,3]和y_coords=[2,2,2],因此位移将为0、1、2和0,0,0,因为x和y coords的最小值分别为1和2。这些移动的组合现在可以用作将不同图像分组的键,如下所示

import collections

def group_matrices():

    matrices = [
        [(1, 1), (2, 1), (3, 1)],
        [(1, 2), (2, 1), (3, 1)],
        [(1, 3), (2, 1), (2, 2)],
        [(1, 2), (2, 2), (3, 2)],
        [(1, 3), (2, 2), (3, 2)],
        [(2, 3), (3, 1), (3, 2)],
        [(1, 3), (2, 3), (3, 3)],
        [(2, 2), (3, 1), (4, 4)],
    ]

    # Dictionary to group images
    groups = collections.defaultdict(list)

    # Iterate over the matrices
    for image in matrices:

        # Extract x and y coordinates from the image
        x_coords, y_coords = zip(*image)

        # Compute minimum of x and y coordinates
        min_x = min(x_coords)
        min_y = min(y_coords)

        # Compute the shifts
        key_x = tuple(x - min_x for x in x_coords)
        key_y = tuple(y - min_y for y in y_coords)

        # Create the key combining the shifts and add image
        # to corresponding key
        key = (key_x, key_y)
        groups[key].append(image)

    # Return the list of lists of grouped images
    return [value for value in groups.values()]

for group in group_matrices():
    print(group)
输出将是

[[(1, 1), (2, 1), (3, 1)], [(1, 2), (2, 2), (3, 2)], [(1, 3), (2, 3), (3, 3)]]
[[(1, 2), (2, 1), (3, 1)], [(1, 3), (2, 2), (3, 2)]]
[[(1, 3), (2, 1), (2, 2)], [(2, 3), (3, 1), (3, 2)]]
[[(2, 2), (3, 1), (4, 4)]]

我的想法是应用上述坐标变换。我尽可能多地保持基于numpy的no循环计算:

import numpy as np
import numpy_indexed as npi

matrices = [
    [(1, 1), (2, 1), (3, 1)],
    [(1, 2), (2, 1), (3, 1)],
    [(1, 3), (2, 1), (2, 2)],
    [(1, 2), (2, 2), (3, 2)],
    [(1, 3), (2, 2), (3, 2)], 
    [(2, 3), (3, 1), (3, 2)],
    [(1, 3), (2, 3), (3, 3)], 
    [(2, 2), (3, 1), (4, 4)],
    ]

translation = np.min(matrices, axis=1)
translated_matrices = np.array([n-m for n, m in zip(np.array(matrices), translation)])
_, groups = np.unique(translated_matrices, return_inverse=True, axis=0)
unique, idx_groups = npi.group_by(groups, np.arange(len(groups)))
result = [[matrices[idx] for idx in n] for n in idx_groups]
print('groups are:', groups)
print('index groups are:', idx_groups)
print('matrix groups are:', result)
输出:
我的想法是应用上述坐标变换。我尽可能多地保持基于numpy的no循环计算:

import numpy as np
import numpy_indexed as npi

matrices = [
    [(1, 1), (2, 1), (3, 1)],
    [(1, 2), (2, 1), (3, 1)],
    [(1, 3), (2, 1), (2, 2)],
    [(1, 2), (2, 2), (3, 2)],
    [(1, 3), (2, 2), (3, 2)], 
    [(2, 3), (3, 1), (3, 2)],
    [(1, 3), (2, 3), (3, 3)], 
    [(2, 2), (3, 1), (4, 4)],
    ]

translation = np.min(matrices, axis=1)
translated_matrices = np.array([n-m for n, m in zip(np.array(matrices), translation)])
_, groups = np.unique(translated_matrices, return_inverse=True, axis=0)
unique, idx_groups = npi.group_by(groups, np.arange(len(groups)))
result = [[matrices[idx] for idx in n] for n in idx_groups]
print('groups are:', groups)
print('index groups are:', idx_groups)
print('matrix groups are:', result)
输出:
通过使最小的x和y坐标均为0,得出点集的标准形式。通过找到最小的x和最小的y,并从所有点中减去它们来实现这一点。然后,您可以创建一个点的规范有序元组,例如先按x再按y排序,或者创建一个点的冻结集(如果您不希望顺序重要),否则只需使用元组并保留顺序即可。创建一个dict,然后你可以立即检查是否有重复。通过使最小的x和y坐标都为0,为点集提供一个标准形式。通过找到最小的x和最小的y,并从所有点中减去它们来实现这一点。然后,您可以创建一个点的规范有序元组,例如先按x再按y排序,或者创建一个点的冻结集(如果您不希望顺序重要),否则只需使用元组并保留顺序即可。创建这些文件的目录,然后您可以立即检查是否有重复的文件。