57158

Interpolation of values when zooming down

Question:

I have a 2D array that I would like to down sample to compare it to another.

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)

So far I looked at scipy.ndimage.interpolation.zoom but I don't get the results I want

>> 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

[[ 2.5 4.5] [10.5 12.5]]

Note that simply setting dtype=np.float32 doesn't solve that ...

Answer1:

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)

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

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 <em>not</em> 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 (e.g. sigma=0.35 * subsample_factor) and then subsample simply by indexing [::2, ::2]

Answer2:

sklearn.feature_extraction.image.extract_patches cleverly uses np.lib.stride_tricks.as_strided to produce a <em>windowed</em> array that can be operated on.

The sliding_window function, found here <a href="http://www.johnvinyard.com/blog/?p=268" rel="nofollow">Efficient Overlapping Windows with Numpy</a>, produces a windowed array with or without overlap also and let's you get a glimpse of what is happening under the hood.

>>> 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.

>>> 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.

Recommend

  • Access element of N-D array using a vector [duplicate]
  • How do I teach Automapper to map X to IContainer?
  • Make pycharm use the real ipython console
  • Is there an equivalent to Mathematica's “usage” function?
  • Theano version of a numpy einsum for two 3dim matrices
  • The signature of pandas scatter_matrix method
  • Fastest way to create a sparse matrix of the form A.T * diag(b) * A + C?
  • tensorflow embeddings don't exist after first RNN example
  • Open protected word document with OpenXML
  • Mechanize not being installed by easy_install?
  • Inverse Dict in Python
  • plot if col A has substring
  • Highest supported JSF version for JBoss 4.0?
  • Different outcomes when using tf.Variable() and tf.get_variable()
  • converting Twilio sms body to a string - encoding error for @ symbol
  • Javascript: parseInt() with trailing characters
  • Matlab to Python Conversion binary file read
  • Why do I get wrong results for hmac in Python but not Perl?
  • How to find the closest (x, y) position to (x,y) position in another list?
  • Cookies getting ignored in Apache httpclient 4.4
  • python function to set accuracy of float
  • Converting a data frame into named object in R
  • Reduce Mono runtime size
  • How can i compile & run a c program (with OpenMP) in gem5 Full System?
  • Easy Way to Get Averages Based on Names in List
  • maven-dependency-plugin ignores outputDirectory configuration
  • TypeError: Value passed to parameter 'input' has DataType float64 not in list of allowed v
  • object play not found in scala application
  • Cythonized function unexpectedly slow
  • Numpy odd behaviour conversion to datetime64 dtype
  • Create function that can pass a parameter without making a new component
  • Alamofire and Reachability.swift not working on xCode8-beta5
  • How can we prepend rows to a react native list-view?
  • Getting different value with placeholder over CPU/GPU
  • How to use function wrapper in mustache.php?
  • Using JRuby with Rails 3.2
  • ORA-29908: missing primary invocation for ancillary operator
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • Load html files in TinyMce