0% found this document useful (0 votes)
15 views6 pages

Archsalt Pepper Noise S3.Ipynb - Colab

Uploaded by

aya hamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

Archsalt Pepper Noise S3.Ipynb - Colab

Uploaded by

aya hamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

keyboard_arrow_down Noise

Adding Noise to images

import random
import cv2
import matplotlib.pyplot as plt
import numpy as np

# salt-and-pepper noise can be applied only to grayscale images


# Reading the color image in grayscale image
img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
plt.imshow(img,cmap="gray")
plt.axis("off")

(np.float64(-0.5), np.float64(511.5), np.float64(511.5), np.float64(-0.5))

img.shape

(512, 512)

def add_noise(img):

# Getting the dimensions of the image


row , col = img.shape

# Randomly pick some pixels in the image for coloring them white
number_of_pixels_w = random.randint(1, row)
for i in range(number_of_pixels_w):
# Pick a random y coordinate
y_coord=random.randint(0, row - 1)

# Pick a random x coordinate


x_coord=random.randint(0, col - 1)

# Color that pixel to white


img[y_coord][x_coord] = 255

# Randomly pick some pixels in the image for coloring them black
number_of_pixels_b = random.randint(1 , row)
for i in range(number_of_pixels_b):

# Pick a random y coordinate


y_coord=random.randint(0, row - 1)

# Pick a random x coordinate


x_coord=random.randint(0, col - 1)

# Color that pixel to black


img[y_coord][x_coord] = 0
return img

noised = add_noise(img)
plt.imshow(noised,cmap="gray")
plt.axis("off")
plt.savefig('noised.jpg')

keyboard_arrow_down
Applying filters using opencv

# Applying Average (Mean) Filter


mean_filter = cv2.blur(img, (5, 5))

# Applying Median Filter


median_filter = cv2.medianBlur(img, 5)
# Applying Maximum Filter
max_filter = cv2.dilate(img, np.ones((5, 5), np.uint8))

# Applying Minimum Filter


min_filter = cv2.erode(img, np.ones((5, 5), np.uint8))

plt.figure(figsize=(8, 12))

plt.subplot( 1,5, 1)
plt.title('Original Image')
plt.imshow(img, cmap='gray')
plt.axis('off')

plt.subplot(1,5, 2)
plt.title('Mean Filter')
plt.imshow(mean_filter, cmap='gray')
plt.axis('off')

plt.subplot(1, 5, 3)
plt.title('Median Filter')
plt.imshow(median_filter, cmap='gray')
plt.axis('off')

plt.subplot(1, 5, 4)
plt.title('Max Filter')
plt.imshow(min_filter, cmap='gray')
plt.axis('off')

plt.subplot(1, 5, 5)
plt.title('Min Filter')
plt.imshow(max_filter, cmap='gray')
plt.axis('off')

plt.tight_layout()
plt.show()

# trial
maxx = cv2.dilate(min_filter, np.ones((5, 5), np.uint8))

plt.title('Using Max Filter on image after min filter')


plt.imshow(maxx, cmap='gray')
plt.axis('off')
(np.float64(-0.5), np.float64(511.5), np.float64(511.5), np.float64(-0.5))

keyboard_arrow_down
1. Mean (Average) filter

def mean_filter(image, kernel_size):


# Pad the image to handle borders using zero-padding
# This ensures that the filter can be applied to border pixels as well
padded_image = cv2.copyMakeBorder(image,
kernel_size//2,
kernel_size//2,
kernel_size//2,
kernel_size//2,
cv2.BORDER_CONSTANT)

# Create an empty image to store the filtered result


filtered_image = np.zeros_like(image)

# Iterate over each pixel in the image


for y in range(image.shape[0]):
for x in range(image.shape[1]):
# Extract the local window centered at the current pixel
window = padded_image[y:y+kernel_size, x:x+kernel_size]
# Calculate the mean value of the window
mean_value = np.mean(window)
# Assign the mean value to the corresponding pixel in the filtered image
filtered_image[y, x] = mean_value

return filtered_image

mean_filtered = mean_filter(img,5)

plt.title('Mean filter')
plt.imshow(mean_filtered, cmap='gray')
plt.axis('off')

(np.float64(-0.5), np.float64(511.5), np.float64(511.5), np.float64(-0.5))

keyboard_arrow_down
2. Median filter

kernel_size // 2, kernel_size // 2, kernel_size // 2, kernel_size // 2, cv2.BORDER_REFLECT)

ered result

l image

ed at the current pixel


el_size, x:x + kernel_size]
he window

corresponding pixel in the filtered image


lue

median_filtered = median_filter(img,3)

plt.title('Median filter')
plt.imshow(median_filtered, cmap='gray')
plt.axis('off')
(np.float64(-0.5), np.float64(511.5), np.float64(511.5), np.float64(-0.5))

keyboard_arrow_down
3. Maximum filter

keyboard_arrow_down
4. Minimum filter

You might also like