Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Unlock the full potential of AI with Building LLMs for Productionβ€”our 470+ page guide to mastering LLMs with practical projects and expert insights!

Publication

Image Processing using Spatial Filters
Computer Vision

Image Processing using Spatial Filters

Last Updated on February 5, 2021 by Editorial Team

Author(s): Ralph Caubalejo

Computer Vision, Programming

Giving Space…

One of the wonders of image processing is being able to see the pixel values of each image and perform different mathematical operations onΒ it.

Since the image is already an array or a matrix, then we can already perform different operations like multiplying, adding, subtracting, and dividing.

One of the most used operation in image processing is convolution, which is primarily used for filtering theΒ image.

The process usually stems from convolving an image array into a user-defined array orΒ matrix.

This user-defined matrix are known as kernels and are usually have a different effect depending on the value they have especially the sign orientation,

For this article, we will try different spatial kernels that can convolve into a specificΒ image.

Let us load our sampleΒ image!

import numpy as np
from skimage.io import imshow, imread
import matplotlib.pyplot as plt
mple = imread('stand.png')
imshow(sample);
(Image byΒ Author)

Using Different Filters:

Horizontal Sobel

import matplotlib.pyplot as plt
from scipy.signal import convolve2d
import numpy as np
sample_g = rgb2gray(sample)
#defining the kernels/filter
# Horizontal Sobel Filter
kernel1 = np.array([[1, 2, 1],
[0, 0, 0],
[-1, -2, -1]])
conv1 = convolve2d(sample_g, kernel1, 'valid')
fig, ax = plt.subplots(1,3,figsize=(15,10))
ax[0].imshow(sample_g,cmap='gray')
ax[1].imshow(kernel1,cmap='gray')
ax[2].imshow(abs(conv1),cmap='gray')
ax[0].set_title('Grayscale Image',fontsize=20)
ax[1].set_title('Filter Specs',fontsize=20)
ax[2].set_title('Using Horizontal Sobel',fontsize=20)
plt.show()

The general step to convolve a separate array to another is to make the image is on the grayscale dimension. We can use a usable function from the scipy library to convolve a specific image. The codes for the following other filters follow some principle, the only thing that changes are the value of the array integers, this can be described by the filter specsΒ graph.

(Image byΒ Author)

The results show that the horizontal Sobel filter was able to attenuate the horizontal features and horizontal lines in theΒ image.

Using VerticalΒ Sobel

(Image byΒ Author)

The experimental vertical Sobel was able to attenuate the vertical features of the image, for this example, we can see the vertical line of the stand more clearly if we compare it to the results of the horizontal Sobel enhancing.

Using Left DiagonalΒ Filer

(Image byΒ Author)

The diagonal filter in a sense is just the same as the vertical and horizontal Sobel but more attenuating the diagonal features of theΒ image.

Using Right DiagonalΒ Filter

(Image byΒ Author)

We tried also doing the inverse of the left diagonal matrix which is the right diagonal matrix, and an interesting insight was seen, the results show that there is no difference between using the left or right diagonalΒ filter.

Using Edge Detection Filter

(Image byΒ Author)

As the name suggests, an edge detection filter is an array that can attenuate the edges of the image as from the results, we were able to clearly show the edges of the woodenΒ stand.

Using a Sharpening Filter

(Image byΒ Author)

Sharpening is always used in a lot of photo editing, and the math behind it is convolving an image to a predefined array wherein we can attenuate the center by putting negative values to the cross-segment.

Using a Box BlurΒ Filter

(Image byΒ Author)

Blurring is the act of subdividing the pixel values of the defined array. From the results, we might think that the image was not blurred, but in reality, is blurred by a fraction.

Using a Gaussian BlurΒ Filter

(Image byΒ Author)

Gaussian Blur Filter works the same way as the box blur but with a much more blurringΒ effect

Summary

In summary, we were able to show the different spatial filters that we can use for an image. Normally these filters are already built-in in a lot of photo editing software but the theory behind it is that all of the filters are stemmed from basic linear algebra functions.

Stay tuned for the nextΒ article!


Image Processing using Spatial Filters was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.

Published via Towards AI

Feedback ↓