Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

CGIP Program Assignment (26)

The document outlines a programming assignment involving image processing techniques such as median filtering, histogram equalization, high pass filtering, power law transformation, and edge detection. Each section includes code implementations using libraries like OpenCV and Matplotlib, along with visual comparisons of the original and processed images. The assignment emphasizes practical applications of these techniques on grayscale images and includes commentary on the results of edge detection methods.

Uploaded by

Avenger Avengers
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

CGIP Program Assignment (26)

The document outlines a programming assignment involving image processing techniques such as median filtering, histogram equalization, high pass filtering, power law transformation, and edge detection. Each section includes code implementations using libraries like OpenCV and Matplotlib, along with visual comparisons of the original and processed images. The assignment emphasizes practical applications of these techniques on grayscale images and includes commentary on the results of edge detection methods.

Uploaded by

Avenger Avengers
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Assignment No: 2

Programming Assignment

Name: Avish V Kumar


Class: R6A Roll no: 26

1. Implement a median filtering. Size of filter should be a parameter.


a)Take an image corrupts them with a salt and paper noise.
b)Apply median filtering to each of corrupted images. For comparison purpose
apply averaging too. Try 2 different mask sizes for each case (5X5),(3X3).

Implementation

import cv2
import numpy as np
import random
import matplotlib.pyplot as plt
from skimage import data

def add_salt_and_pepper_noise(image, salt_prob, pepper_prob):


noisy_img = np.copy(image)
total_pixels = image.size

num_salt = int(total_pixels * salt_prob)


coords = [np.random.randint(0, i - 1, num_salt) for i in image.shape]
noisy_img[coords[0], coords[1]] = 255

num_pepper = int(total_pixels * pepper_prob)


coords = [np.random.randint(0, i - 1, num_pepper) for i in image.shape]
noisy_img[coords[0], coords[1]] = 0

return noisy_img

def apply_median_filter(image, kernel_size):


return cv2.medianBlur(image, kernel_size)

def apply_mean_filter(image, kernel_size):


return cv2.blur(image, (kernel_size, kernel_size))

def main():
image = data.camera()
noisy_image = add_salt_and_pepper_noise(image, 0.02, 0.02)

median_3x3 = apply_median_filter(noisy_image, 3)
median_5x5 = apply_median_filter(noisy_image, 5)

mean_3x3 = apply_mean_filter(noisy_image, 3)
mean_5x5 = apply_mean_filter(noisy_image, 5)

fig, axes = plt.subplots(2, 3, figsize=(12, 8))

axes[0, 0].imshow(image, cmap='gray')


axes[0, 0].set_title('Original Image')

axes[0, 1].imshow(median_3x3, cmap='gray')


axes[0, 1].set_title('Median Filter 3x3')

axes[0, 2].imshow(median_5x5, cmap='gray')


axes[0, 2].set_title('Median Filter 5x5')
axes[1, 0].imshow(noisy_image, cmap='gray')
axes[1, 0].set_title('Noisy Image')

axes[1, 1].imshow(mean_3x3, cmap='gray')


axes[1, 1].set_title('Mean Filter 3x3')

axes[1, 2].imshow(mean_5x5, cmap='gray')


axes[1, 2].set_title('Mean Filter 5x5')

for ax in axes.ravel():
ax.axis('off')
plt.tight_layout()
plt.show()

if __name__ == "__main__":
main()

Output
2. Write a program that perform histogram equalization of the input gray scale
image. Plot the histogram of original and histogram equalized.

Implementation

import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage import data

def histogram_equalization():

gray_image = data.camera()

equalized_image = cv2.equalizeHist(gray_image)

original_hist = cv2.calcHist([gray_image], [0], None, [256], [0, 256])


equalized_hist = cv2.calcHist([equalized_image], [0], None, [256], [0, 256])

fig, axes = plt.subplots(2, 2, figsize=(12, 8))

axes[0, 0].imshow(gray_image, cmap='gray')


axes[0, 0].set_title("Original Image")
axes[0, 0].axis("off")

