stride

numpy stride_tricks.as_strided vs list comprehension for rolling window

妖精的绣舞 提交于 2021-02-18 07:26:11
问题 When dealing with rolling windows, I wrote my functions in the way like list comprehension [np.std(x[i:i+framesize]) for i in range(0, len(x)-framesize, hopsize)])] Recently I discovered numpy.lib.stride_tricks.as_strided and found it is used widely for rolling windows (for example, this post), even though it is a "hidden" function. In this issue concerning why stride_tricks.as_strided is undocumented, it's mentioned that Intentionally! It's dangerous! It was just low-level plumbing to help

Sliding windows along last axis of a 2D array to give a 3D array using NumPy strides

青春壹個敷衍的年華 提交于 2021-02-08 02:03:46
问题 I am trying to use the function as_strided from numpy.lib.stride_tricks to extract sub series from a larger 2D array, but I struggled to find the right thing to write for the strides argument. Let's say I have a matrix m which contains 5 1D array of length ( a= )10. I want to extract sub 1D arrays of length ( b= )4 for each 1D array in m . import numpy from numpy.lib.stride_tricks import as_strided a, b = 10, 4 m = numpy.array([range(i,i+a) for i in range(5)]) # first try sub_m = as_strided(m

Inverse function of numpy as_strided

人盡茶涼 提交于 2021-01-28 03:41:28
问题 I have a 4-tensor x . The 6-tensor y is computed as follows: x = np.random.randn(64, 28, 28, 1) strided_shape = 64, 26, 26, 3, 3, 1 y = numpy.lib.stride_tricks.as_strided(x, strided_shape, strides=(x.strides[0], x.strides[1], x.strides[2], x.strides[1], x.strides[2], x.strides[3])) strided_shape in general can be any shape as long as the first and last dimensions match those of x (this is just a concrete example). My question is, using y (and the x.shape and x.strides tuples), is it possible

How can I copy the pixel data from a Bitmap with negative stride?

大憨熊 提交于 2020-01-19 14:16:20
问题 I was looking for the fastest way to convert a Bitmap to 8bpp. I found 2 ways: 1. public static System.Drawing.Image ConvertTo8bpp(Bitmap oldbmp) { using (var ms = new MemoryStream()) { oldbmp.Save(ms, ImageFormat.Gif); ms.Position = 0; return System.Drawing.Image.FromStream(ms); } } 2. http://www.wischik.com/lu/programmer/1bpp.html But: 1. Results in a very low quality result (bad pallet) and 2 gives me a Bitmap with negative stride, when I try to lockbits and copy the data to a byte array I

It is possible to get an IntPtr from an int[] array?

前提是你 提交于 2019-12-30 17:35:27
问题 Greetings. In C#: If I have an int[] array declared like this int[] array = new array[size]; there is an way to get the IntPtr from this array? The thing is that I'm using the EmguCV framework, and there is an constructor to create an image which takes an IntPtr to the pixel data, in order to build an image from an array (int[]). Image<Gray,Int32> result = new Image<Gray,int>(bitmap.Width,bitmap.Height,stride,"**array.toPointer??**"); By the way if someone could told me how to calculate the

as_strided: Linking stepsize (strides of conv2d) with as_strided strides parameter

烈酒焚心 提交于 2019-12-24 08:59:10
问题 I found that for generating (X - x + 1, Y - y + 1) patches of size (x,y) from (X,Y) with stride 1, image requires us to give strides parameter as img.strides * 2 or img.strides + img.strides . I don't know how they quickly compute this knowing the no. of strides in conv2d But what should I do to get ((X-x)/stride)+1, ((Y-y)/stride)+1 patches of same size from same sized image with stride stride? From this SO answer with slight modification, with channels and number of images placed in front

Extract random 2d windows of a 2d numpy array

风流意气都作罢 提交于 2019-12-24 00:21:10
问题 import numpy as np arr = np.array(range(60)).reshape(6,10) arr > array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], > [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], > [20, 21, 22, 23, 24, 25, 26, 27, 28, 29], > [30, 31, 32, 33, 34, 35, 36, 37, 38, 39], > [40, 41, 42, 43, 44, 45, 46, 47, 48, 49], > [50, 51, 52, 53, 54, 55, 56, 57, 58, 59]]) What I need: select_random_windows(arr, number_of windows= 3, window_size=3) > array([[[ 1, 2, 3], > [11, 12, 13], > [21, 22, 23]], > > [37, 38, 39], > [47, 48, 49], > [57

D dynamic array initialization, stride and the index operation

半城伤御伤魂 提交于 2019-12-23 12:10:19
问题 Sorry, this became a 3-fold question regarding arrays I think (dynamic) arrays are truly powerful in D, but the following has been bothering me for a while: In C++ I could easily allocate an array with designated values, but in D I haven't found a way to do so. Surely the following is no problem: int[] a = new int[N]; a[] = a0; But it looks inefficient, since line one will initialize with 0 , and like 2 with a0 . Could something similar to the following be done in D? int[] a = new int(a0)[N];

Swift 2.2 decrementing specific for loop in Swift 3

有些话、适合烂在心里 提交于 2019-12-20 06:29:45
问题 I have the task to refactor an iOS app to Swift 3. However, there is a for loop, in C-style, that does more than just looping an array backwards (it's mandatory to be backwards). This is a sample code. The principle is the same. let array = ["hello", "world", nil, "foo", nil, "bar", "Peter Griffin"] var threeLetterWords = 0 for var i = array.count-1; i >= 0 && array[i].characters.count == 3; --i, ++threeLetterWords { } print("Found words: \(threeLetterWords)") // should say `Found words: 2` I

Slanted bitmap, stride calculation for RGB565 C#

三世轮回 提交于 2019-12-20 04:10:59
问题 Some of my resulting images are slanted, some are not. Expected Result: (529x22) Actual Result: (529x22) Don't mind the different image sizes, these are screenshots. They are both 529x22. The code I am using, I just got this from an answer on a question here at SO. // some other method byte[] pixels = new byte[size - 16]; Array.Copy(this.data, offset, pixels, 0, pixels.Length); this.ByteToImage(w, h, pixels); // builds the pixels to a image private Bitmap ByteToImage(int w, int h, byte[]