{{ message }}

Instantly share code, notes, and snippets.

# AllanLRH/image_convolution_mean_example.m

Last active Jan 27, 2019
 % **************************************************************************** % * This demonstrate how to apply blur to an RGB-image, and create a third * % * image which is composed partly of the original unblurred image, and * % * partly of the blurred image. * % **************************************************************************** % Read example image built into MATLAB I0 = double(imread('saturn.png')); % Define convolution kernel % I am using a 40 by 40 kernel to make the difference easily visible on an % image of this size, but you can just change n to be 4 or whatever integer % value you desire n = 40; kernel = ones(n)/n^2; % Apply a convolution to each of the channels (R, G, B) in the image, and % assign the result to a new image I1. I1 = zeros(size(I0)); for ii = 1:3 I1(:, :, ii) = conv2(I0(:, :, ii), kernel, 'same'); end % Create a third image, composed partly of I0 and I1, such that part of % it is blurry I2 = I0; I2(1:300, 1:450, :) = I1(1:300, 1:450, :); fig = figure; subplot(1, 3, 1) imshow(uint8(I0)); subplot(1, 3, 2) imshow(uint8(I1)); subplot(1, 3, 3) imshow(uint8(I2)); % maximize; % maximize the figure window
 %MAXIMIZE Maximize a figure window to fill the entire screen % % Examples: % maximize % maximize(hFig) % % Maximizes the current or input figure so that it fills the whole of the % screen that the figure is currently on. This function is platform % independent. % %IN: % hFig - Handle of figure to maximize. Default: gcf. function maximize(hFig) if nargin < 1 hFig = gcf; end warning('off','MATLAB:HandleGraphics:ObsoletedProperty:JavaFrame'); drawnow % Required to avoid Java errors jFig = get(handle(hFig), 'JavaFrame'); jFig.setMaximized(true);
 import numpy as np import matplotlib as matplotlib # for plotting import matplotlib.pyplot as plt # Answer to Quora question: # https://www.quora.com/How-do-I-perform-moving-average-in-Python # Create some data np.random.seed(17) n = 10 N = 500 x = np.linspace(0, n, N) y0 = -0.05*x**4 + 5*x**2 + 7*x - 6 yn = 4.5*np.random.standard_normal(N) * np.log10(y0**2 + 0.1)/2 y = y0 + yn # Create a figure canvas and plot the original, noisy data fig, ax = plt.subplots() ax.plot(x, y, label="Original") # Compute moving averages using different window sizes window_lst = [3, 6, 10, 16, 22, 35] y_avg = np.zeros((len(window_lst) , N)) for i, window in enumerate(window_lst): avg_mask = np.ones(window) / window y_avg[i, :] = np.convolve(y, avg_mask, 'same') # Plot each running average with an offset of 50 in order to be able to distinguish them ax.plot(x, y_avg[i, :] + (i+1)*50, label=window) # Add legend to plot ax.legend() plt.show() # http://mathworld.wolfram.com/NormalDistribution.html gaussian_func = lambda x, sigma: 1/np.sqrt(2*np.pi*sigma**2) * np.exp(-(x**2)/(2*sigma**2)) fig, ax = plt.subplots() # Compute moving averages using different window sizes sigma_lst = [1, 2, 3, 5, 8, 10] y_gau = np.zeros((len(sigma_lst), N)) for i, sigma in enumerate(sigma_lst): gau_x = np.linspace(-2.7*sigma, 2.7*sigma, 6*sigma) gau_mask = gaussian_func(gau_x, sigma) y_gau[i, :] = np.convolve(y, gau_mask, 'same') # Plot each running average with an offset of 50 in order to be able to distinguish them ax.plot(x, y_gau[i, :] + (i+1)*50, label=r"$\sigma = {}$, $points = {}$".format(sigma, len(gau_x))) # Add legend to plot ax.legend(loc='upper left') plt.show()