Untitled

 avatar
unknown
python
a year ago
1.1 kB
3
Indexable
import numpy as np
import imageio
import matplotlib.pyplot as plt

# Load the image
image = imageio.imread('your_image.jpg')

# Define a 5x5 kernel for blurring
kernel = np.ones((5, 5)) / 25

# Get the dimensions of the image and kernel
image_height, image_width, num_channels = image.shape
kernel_height, kernel_width = kernel.shape

# Pad the image to handle borders
pad_height = kernel_height // 2
pad_width = kernel_width // 2
padded_image = np.pad(image, ((pad_height, pad_height), (pad_width, pad_width), (0, 0)), mode='constant')

# Initialize the output image
blurred_image = np.zeros_like(image)

# Perform convolution using nested for loops
for y in range(image_height):
    for x in range(image_width):
        for c in range(num_channels):
            region = padded_image[y:y+kernel_height, x:x+kernel_width, c]
            blurred_image[y, x, c] = np.sum(region * kernel)

# Display the blurred image
plt.imshow(blurred_image)
plt.axis('off')
plt.show()

# Save the blurred image if needed
imageio.imwrite('blurred_image.jpg', blurred_image)
Editor is loading...
Leave a Comment