0% found this document useful (0 votes)
5 views5 pages

ArchGuass - S04.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)
5 views5 pages

ArchGuass - S04.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/ 5

keyboard_arrow_down Guasssian Noise Removal

1. Guassian Filter

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

def show(img):
plt.axis('off')
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))

# add guassian noise


def add_gaussian_noise(img):
# Genearte noise with same shape as that of the image
noise = np.random.normal(0, 50, img.shape)
# Add the noise to the image
img_noised = img + noise

# Clip the pixel values to be between 0 and 255.


img_noised = np.clip(img_noised, 0, 255).astype(np.uint8)
return img_noised

# Read an Image
image = cv2.imread("img.jpg")
guass_img = add_gaussian_noise(image)
show(guass_img)
# Direct way
guass = cv2.GaussianBlur(guass_img,(7,7),cv2.BORDER_DEFAULT)
show(guass)

keyboard_arrow_down Guassian Filter from scratch

# 1. Create a Gaussian kernel


def gaussian_kernel(size, sigma=1):
center = size // 2
kernel = np.array([[np.exp(-((x - center)**2 + (y - center)**2) / (2 * sigma**2))
for x in range(size)] for y in range(size)])
return kernel / np.sum(kernel)

# 2. Apply convolution without padding


def apply_convolution(image, kernel):
k_size = kernel.shape[0]
pad_size = k_size // 2
# Add padding to the image for the border pixels
padded_image = np.pad(image, pad_size, mode='constant')
height, width = image.shape
new_image = np.zeros((height, width))

for i in range(height):
for j in range(width):
# Extract the region corresponding to the kernel size
region = padded_image[i:i + k_size, j:j + k_size]
new_image[i, j] = np.sum(region * kernel)
return new_image

grayed = cv2.cvtColor(guass_img, cv2.COLOR_BGR2GRAY)


kernel = gaussian_kernel(3)
filtered_image = apply_convolution(grayed, kernel)

plt.imshow(grayed,cmap="gray")

<matplotlib.image.AxesImage at 0x1b3dc5e6990>

plt.imshow(filtered_image,cmap="gray")

<matplotlib.image.AxesImage at 0x1b3dc660f50>

keyboard_arrow_down Image Averaging


# Read 3 images
img1 = cv2.imread("img1.jpg")
img2 = cv2.imread("img2.jpg")
img3 = cv2.imread("img3.jpg")

# Resize all 3 images


print("Image shapes")
print(img1.shape)
print(img2.shape)
print(img3.shape)
#(166 ,194 , 3 )

img1 = cv2.resize(img2, (166, 194))


img3 = cv2.resize(img2, (166, 194))

Image shapes
(201, 251, 3)
(166, 194, 3)
(194, 259, 3)

accumulated_image = np.zeros((166, 194))

# Sum the corresponding pixels of each image


for i in range(166):
for j in range(194):
# print(img1[i, j])
pass

import numpy as np
import cv2

# List of image filenames


image_filenames = ['img1.jpg', 'img2.jpg'] # Add more filenames as needed

# Number of images
N = float(len(image_filenames))

# 1. Read the first image to get dimensions and initialize the output array
first_image = cv2.imread(image_filenames[0], cv2.IMREAD_GRAYSCALE)
height, width = first_image.shape
output = np.zeros((height, width), dtype=np.float32)

# 2. Loop through each image, read it, and add to the output
for image_filename in image_filenames:
# Read the image
image = cv2.imread(image_filename, cv2.IMREAD_GRAYSCALE)

# Ensure the image is the correct size


if image.shape != (height, width):
raise ValueError(f"Image {image_filename} does not have the correct dimensions.")

# Add the image to the output


output += image / N # Average it by dividing by N

# 3. Convert the output to an 8-bit format for saving


average_image = output.astype(np.uint8)

# 4. Save the output image


cv2.imwrite('averaged_image.tif', average_image)

# Optional: Display the output image


cv2.imshow("Averaged Image", average_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[62], line 22
20 # Ensure the image is the correct size
21 if image.shape != (height, width):
---> 22 raise ValueError(f"Image {image_filename} does not have the correct
dimensions.")
24 # Add the image to the output
25 output += image / N # Average it by dividing by N

ValueError: Image img2.jpg does not have the correct dimensions.

You might also like