-
Notifications
You must be signed in to change notification settings - Fork 0
/
fast_core.py
104 lines (84 loc) · 3.5 KB
/
fast_core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import os
import time
from utils import (
load_image,
save_image,
calculate_psnr,
print_blue,
print_green,
print_red,
print_yellow,
print_purple,
)
from image_processing.kernels import kernel_average, kernel_gaussian
from image_processing.fast_richardson_lucy import FastRichardsonLucy
from scipy.signal import convolve2d
import numpy as np
class ImageProcessor:
def __init__(self, input_folder, output_folder):
self.input_folder = input_folder
self.output_folder = output_folder
def process_image(self, image_path, kernel_obj, iterations_list):
image = load_image(image_path)
filename = os.path.basename(image_path)
image_output_folder = os.path.join(
self.output_folder, os.path.splitext(filename)[0]
)
kernel_folder_name = str(kernel_obj)
kernel_output_folder = os.path.join(image_output_folder, kernel_folder_name)
if not os.path.exists(kernel_output_folder):
os.makedirs(kernel_output_folder)
# Blurring
print_red(f"Blurring image with {kernel_obj}")
blurred_image = np.zeros_like(image)
for channel in range(image.shape[2]):
blurred_image[:, :, channel] = convolve2d(image[:, :, channel], kernel_obj.kernel, mode='same', boundary='wrap')
blurred_image_path = os.path.join(kernel_output_folder, "blurred.png")
save_image(blurred_image, blurred_image_path)
# Unblurring with specified iterations
for iterations in iterations_list:
start_time = time.time()
print_purple(
f"Unblurring image with {kernel_obj} and {iterations} iterations"
)
rl = FastRichardsonLucy(image, kernel_obj.kernel, iterations)
unblurred_image = rl.apply()
# Calculate PSNR.
psnr_value = calculate_psnr(image, unblurred_image)
print_yellow(f"PSNR: {psnr_value:.2f} dB")
duration = time.time() - start_time
unblurred_image_path = os.path.join(
kernel_output_folder, f"unblurred_{iterations}-iter.png"
)
save_image(unblurred_image, unblurred_image_path)
print_green(f"Completed in: {duration:.2f} seconds")
print("")
def process_folder(self, kernels, iterations_list):
for filename in os.listdir(self.input_folder):
if filename.endswith((".png", ".jpg", ".jpeg", ".webp", ".gif")):
print_blue(
f"############### Processing image: {filename} ###############"
)
image_path = os.path.join(self.input_folder, filename)
image_output_folder = os.path.join(
self.output_folder, os.path.splitext(filename)[0]
)
if not os.path.exists(image_output_folder):
os.makedirs(image_output_folder)
for kernel in kernels:
self.process_image(image_path, kernel, iterations_list)
if __name__ == "__main__":
input_folder = "images/originals"
output_folder = "images/processed"
kernels = [
kernel_average(3),
kernel_average(5),
kernel_average(11),
kernel_gaussian(3, 1.0),
kernel_gaussian(5, 1.0),
kernel_gaussian(3, 2.0),
kernel_gaussian(5, 2.0),
]
iterations_list = [5, 10, 15]
processor = ImageProcessor(input_folder, output_folder)
processor.process_folder(kernels, iterations_list)