python - 如何在 python 中对数组进行重采样

标签 python image-processing matrix

我是 python 的新手,我有一个关于数组/矩阵的问题。 下面是我得到的矩阵。

一个=

[[85 77 83 ..., 59 58 59]

[80 83 80 ..., 57 60 58]

[75 76 81 ..., 59 58 60]]

我想重新采样(我不知道这是不是正确的词)矩阵所以它变成了

乙=

[[ 85 85 85 85 77 77 77 77 83 83 83 83 ....... 59 59 59 59 58 58 58 58 59 59 59 59]

[ 85 85 85 85 77 77 77 77 83 83 83 83 ....... 59 59 59 59 58 58 58 58 59 59 59 59]

[ 85 85 85 85 77 77 77 77 83 83 83 83 ....... 59 59 59 59 58 58 58 58 59 59 59 59]

[ 85 85 85 85 77 77 77 77 83 83 83 83 ....... 59 59 59 59 58 58 58 58 59 59 59 59]

[ 80 80 80 80 83 83 83 83 80 80 80 80 ....... 57 57 57 57 60 60 60 60 58 58 58 58]

[ 80 80 80 80 83 83 83 83 80 80 80 80 ....... 57 57 57 57 60 60 60 60 58 58 58 58]

[ 80 80 80 80 83 83 83 83 80 80 80 80 ....... 57 57 57 57 60 60 60 60 58 58 58 58]

[ 80 80 80 80 83 83 83 83 80 80 80 80 ....... 57 57 57 57 60 60 60 60 58 58 58 58]

[ 75 75 75 75 76 76 76 76 81 81 81 81 ....... 59 59 59 59 58 58 58 58 60 60 60 60]

[ 75 75 75 75 76 76 76 76 81 81 81 81 ....... 59 59 59 59 58 58 58 58 60 60 60 60]

[ 75 75 75 75 76 76 76 76 81 81 81 81 ....... 59 59 59 59 58 58 58 58 60 60 60 60]]

我在网上搜索了很多帖子,但我仍然不知道该怎么做。 所以请教我怎么做,非常感谢。

最佳答案

一定要使用来自 Scipy interpolation how to resize/resample 3x3 matrix to 5x5? 的信息来自评论。

但我想我会搞砸,这就是我得到的:

可能是有史以来最糟糕的方法:

>>> import pprint
>>> a = [[85, 77, 99],
...      [11, 22, 33],
...      [44, 55, 66]]
>>> 
>>> def transform(n,matrix):
...     return [item for sublist in [[[item for sublist in [[element]*n for element in row] for item in sublist] for _ in range(n)] for row in matrix] for item in sublist]
... 
>>> pprint.pprint(transform(3,a))
[[85, 85, 85, 77, 77, 77, 99, 99, 99],
 [85, 85, 85, 77, 77, 77, 99, 99, 99],
 [85, 85, 85, 77, 77, 77, 99, 99, 99],
 [11, 11, 11, 22, 22, 22, 33, 33, 33],
 [11, 11, 11, 22, 22, 22, 33, 33, 33],
 [11, 11, 11, 22, 22, 22, 33, 33, 33],
 [44, 44, 44, 55, 55, 55, 66, 66, 66],
 [44, 44, 44, 55, 55, 55, 66, 66, 66],
 [44, 44, 44, 55, 55, 55, 66, 66, 66]]
>>> pprint.pprint(transform(4,a))
[[85, 85, 85, 85, 77, 77, 77, 77, 99, 99, 99, 99],
 [85, 85, 85, 85, 77, 77, 77, 77, 99, 99, 99, 99],
 [85, 85, 85, 85, 77, 77, 77, 77, 99, 99, 99, 99],
 [85, 85, 85, 85, 77, 77, 77, 77, 99, 99, 99, 99],
 [11, 11, 11, 11, 22, 22, 22, 22, 33, 33, 33, 33],
 [11, 11, 11, 11, 22, 22, 22, 22, 33, 33, 33, 33],
 [11, 11, 11, 11, 22, 22, 22, 22, 33, 33, 33, 33],
 [11, 11, 11, 11, 22, 22, 22, 22, 33, 33, 33, 33],
 [44, 44, 44, 44, 55, 55, 55, 55, 66, 66, 66, 66],
 [44, 44, 44, 44, 55, 55, 55, 55, 66, 66, 66, 66],
 [44, 44, 44, 44, 55, 55, 55, 55, 66, 66, 66, 66],
 [44, 44, 44, 44, 55, 55, 55, 55, 66, 66, 66, 66]]
>>> pprint.pprint(transform(5,a))
[[85, 85, 85, 85, 85, 77, 77, 77, 77, 77, 99, 99, 99, 99, 99],
 [85, 85, 85, 85, 85, 77, 77, 77, 77, 77, 99, 99, 99, 99, 99],
 [85, 85, 85, 85, 85, 77, 77, 77, 77, 77, 99, 99, 99, 99, 99],
 [85, 85, 85, 85, 85, 77, 77, 77, 77, 77, 99, 99, 99, 99, 99],
 [85, 85, 85, 85, 85, 77, 77, 77, 77, 77, 99, 99, 99, 99, 99],
 [11, 11, 11, 11, 11, 22, 22, 22, 22, 22, 33, 33, 33, 33, 33],
 [11, 11, 11, 11, 11, 22, 22, 22, 22, 22, 33, 33, 33, 33, 33],
 [11, 11, 11, 11, 11, 22, 22, 22, 22, 22, 33, 33, 33, 33, 33],
 [11, 11, 11, 11, 11, 22, 22, 22, 22, 22, 33, 33, 33, 33, 33],
 [11, 11, 11, 11, 11, 22, 22, 22, 22, 22, 33, 33, 33, 33, 33],
 [44, 44, 44, 44, 44, 55, 55, 55, 55, 55, 66, 66, 66, 66, 66],
 [44, 44, 44, 44, 44, 55, 55, 55, 55, 55, 66, 66, 66, 66, 66],
 [44, 44, 44, 44, 44, 55, 55, 55, 55, 55, 66, 66, 66, 66, 66],
 [44, 44, 44, 44, 44, 55, 55, 55, 55, 55, 66, 66, 66, 66, 66],
 [44, 44, 44, 44, 44, 55, 55, 55, 55, 55, 66, 66, 66, 66, 66]]
>>> 

关于python - 如何在 python 中对数组进行重采样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5785810/

相关文章:

matlab - 使用 Matlab 进行肿瘤分割的模糊 C 均值

java - 在 Java Camera2 API 中使用 OpenCV

algorithm - 直径提取的 MATLAB 圆形滑动窗口和像素值

algorithm - 寻找所有回文子串的动态规划

python - 如果API没有返回数据,如何跳到Python中的另一个循环?

python - 在 Python 2.7 中,Unicode 文本表示为 u'xxxx 而不是日语

python - Python 守护进程关闭问题的调试技术

python - 在 numpy 中向量化(平方)马哈拉诺比斯距离

r - 基于组和时间的值的共现(矩阵)

python - Apache2 "Response header name ' <!- -' contains invalid characters, aborting request"