Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Nice question! <strong>tcaswell</strong> post here is a great suggestion, but you will not learn much this way because scipy is doing all the work for you! Since your question said you wanted to <em>try and write the function</em>, I will show you a bit more crude and basic kind of way to do it all manually in the hope that you will better understand the maths behind convolution etc, and then you can improve it with your own ideas and efforts! </p> <p>Note: You will get different results with different shapes/sizes of kernels, a Gaussian is the usual way but you can try out some other ones for fun (cosine, triangle, etc!). I just made up this one on the spot, I think it's a kind of pyramid shaped one. </p> <pre><code>import scipy.signal import numpy as np import matplotlib.pyplot as plt im = plt.imread('example.jpg') im /= 255. # normalise to 0-1, it's easier to work in float space # make some kind of kernel, there are many ways to do this... t = 1 - np.abs(np.linspace(-1, 1, 21)) kernel = t.reshape(21, 1) * t.reshape(1, 21) kernel /= kernel.sum() # kernel should sum to 1! :) # convolve 2d the kernel with each channel r = scipy.signal.convolve2d(im[:,:,0], kernel, mode='same') g = scipy.signal.convolve2d(im[:,:,1], kernel, mode='same') b = scipy.signal.convolve2d(im[:,:,2], kernel, mode='same') # stack the channels back into a 8-bit colour depth image and plot it im_out = np.dstack([r, g, b]) im_out = (im_out * 255).astype(np.uint8) plt.subplot(2,1,1) plt.imshow(im) plt.subplot(2,1,2) plt.imshow(im_out) plt.show() </code></pre> <p><img src="https://i.stack.imgur.com/NBZpl.jpg" alt="enter image description here"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload