简体   繁体   English

缩小时的值插值

[英]Interpolation of values when zooming down

I have a 2D array that I would like to down sample to compare it to another. 我有一个2D阵列,我想下采样以将其与另一个阵列进行比较。

Lets say my array x is 512x512 , I'd like an array y 128x128 where the elements of y are build using an interpolation of the values overs 4x4 blocks of x (this interpolation could just be taking the average, but other methodes, like geometric average, could be interesting) 比方说,我的阵列x512x512 ,我想一个数组y 128x128 ,其中的元素y使用值进行插补接管的建设4x4x (此插值可能只是取平均值,但其他methodes,像几何一般,可能很有趣)

So far I looked at scipy.ndimage.interpolation.zoom but I don't get the results I want 到目前为止,我看了scipy.ndimage.interpolation.zoom但没有得到想要的结果

>> x = np.arange(16).reshape(4,4)
>> print(x)
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]
>> y = scipy.ndimage.interpolation.zoom(x, 0.5)
>> print(y)
[[ 0  3]
 [12 15]]

I expected y to be 我预计y

[[ 2.5  4.5]
 [10.5 12.5]]

Note that simply setting dtype=np.float32 doesn't solve that ... 请注意,仅设置dtype=np.float32并不能解决该问题。

sklearn.feature_extraction.image.extract_patches cleverly uses np.lib.stride_tricks.as_strided to produce a windowed array that can be operated on. sklearn.feature_extraction.image.extract_patches巧妙地使用np.lib.stride_tricks.as_strided来生成np.lib.stride_tricks.as_strided窗口阵列。

The sliding_window function, found here Efficient Overlapping Windows with Numpy , produces a windowed array with or without overlap also and let's you get a glimpse of what is happening under the hood. sliding_window函数(在这里有Numpy的Efficient Overlapping Windows与Numpy结合使用 )可以生成带有或不具有重叠的窗口数组,让您了解引擎盖下正在发生的事情。

>>> a = np.arange(16).reshape(4,4)

step_height,step_width determines the overlap for the windows - in your case the steps are the same as the window size, no overlap. step_height,step_width确定窗口的重叠-在您的情况下,步骤与窗口大小相同,没有重叠。

>>> window_height, window_width, step_height, step_width = 2, 2, 2, 2
>>> y = sliding_window(a, (window_height, window_width), (step_height,step_width))
>>> y
array([[[ 0,  1],
        [ 4,  5]],

       [[ 2,  3],
        [ 6,  7]],

       [[ 8,  9],
        [12, 13]],

       [[10, 11],
        [14, 15]]])

Operate on the windows: 在窗户上操作:

>>> y = y.mean(axis = (1,2))
>>> y
array([  2.5,   4.5,  10.5,  12.5])

You need to determine the final shape depending on the number of windows. 您需要根据窗户的数量确定最终形状。

>>> final_shape = (2,2)
>>> y = y.reshape(final_shape)
>>> y
array([[  2.5,   4.5],
       [ 10.5,  12.5]])

Searching SO for numpy , window, array should produce numerous other answers and possible solutions. 在SO中搜索numpy ,window,array应该会产生许多其他答案和可能的解决方案。

What you seem to be looking for is the mean over blocks of 4, which is not obtainable with zoom , since zoom uses interpolation (see its docstring) 您似乎要查找的是4块的平均值,该值不能通过zoom获得,因为zoom使用插值(请参见其文档字符串)

To obtain what you show, try the following 要获取您显示的内容,请尝试以下操作

import numpy as np
x = np.arange(16).reshape(4, 4)

xx = x.reshape(len(x) // 2, 2, x.shape[1] // 2, 2).transpose(0, 2, 1, 3).reshape(len(x) // 2, x.shape[1] // 2, -1).mean(-1)

print xx

This yields 这产生

[[  2.5   4.5]
 [ 10.5  12.5]]

Alternatively, this can be done using sklearn.feature_extraction.image.extract_patches 或者,可以使用sklearn.feature_extraction.image.extract_patches完成此操作

from sklearn.feature_extraction.image import extract_patches

patches = extract_patches(x, patch_shape=(2, 2), extraction_step=(2, 2))

xx = patches.mean(-1).mean(-1)

print xx

However, if your goal is to subsample an image in a graceful way, then taking the mean over blocks of the image is not the right way to do it: It is likely to cause aliasing effects. 但是,如果您的目标是以优美的方式对图像进行二次采样,则对图像的块进行均值并不是正确的方法:它很可能会导致混叠效果。 What you should do in this case is smooth the image ever so slightly using scipy.ndimage.gaussian_filter (eg sigma=0.35 * subsample_factor ) and then subsample simply by indexing [::2, ::2] 在这种情况下,您应该使用scipy.ndimage.gaussian_filter稍微平滑图像(例如sigma=0.35 * subsample_factor ),然后仅通过索引[::2, ::2]进行子采样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM