如何将数组升采样为任意大小? [英] How to upsample an array to arbitrary sizes?
问题描述
我正在尝试通过按比例将每个元素按比例地重复到新的大小来在Python中将数组调整为更大的大小。但是,我希望能够调整为任意大小。
I am trying to resize an array to a larger size in Python by repeating each element proportionally to the new size. However, I want to be able to resize to arbitrary sizes.
我知道我可以使用 numpy.repeat
例如,如果我必须将大小加倍,但可以说我想将大小为(180,150)
的数组转换为(300,250)
。我知道没有一个完美的方法可以做到这一点,但我正在寻找最有效的(信息丢失最少)方法!
I know that I can do it with numpy.repeat
if for example I have to double the size but lets say I want to convert an array of size (180,150)
to (300,250)
. I know there is not a perfect way to do this but I am looking for the most efficient (minimum loss of information) method!
到目前为止,我正在转换数组到图像并相应地调整大小,然后再次将其转换为数组。但是,看来我无法将所有类型的数据都转换为图像,所以我需要一种通用的方法。
So far, I was converting the array to an image and resize it accordingly, then convert it to an array again. However, it seems that I cannot convert all types of data to image so I need a general way to do this.
例如,假设我有一个输入数组大小(2,2)
:
For example, lets say I have an input array of size (2,2)
:
input_array = np.array([[1 ,2],[3,4]])
如果我想将其转换为(3, 3)
数组,输出可能像这样:
If I want to convert it to a (3,3)
array, output may be like:
output_array = np.array([[1,1,2 ],[1,1,2],[3,3,4]])
就像我之前说过的,我只是不想要平铺或填充零,我想通过重复一些元素来扩大尺寸。
Like I said before, I just don't want to tile or fill with zeros, I want to expand the size by repeating some of the elements.
推荐答案
对于您想要获得的最终结果没有一个清晰的想法,您的问题为您提供了多种途径和解决方案。仅举几例:
Without a clear idea about the final result you would like to achieve, your question opens multiple paths and solutions. Just to name a few:
- 使用
numpy.resize
:
import numpy as np
input_array=np.array([[1.,2],[3,4]])
np.resize(input_array, (3,3))
你会得到:
array([[1., 2., 3.],
[4., 1., 2.],
[3., 4., 1.]])
- 使用
cv2.resize
:
import cv2
import numpy as np
input_array=np.array([[1.,2],[3,4]])
cv2.resize(input_array,
(3,3),
interpolation=cv2.INTER_NEAREST)
您得到:
array([[1., 1., 2.],
[1., 1., 2.],
[3., 3., 4.]])
根据您的目标,可以使用不同的插值方法。
Depending on your objective, you can use different interpolation methods.
这篇关于如何将数组升采样为任意大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!