axes[0, 1].imshow(equalized_image, cmap='gray')


axes[0, 1].set_title("Histogram Equalized Image")
axes[0, 1].axis("off")
axes[1, 0].plot(original_hist, color='black')
axes[1, 0].set_title("Original Histogram")

axes[1, 1].plot(equalized_hist, color='black')


axes[1, 1].set_title("Equalized Histogram")

plt.tight_layout()
plt.show()

histogram_equalization()

Output

3. Apply high pass filtering of input image using the mask h(m,n)=[1 1 1;1 -8
1;1 1 1].
Implementation

import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage import data

def high_pass_filtering():
gray_image = data.camera()

high_pass_kernel = np.array([[1, 1, 1],


[1, -8, 1],
[1, 1, 1]])

high_pass_image = cv2.filter2D(gray_image, -1, high_pass_kernel)

fig, axes = plt.subplots(1, 2, figsize=(10, 5))

axes[0].imshow(gray_image, cmap='gray')
axes[0].set_title("Original Image")
axes[0].axis("off")

axes[1].imshow(high_pass_image, cmap='gray')
axes[1].set_title("High-Pass Filtered Image")
axes[1].axis("off")

plt.tight_layout()
plt.show()
high_pass_filtering()

Output

4. Write a function called powerlaw that accept uint8 bits intensity image and
variable gamma and applies power law transformation to the input image.
Output image should be a uint8 intensity image observe the output image by
varying the value of gamma between 0.2 and 1.2 in the steps of 0.2.

Implementation

import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage import data
def powerlaw(image, gamma):

normalized_image = image / 255.0


transformed_image = np.power(normalized_image, gamma)
output_image = np.uint8(transformed_image * 255)

return output_image

def observe_powerlaw():
gray_image = data.camera().astype(np.uint8)

gamma_values = np.arange(0.2, 1.3, 0.2)

fig, axes = plt.subplots(2, 3, figsize=(12, 8))

for ax, gamma in zip(axes.flatten(), gamma_values):


transformed = powerlaw(gray_image, gamma)
ax.imshow(transformed, cmap='gray')
ax.set_title(f'Gamma = {gamma:.1f}')
ax.axis("off")

plt.tight_layout()
plt.show()

observe_powerlaw()
Output

5. Read an input image and compute the edges using edge detectors like Robert,
prewitt, sobel and canny and comment the result.

Implementation

import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage import data, filters

def edge_detection(image):
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) if len(image.shape) == 3
else image
roberts_edges = filters.roberts(gray_image)
prewitt_edges = filters.prewitt(gray_image)
sobel_edges = filters.sobel(gray_image)
canny_edges = cv2.Canny(gray_image, 100, 200)

fig, axes = plt.subplots(2, 3, figsize=(12, 8))


axes = axes.ravel()

axes[0].imshow(gray_image, cmap='gray')
axes[0].set_title("Original Image")
axes[0].axis("off")

axes[1].imshow(roberts_edges, cmap='gray')
axes[1].set_title("Roberts Edge Detection")
axes[1].axis("off")

axes[2].imshow(prewitt_edges, cmap='gray')
axes[2].set_title("Prewitt Edge Detection")
axes[2].axis("off")

axes[3].imshow(sobel_edges, cmap='gray')
axes[3].set_title("Sobel Edge Detection")
axes[3].axis("off")

axes[4].imshow(canny_edges, cmap='gray')
axes[4].set_title("Canny Edge Detection")
axes[4].axis("off")

fig.delaxes(axes[5])
plt.tight_layout()
plt.show()

print("Roberts Edge: Detects sharp changes but is sensitive to noise.")


print("Prewitt Edge: Similar to Sobel but less smooth, detects vertical and horizontal
edges.")
print("Sobel Edge: Smooths the image while detecting edges, better for noisy images.")
print("Canny Edge: Most effective, applies Gaussian filter and finds edges using gradient
intensity.")

test_image = data.camera()
edge_detection(test_image)

Output

You might also like