A3_1

.py

School

Arizona State University *

*We aren’t endorsed by this school

Course

509

Subject

Electrical Engineering

Date

Dec 6, 2023

Type

py

Pages

3

Uploaded by SuperHumanOstrich3671 on coursehero.com

import numpy as np import cv2 from skimage.metrics import structural_similarity as ssim from skimage.metrics import mean_squared_error, peak_signal_noise_ratio import matplotlib.pyplot as plt import random # Step 1: Load the original Nadia image nadia_original = cv2.imread('nadia_orig.png', cv2.IMREAD_GRAYSCALE) # Step 2: Create images with impulse noise and structural distortions def add_impulse_noise(image, noise_ratio): output = np.zeros(image.shape, np.uint8) thres = 1 - noise_ratio for i in range(image.shape[0]): for j in range(image.shape[1]): rdn = random.random() if rdn > thres: output[i][j] = 255 else: output[i][j] = image[i][j] return output # noisy_image = image.copy() # total_pixels = image.size # num_noisy_pixels = int(noise_ratio * total_pixels) # indices = np.random.choice(total_pixels, num_noisy_pixels, replace=False) # noisy_image.flat[indices] = 255 # return noisy_image def introduce_structural_distortions(image): distorted_image = image.copy() left = 70 top = 150 right = 330 bottom = 320 roi = distorted_image[top:bottom, left:right] scaled_section = cv2.resize(roi, (distorted_image.shape[1], int((distorted_image.shape[1] / roi.shape[1]) * roi.shape[0])), interpolation=cv2.INTER_AREA) scaled_height, scaled_width = scaled_section.shape[:2] distorted_image[top - 50:top - 50 + scaled_height, 0:distorted_image.shape[1]] = scaled_section left_n = 120 top_n = top - 50 + scaled_height right_n = 320 bottom_n = top - 50 + scaled_height + 150 scaling_factor_zoom_out = 0.8 roi = distorted_image[top_n:bottom_n, left_n:right_n] zoomed_out_section = cv2.resize(roi, None, fx=scaling_factor_zoom_out, fy=scaling_factor_zoom_out, interpolation=cv2.INTER_AREA) zoomed_out_height, zoomed_out_width = zoomed_out_section.shape[:2] distorted_image[top_n:top_n + zoomed_out_height, left_n:left_n + zoomed_out_width] = zoomed_out_section return distorted_image
noise_ratio = 0.395 # Adjust as needed impulse_noisy_nadia = add_impulse_noise(nadia_original, noise_ratio) structurally_distorted_nadia = introduce_structural_distortions(nadia_original) # Add very tiny amount of noise tiny_noise_ratio = 0.00028 # Adjust as needed tiny_noisy_nadia = add_impulse_noise(nadia_original, tiny_noise_ratio) # Step 3: Calculate SSIM and MSE def calculate_metrics(original, distorted): ssim_score = ssim(original, distorted) mse_score = mean_squared_error(original, distorted) return ssim_score, mse_score # Calculate SSIM & MSE ssim_impulse_noisy, mse_impulse_noisy = calculate_metrics(nadia_original, impulse_noisy_nadia) ssim_structurally_distorted, mse_structurally_distorted = calculate_metrics(nadia_original, structurally_distorted_nadia) ssim_tiny_noisy, mse_tiny_noisy = calculate_metrics(nadia_original, tiny_noisy_nadia) # Calculate PSNR psnr_impulse_noisy = peak_signal_noise_ratio(nadia_original, impulse_noisy_nadia) psnr_structurally_distorted = peak_signal_noise_ratio(nadia_original, structurally_distorted_nadia) psnr_tiny_noisy = peak_signal_noise_ratio(nadia_original, tiny_noisy_nadia) # Display the images with metrics plt.figure(figsize=(12, 8)) # Original Nadia Image plt.subplot(141) plt.imshow(nadia_original, cmap='gray') plt.title('Original Nadia Image') plt.axis('off') # Impulse Noisy Nadia Image plt.subplot(142) plt.imshow(impulse_noisy_nadia, cmap='gray') plt.title( f'Impulse Noisy Nadia\nMSE: {mse_impulse_noisy:.4f}, \nPSNR: {psnr_impulse_noisy:.4f}, \nSSIM: {ssim_impulse_noisy:.4f}') plt.axis('off') # Structurally Distorted Nadia Image plt.subplot(143) plt.imshow(structurally_distorted_nadia, cmap='gray') plt.title( f'Structurally Distorted Nadia\nMSE: {mse_structurally_distorted:.4f}, \nPSNR: {psnr_structurally_distorted:.4f}, \nSSIM: {ssim_structurally_distorted:.4f}') plt.axis('off') # Tiny Noisy Nadia Image plt.subplot(144) plt.imshow(tiny_noisy_nadia, cmap='gray')
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help