convolution

What is the best way to create a block matrix form a row vector?

◇◆丶佛笑我妖孽 提交于 2019-12-11 02:16:43
问题 I have the following numpy row matrix. X = np.array([1,2,3]) I want to create a block matrix as follows: 1 0 0 2 1 0 3 2 1 0 3 2 0 0 3 How can I do this using numpy? 回答1: Approach #1 : Using np.lib.stride_tricks.as_strided - from numpy.lib.stride_tricks import as_strided as strided def zeropad_arr_v1(X): n = len(X) z = np.zeros(len(X)-1,dtype=X.dtype) X_ext = np.concatenate(( z, X, z)) s = X_ext.strides[0] return strided(X_ext[n-1:], (2*n-1,n), (s,-s), writeable=False) Note that this would

Point-product with fft

余生颓废 提交于 2019-12-10 22:03:10
问题 According to the convolution theorem, a convolution in the time domain is a product in the fft domain. With correct zero-padding, it works: % convolution in time domain a = [1 2 3]; b = [4 5 6]; c = conv(a,b); a_padded=[a 0 0]; b_padded=[b 0 0]; c_bis=ifft(fft(a_padded).*fft(b_padded)); % we do find c_bis=c However, this theorem is suposed to work the other way around as well, a product in the time domain is a convolution in the fft domain. I dont get this part: d = a.*b; D=conv(fft(a_padded)

Matlab image filtering without using conv2

喜夏-厌秋 提交于 2019-12-10 21:27:19
问题 I've been given a task to create image filtering function for 3x3 matrices, and its outcome must be equal to conv2's. I have written this function, but it filters image incorrectly: function [ image ] = Func134( img,matrix ) image=img; len=length(img) for i=2:1:len-1 for j=2:1:len-1 value=0; for g=-1:1:1 for l=-1:1:1 value=value+img(i+g,j+l)*matrix(g+2,l+2); end end image(i,j)=value; end end i=1:1:length image(i,1)=image(i,2) image(i,len)=image(i,len-1) image(1,i)=image(2,i) image(len,i)

Checking fftw3 with valgrind

允我心安 提交于 2019-12-10 19:17:23
问题 In one step of my program I need to convolve an image. To do that I am using the functions provided by fftw3 . When I run valgrind on my program I get this stack trace. My function is called convolve and it runs fftw3 's fftw_plan_dft_r2c_2d two times (once on the image, once on the convolution kernel. In order to make it more readable, I removed all addresses and process IDs. HEAP SUMMARY: in use at exit: 62,280 bytes in 683 blocks total heap usage: 178,271 allocs, 177,588 frees, 36,617,058

Fastest method to compute convolution

此生再无相见时 提交于 2019-12-10 18:42:11
问题 I have to apply a convolution filter on each row of many images. The classic is 360 images of 1024x1024 pixels. In my use case it is 720 images 560x600 pixels. The problem is that my code is much slower than what is advertised in articles. I have implemented the naive convolution, and it takes 2m 30s. I then switched to FFT using fftw. I used complex 2 complex, filtering two rows in each transform. I'm now around 20s. The thing is that articles advertise around 10s and even less for the

computational complexity of convolution

拈花ヽ惹草 提交于 2019-12-10 16:47:45
问题 I read that the computational complexity of the general convolution algorithm is O(n^2) , while by means of the FFT is O(n log n) . What about convolution in 2-D and 3-D? Any reference? 回答1: As for two- and three-dimensional convolution and Fast Fourier Transform the complexity is following: 2D 3D Convolution O(n^4) O(n^6) FFT O(n^2 log^2 n) O(n^3 log^3 n) Reference: Slides on Digital Image Processing, slide no. 34. 来源: https://stackoverflow.com/questions/16164749/computational-complexity-of

How do I do convolution in F#?

雨燕双飞 提交于 2019-12-10 16:13:38
问题 I would like convolve a discrete signal with a discrete filter. The signal and filter is sequences of float in F#. The only way I can figure out how to do it is with two nested for loops and a mutable array to store the result, but it does not feel very functional. Here is how I would do it non-functional: conv = double[len(signal) + len(filter) - 1] for i = 1 to len(signal) for j = 1 to len(filter) conv[i + j] = conv[i + j] + signal(i) * filter(len(filter) - j) 回答1: Try this function: let

is There any function in opencv which is equivalent to matlab conv2

倖福魔咒の 提交于 2019-12-10 15:53:26
问题 Is there any direct opencv function for matlab function conv2? I tried using cvFilter2D(), but it seems to be giving me different results than conv2(). For example: CvMat * Aa = cvCreateMat(2, 2, CV_32FC1); CvMat * Bb = cvCreateMat(2, 2, CV_32FC1); CvMat * Cc = cvCreateMat(2, 2, CV_32FC1); cvSetReal2D(Aa, 0, 0, 1); cvSetReal2D(Aa, 0, 1, 2); cvSetReal2D(Aa, 1, 0, 3); cvSetReal2D(Aa, 1, 1, 4); cvSetReal2D(Bb, 0, 0, 5); cvSetReal2D(Bb, 0, 1, 5); cvSetReal2D(Bb, 1, 0, 5); cvSetReal2D(Bb, 1, 1, 5)

Hard to understand Caffe MNIST example

心不动则不痛 提交于 2019-12-10 15:23:11
问题 After going through the Caffe tutorial here: http://caffe.berkeleyvision.org/gathered/examples/mnist.html I am really confused about the different (and efficient) model using in this tutorial, which is defined here: https://github.com/BVLC/caffe/blob/master/examples/mnist/lenet_train_test.prototxt As I understand, Convolutional layer in Caffe simply calculate the sum of Wx+b for each input, without applying any activation function. If we would like to add the activation function, we should

A reusable Tensorflow convolutional Network

一个人想着一个人 提交于 2019-12-10 12:56:50
问题 I want to reuse code from the Tensorflow "MNIST for Pros" CNN example. My images are 388px X 191px, with only 2 output classes. The original code can be found here. I tried to reuse this code by changing the input & output layers ONLY , as shown below: input layer x = tf.placeholder("float", shape=[None, 74108]) y_ = tf.placeholder("float", shape=[None, 2]) x_image = tf.reshape(x, [-1,388,191,1]) output layer W_fc2 = weight_variable([1024, 2]) b_fc2 = bias_variable([2]) Running the modified