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

Computer Vision Tutorial Series M2C2
Computer Vision   Latest   Machine Learning

Computer Vision Tutorial Series M2C2

Last Updated on May 22, 2023 by Editorial Team

Author(s): Sujay Kapadnis

Originally published on Towards AI.

Module 2 β€” Convolutional Filters and Edge Detection

Chapter 2β€” Filters

Starting here? This article is part of a computer vision Tutorial Series. Here’s where you can start.

Learning Objectives:

  1. What are Filters?
  2. What is sobel filter?
  3. How to create and apply sobel filter?
  4. What is the Laplacian filter?
  5. How to create and apply the Laplacian filter?

Pre-Requisites: Previous Tutorials

source: MidJourney

What is a filter?

In computer vision, a filter is a mathematical operation that is applied to an image to transform it in some way. Filters are used to extract information from images, enhance features, remove noise, and prepare images for further processing.

There are many different types of filters used in computer vision, each with a specific purpose. Some common filters include:

  1. Gaussian filter: smooths an image by removing high-frequency noise.
  2. Sobel filter: detects edges in an image by computing the gradient of the image in the horizontal and vertical directions.
  3. Laplacian filter: detects changes in intensity in an image by computing the second derivative of the image.
  4. Median filter: removes noise from an image by replacing each pixel with the median value of its neighboring pixels.
  5. Bilateral filter: smooths an image while preserving edges

Filters work by convolving an image with a small matrix, known as a kernel or a filter. The filter is centered at each pixel of the image, and the values of the neighboring pixels are multiplied by the corresponding values in the filter matrix. The resulting products are then summed, and the sum is used to replace the value of the pixel being processed.

  1. Imports
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import cv2

2. Load the images in the format you want

Since we use RGB colorscheme, I will load images directly into RGB channels

image3 = cv2.cvtColor(cv2.imread('image_path'),cv2.COLOR_BGR2RGB)

3. Plot the image

plt.imshow(image3)
plt.set_title('Image 3')
plt.show()

for the sample I have taken the image displayed is as below

output

Writing a helper function to convert our image to grayScale

def to_gray(image):
return cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)

Creating A kernel

Sobel Filter β€” Used to detect verticle edges

  • format -> [-k,0,k]
  • detect abrupt intensity changes in a certain direction(horizontal here)

4. Sobel Filter

below is the sample format of how a sobel filter looks like, if you want to learn the maths behind these filters, you can refer to this article

sobel_x = np.array([[-1,0,1],
[-5,0,5],
[-1,0,1]])
filtered_x = cv2.filter2D(to_gray(image3),-1,sobel_x)
#represents that 'do not change the format of output image and keep it same as input image'
plt.imshow(filtered_x,cmap='gray')

output:

output

Let's see how the change in threshold value changes the output

# Set a threshold, so that points above threshold will be white/visible
retval, binary_image1 = cv2.threshold(filtered_x,50,255,cv2.THRESH_BINARY)
retval, binary_image2 = cv2.threshold(filtered_x,100,255,cv2.THRESH_BINARY)
retval, binary_image3 = cv2.threshold(filtered_x,150,255,cv2.THRESH_BINARY)
retval, binary_image4 = cv2.threshold(filtered_x,200,255,cv2.THRESH_BINARY)

fig,(ax1,ax2,ax3,ax4) = plt.subplots(1,4,figsize=(15,8))
ax1.set_title('threshold 50')
ax1.imshow(binary_image1,cmap='gray')
ax2.set_title('threshold 100')
ax2.imshow(binary_image2,cmap='gray')
ax3.set_title('threshold 150')
ax3.imshow(binary_image3,cmap='gray')
ax4.set_title('threshold 200')
ax4.imshow(binary_image4,cmap='gray')
output

Why Set a Threshold?

  • The purpose of using a threshold here is to remove the noise present in an image; for example, in image 1 with a threshold of 50, we detected a lot of lines, but as we increased the threshold, fewer lines were detected.
  • We need a high threshold when we need only strong lines from an image and not noise.
  • consider the image we are experimenting rn. Suppose that

Sobel Filter β€” Used to detect horizontal edges

format -> [-k,
0,
k]
  • detect abrupt intensity changes in a certain direction(vertical here)
sobel_y = np.array([[-1,-5,-1],
[0,0,0],
[1,5,1]])
filtered_y = cv2.filter2D(to_gray(image3),-1,sobel_y)
plt.imshow(filtered_y,cmap='gray')
output
# Set a threshold, so that points above threshold will be white/visible
retval, binary_image1 = cv2.threshold(filtered_y,50,255,cv2.THRESH_BINARY)
retval, binary_image2 = cv2.threshold(filtered_y,100,255,cv2.THRESH_BINARY)
retval, binary_image3 = cv2.threshold(filtered_y,150,255,cv2.THRESH_BINARY)
retval, binary_image4 = cv2.threshold(filtered_y,200,255,cv2.THRESH_BINARY)

fig,(ax1,ax2,ax3,ax4) = plt.subplots(1,4,figsize=(15,8))
ax1.set_title('threshold 50')
ax1.imshow(binary_image1,cmap='gray')
ax2.set_title('threshold 100')
ax2.imshow(binary_image2,cmap='gray')
ax3.set_title('threshold 150')
ax3.imshow(binary_image3,cmap='gray')
ax4.set_title('threshold 200')
ax4.imshow(binary_image4,cmap='gray')
output

Laplacian Filter

  • A Laplacian filter is used to enhance edges in an image. It applies a second-order derivative kernel to an image to detect changes in intensity.
kernel_size = 3
def lap_filter(k):
laplacian_filter = np.array([[1, 1, 1],
[1, -k, 1],
[1, 1, 1]]
)
laplacian_filter = laplacian_filter / np.sum(np.abs(laplacian_filter))
return laplacian_filter
# Apply Laplacian filter

img = cv2.imread('img_path', cv2.IMREAD_GRAYSCALE)

filtered_3 = cv2.filter2D(img, -1, lap_filter(3))
filtered_5 = cv2.filter2D(img, -1, lap_filter(5))
filtered_7 = cv2.filter2D(img, -1, lap_filter(7))

fig,(ax1,ax2,ax3) = plt.subplots(1,3,figsize=(15,8))
ax1.set_title('k = 3')
ax1.imshow(filtered_3,cmap='gray')
ax2.set_title('k = 5')
ax2.imshow(filtered_5,cmap='gray')
ax3.set_title('k = 7')
ax3.imshow(filtered_7,cmap='gray')
output

Wrap Up

With this, we have completed our learning objectives for this lesson.

After this, You will create filters and apply them to images of your choice.

Refer: Reading Material

  1. Laplacian Filters
  2. Median Filters
  3. Bilateral Filters

Link to GitHub.

Links Catalogue here

Upcoming

This is it for Filters and their types, next tutorial will be dedicated to Gaussian Filter

With this, we have come to the end of this article. See you at the next one

Until then, Follow for more, and don’t forget to connect with me on LinkedIn.U+2764U+2764U+2764

Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.

Published via Towards AI

Feedback ↓