图像匹配问题。给出若干幅图像,图像的数据可以用矩阵表示。图像的数据范围是[0,255]。给出起始的图像x,在剩下任一图像中任选图像y,计算x的最后一列和y的第一列的欧氏距离。剩下图像中这种欧氏距离最小的图像是是最匹配的图像。假设最匹配图像为y 0 , 继续在余下的图像中寻找和y 0最匹配的图像。重复这个过程,直到没有图像剩下。
现在给出p幅图像的数据,依次编号为1,2,,p。 每幅图像的大小是m×n. 假设第一幅图像设置为编号x, 1≤x≤p. 按照前一段图像最匹配描述,计算最匹配的序列。
输入格式:
第一行输入四个整数m,n,p,x
第二行到到底m*p行,每一行有n个数据。
依次表示编号为1,2,.,p的图像数据。
输出格式:
输出最匹配序列,包括起始编号x
输入样例:
2, 2, 3, 2
33, 44
33, 44
55, 33
55, 33
44, 99
44, 99
输出样例:
2 1 3
样例说明, 2, 2, 3, 2 表示图像大小为2×2, 共3幅图像,
起始图像编号是2。
第一幅图像数据
33, 44
33, 44
第二幅图像数据
55, 33
55, 33
第三幅图像数据
44, 99
44, 99
输出结果
2 1 3 表明起始图像x编号为2,图像2最匹配的是1,图像1最匹配的是3。
import numpy as np
m, n, p, x = map(int, input().split(','))
array = [[] for i in range(p)]
for i in range(p):
array[i] = np.full((m, n), 0)
for i in range(p):
for j in range(m):
array[i][j] = input().split(',')
distance = 0
begin = x - 1
order = [x]
list = []
for i in range(p - 2):
for j in range(p):
for k in range(m):
distance = distance + (array[begin][k][m - 1] - array[j][k][0]) ** 2
distance = np.sqrt(distance)
list.append(distance)
if distance == min(list) and j+1 != x and j+1 not in order:
order.append(j + 1)
begin = j
distance = 0
for i in range(p):
if i+1 not in order:
order.append(i+1)
for i in range(p):
print(order[i], end=' ')
第二个测试点一直过不去!