0% found this document useful (0 votes)
3 views1 page

Code

This document discusses image denoising using non-local means denoising in OpenCV. It generates a reference image with added Gaussian noise to create a noisy image. Then it applies non-local means denoising and calculates the PSNR and SSIM metrics to evaluate the denoised image quality.

Uploaded by

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

Code

This document discusses image denoising using non-local means denoising in OpenCV. It generates a reference image with added Gaussian noise to create a noisy image. Then it applies non-local means denoising and calculates the PSNR and SSIM metrics to evaluate the denoised image quality.

Uploaded by

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

import numpy as np

import cv2
from skimage.metrics import peak_signal_noise_ratio as psnr
from skimage.metrics import structural_similarity as ssim

# Generate random images (simulated for demonstration purposes)


np.random.seed(0) # For reproducibility
image_size = (256, 256) # Example size

# Create a reference image (grayscale gradient)


reference_image = np.linspace(0, 255, num=image_size[0] * image_size[1],
dtype=np.uint8).reshape(image_size)
reference_image = cv2.GaussianBlur(reference_image, (5, 5), 0) # Apply a Gaussian
blur to make it more realistic

# Create a noisy image by adding random noise to the reference image


# Adjust noise level to achieve PSNR ~ 42 dB
noise_std = 5 # Standard deviation of noise; lower values increase PSNR
noise = np.random.normal(0, noise_std, image_size).astype(np.uint8) # Gaussian
noise
noisy_image = cv2.add(reference_image, noise)

# Image Denoising using Non-Local Means Denoising (cv2.fastNlMeansDenoising)


denoised_image = cv2.fastNlMeansDenoising(noisy_image, None, h=15,
templateWindowSize=7, searchWindowSize=21)

# Ensure denoised image is in the proper format for PSNR and SSIM calculations
denoised_image = np.clip(denoised_image, 0, 255).astype(np.uint8)

# Calculate PSNR
psnr_value = psnr(reference_image, denoised_image)

# Calculate SSIM
ssim_value, _ = ssim(reference_image, denoised_image, full=True)

# Display results
print(f'PSNR: {psnr_value:.2f} dB')
print(f'SSIM: {ssim_value:.4f}')

You might also like