0% found this document useful (0 votes)
6 views

Code

Uploaded by

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

Code

Uploaded by

asiimwerobert598
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

import cv2

import numpy as np

import glob

import matplotlib.pyplot as plt

from scipy.signal import convolve2d

from scipy.signal import wiener

def load_images():

images = []

for filename in glob.glob('./*.png'): # Assuming images are in JPEG format

img = cv2.imread(filename)

if img is not None:

images.append(img)

return images

def motion_blur_kernel(size, angle):

kernel = np.zeros((size, size), dtype=np.float32)

center = (size - 1) / 2

for i in range(size):

x = int(center + (i - center) * np.cos(angle))

y = int(center + (i - center) * np.sin(angle))

kernel[y, x] = 1

kernel /= np.sum(kernel)

return kernel

def apply_wiener_filter(image, kernel_size=3, noise_variance=.1):

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

kernel = motion_blur_kernel(kernel_size, np.pi / 2) # Example: 45 degree motion blur

convolved = convolve2d(gray_image, kernel, boundary='wrap', mode='same')


filtered = wiener(convolved, (kernel_size, kernel_size), noise_variance)

return cv2.cvtColor(np.uint8(filtered), cv2.COLOR_GRAY2BGR)

def sharpen_image(image, method='gaussian'):

if method == 'basic':

kernel = np.array([[ 0, -1, 0],

[-1, 5, -1],

[ 0, -1, 0]])

elif method == 'strong':

kernel = np.array([[-1, -1, -1],

[-1, 9, -1],

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

elif method == 'gaussian':

kernel = np.array([[-1, -1, -1, -1, -1],

[-1, 2, 2, 2, -1],

[-1, 2, 8, 2, -1],

[-1, 2, 2, 2, -1],

[-1, -1, -1, -1, -1]]) / 8.0

sharpened = cv2.filter2D(image, -1, kernel)

return sharpened

def unsharp_mask(image, kernel_size=(7, 7), sigma=5, amount=1.5, threshold=0):

blurred = cv2.GaussianBlur(image, kernel_size, sigma)

sharpened = float(amount + 1) * image - float(amount) * blurred

sharpened = np.maximum(sharpened, np.zeros(sharpened.shape))

sharpened = np.minimum(sharpened, 255 * np.ones(sharpened.shape))

sharpened = sharpened.round().astype(np.uint8)

if threshold > 0:
low_contrast_mask = np.absolute(image - blurred) < threshold

np.copyto(sharpened, image, where=low_contrast_mask)

return sharpened

def adjust_brightness_contrast(image, alpha=1.3, beta=-1):

adjusted = cv2.convertScaleAbs(image, alpha=alpha, beta=beta)

return adjusted

def correct_motion_artifacts(images, sharpening_method='basic'):

corrected_images = []

for img in images:

corrected_img = apply_wiener_filter(img)

if sharpening_method == 'unsharp':

sharpened_img = unsharp_mask(corrected_img)

else:

sharpened_img = sharpen_image(corrected_img, method=sharpening_method)

final_img = adjust_brightness_contrast(sharpened_img)

corrected_images.append(final_img)

return corrected_images

def plot_images(original_images, corrected_images):

n = len(original_images)

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

for i in range(n):

axes[i, 0].imshow(cv2.cvtColor(original_images[i], cv2.COLOR_BGR2RGB))

axes[i, 0].set_title(f'\n\nOriginal Image {i+1}')

axes[i, 0].axis('off')
axes[i, 1].imshow(cv2.cvtColor(corrected_images[i], cv2.COLOR_BGR2RGB))

axes[i, 1].set_title(f'\n\nCorrected Image {i+1}')

axes[i, 1].axis('off')

plt.tight_layout()

plt.show()

images = load_images()

if images:

corrected_images = correct_motion_artifacts(images, sharpening_method='strong')

plot_images(images, corrected_images)

print("Motion artifact correction completed.")

print("................................................................................")

else:

print("No images found.")

print("................................................................................")

You might also like