15-Matlab

Filter in Matlab and Python 

As I mentioned at the start of this class, I'll be showing my examples mostly using Matlab. I know we've looked at the course developer. Arpin has worked on being able to do it in Octave or also in Python using various image manipulation OpenCV.But what I'm going to do is show you filtering in Matlab. And basically Matlab makes it trivial to build filters and apply filters.

  

So Matlab, what we're going to do is define two things. We're going to define the size of the kernel. Remember that's what we were talking about before. So in this case it's going to be a 31 by 31. Again, odd, so I can put a center pixel down. I'm going to a sigma of five, and Matlab has this really great little function called fspecial. And obviously it's special, or they would call it something else. In fspecial, you can give it parameters, one of which is the type of filter you'd like. You can give it the size and the sigma. You can also give it rectangular size and multiple sigmas. It will build these filters for you. And in fact, Matlab has this beautiful little function called surf. Right, which will plot for you as a surface and if you do it with the right color map you would see this. You could also show it as an image. That's what this little picture is right here, all right? But even more importantly, you can take your image, and that's our image here of a panda, I can filter it by this h, which was the filter we just built, and then I can show that. What's that going to look like? It's going to be a blurry panda. Okay. This code is all it takes to build your filters and apply them to images in Matlab. It makes it very easy.

Python 

import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import gaussian_filter
from scipy.signal import convolve2d
hsize = 31
sigma = 5
# Create Gaussian kernel
h = np.outer(signal.gaussian(hsize, std=sigma), signal.gaussian(hsize, std=sigma))
# Display 3D surface plot of the kernel
fig = plt.figure()
ax = fig.add_subplot(121, projection='3d')
ax.plot_surface(np.arange(hsize), np.arange(hsize), h, cmap='viridis')
ax.set_title('3D Surface Plot of Gaussian Kernel')
# Display 2D image plot of the kernel
plt.subplot(122)
plt.imshow(h, cmap='viridis')
plt.title('2D Image Plot of Gaussian Kernel')
plt.colorbar()
plt.show()
# Assuming 'im' is your input image
# Apply Gaussian filter to the input image
outim = gaussian_filter(im, sigma)
# Display the filtered image
plt.imshow(outim, cmap='gray')
plt.title('Filtered Image')
plt.colorbar()
plt.show()
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Load the input image (assuming 'im' is your input image)
im = cv2.imread('input_image.jpg', cv2.IMREAD_GRAYSCALE)
# Define the size and sigma value for the Gaussian kernel
hsize = 31
sigma = 5
# Create the Gaussian kernel using OpenCV
h = cv2.getGaussianKernel(hsize, sigma)
# Apply Gaussian blur to the input image
outim = cv2.filter2D(im, -1, h)
# Display the filtered image
plt.imshow(outim, cmap='gray')
plt.colorbar()
plt.show()

Section 2- Smoothing with a Gaussian 

Again, depending upon the size of the sigma we get different amounts of smoothing. So here we're using three different sigmas of 1, 3, and 10. We build our Gaussians using the different sized sigmas. We filter them and show them, and you see that we get, you know, hardly any blurring, little more blurring, and a little more blurring. That's all it takes to build these filters in Matlab.

Last modified: Saturday, 23 March 2024, 11:34 AM