0% found this document useful (0 votes)
4 views2 pages

Convolution with Sobel Filter in Python

Detailed convution assignmrnthjggngggfghhhfhjggjfhfhfhfhfhfhfngnfusg risks hfhfjfjfjfjdh_fufjgggjgjgg
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Convolution with Sobel Filter in Python

Detailed convution assignmrnthjggngggfghhhfhjggjfhfhfhfhfhfhfngnfusg risks hfhfjfjfjfjdh_fufjgggjgjgg
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Detailed Assignment: Convolution Implementation in Python

Write a Python program to implement convolution on a grayscale image using a custom


kernel (Sobel filter for edge detection) without using any built-in convolution functions.
Follow the detailed instructions below:

Step 1: Import necessary libraries


- Import OpenCV (cv2) for image reading and processing.
- Import NumPy (numpy) for numerical operations like array handling.
- Import [Link] for displaying images.

Step 2: Load the image in grayscale


- Use [Link]() to load the image named [Link].
- Read it in grayscale mode using the cv2.IMREAD_GRAYSCALE flag.
- Store it in a variable named image.

Step 3: Define the convolution kernel (Sobel filter)


- Create a 3 × 3 NumPy array for the Sobel filter that detects horizontal edges:
[ [1, 0, -1],
[2, 0, -2],
[1, 0, -1] ]
- Store it in a variable named kernel.

Step 4: Get dimensions of image and kernel


- Use .shape to get image_height and image_width.
- Use .shape to get kernel_height and kernel_width.

Step 5: Create an output image


- Use np.zeros_like(image) to create a new image (output) of the same size as the input
image, filled with zeros.

Step 6: Pad the input image


- Calculate padding sizes:
pad_height = kernel_height // 2
pad_width = kernel_width // 2
- Use [Link]() to pad the image with zeros on all sides.
- Store the padded image in a variable named padded_image.

Step 7: Perform the convolution operation


- Loop through each pixel position (i, j) of the original image.
- For each position:
1. Extract a Region of Interest (ROI) from the padded_image equal to the kernel size.
2. Multiply the ROI and the kernel element-wise.
3. Sum all the multiplied values.
4. Assign this sum to output[i, j].

Step 8: Normalize the output image


- Use [Link]() to scale values between 0 and 255.

Step 9: Display the results


- Use matplotlib to display the original and convolved images side by side.
- Remove axis ticks using [Link]('off').
- Add titles: 'Original Image' and 'Convolved Image'.

Final Output Expected


- Left: Original grayscale image.
- Right: Sobel-filtered image showing detected edges.

You might also like