-
Notifications
You must be signed in to change notification settings - Fork 0
/
boolean_function.py
146 lines (131 loc) · 5.11 KB
/
boolean_function.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from statistics import variance
import tkinter as tk
import tkinter.filedialog as fd
import cv2
import numpy as np
from scipy.signal import convolve2d
boolean_functions_matrices = [
np.array([[0, 1, 1],
[0, 1, 1],
[0, 1, 1]]),
np.array([[0, 0, 0],
[1, 1, 1],
[1, 1, 1]]),
np.array([[1, 1, 0],
[1, 1, 0],
[1, 1, 0]]),
np.array([[1, 1, 1],
[1, 1, 1],
[0, 0, 0]]),
np.array([[1, 1, 1],
[0, 1, 1],
[0, 0, 1]]),
np.array([[1, 0, 0],
[1, 1, 0],
[1, 1, 1]]),
np.array([[0, 0, 1],
[0, 1, 1],
[1, 1, 1]]),
np.array([[1, 1, 1],
[1, 1, 0],
[1, 0, 0]]),
np.array([[0, 1, 1],
[0, 1, 1],
[0, 0, 1]]),
np.array([[1, 0, 0],
[1, 1, 0],
[1, 1, 0]]),
np.array([[0, 0, 0],
[0, 1, 1],
[1, 1, 1]]),
np.array([[1, 1, 1],
[1, 1, 0],
[0, 0, 0]]),
np.array([[1, 1, 1],
[0, 1, 1],
[0, 0, 0]]),
np.array([[0, 0, 0],
[1, 1, 0],
[1, 1, 1]]),
np.array([[0, 0, 1],
[0, 1, 1],
[0, 1, 1]]),
np.array([[1, 1, 0],
[1, 1, 0],
[1, 0, 0]]),
]
def perform_boolean_functions(image, window_size=3, c=0):
new_img = np.zeros(image.shape, dtype=np.uint8)
for i in range(1,image.shape[0]):
for j in range(1,image.shape[1]):
# Calculate the local window
local_window = image[i-window_size//2:i+window_size//2 + 1, j-window_size//2:j+window_size//2 + 1].copy()
# Calculate the mean and standard deviation
threshold = np.mean(local_window) - c
# Compare local window value with threshold
local_window[local_window > threshold] = 255
local_window[local_window <= threshold] = 0
# Check if local window is equal to a boolean function
local_window //= 255
for boolean_function in boolean_functions_matrices:
if np.all(local_window == boolean_function):
new_img[i, j] = 255
break
return new_img
def calculate_variance(image, window_size=3):
new_img = np.zeros(image.shape, dtype=np.uint8)
for i in range(1,image.shape[0]):
for j in range(1,image.shape[1]):
# Calculate the local window
local_window = image[i-window_size//2:i+window_size//2 + 1, j-window_size//2:j+window_size//2 + 1].copy()
variance_local_window = np.var(local_window)
# Calculate the mean and standard deviation
new_img[i, j] = variance_local_window
return new_img
def threshold(img, threshold):
new_img = np.zeros(img.shape, dtype=np.uint8)
new_img[img > threshold] = 255
return new_img
def boolean_function_algorithm(img, window_size=3, c=0, global_threshold=0):
global boolean_function_image, variance_image, final_image
boolean_function_image = perform_boolean_functions(img, window_size=window_size, c = c)
variance_image = calculate_variance(img, window_size=3)
global_threshold = int(global_threshold * np.max(variance_image) / 500)
global_thresholded_image = threshold(variance_image, global_threshold)
final_image = boolean_function_image & global_thresholded_image
final_image = cv2.putText(final_image, f'C: {c} T: {global_threshold}', (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
# Show results
cv2.imshow('Boolean Function', boolean_function_image)
cv2.imshow('Global Threshold', global_thresholded_image)
cv2.imshow('Final Image', final_image)
def on_c_trackbar(val):
c = cv2.getTrackbarPos('C', 'Trackbars')
thresh = cv2.getTrackbarPos('T', 'Trackbars')
boolean_function_algorithm(img, window_size=3, c=c, global_threshold=thresh)
def on_thresh_trackbar(val):
global img, variance_image, final_image
c = cv2.getTrackbarPos('C', 'Trackbars')
variance_image = calculate_variance(img, window_size=3)
thresh = int(cv2.getTrackbarPos('T', 'Trackbars') * np.max(variance_image) / 500)
global_thresholded_image = threshold(variance_image, thresh)
final_image = boolean_function_image & global_thresholded_image
final_image = cv2.putText(final_image, f'C: {c} T: {thresh}', (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
cv2.imshow('Global Threshold', global_thresholded_image)
cv2.imshow('Final Image', final_image)
IMAGE_PATH = '../../test-images/lena-noisy.jpeg'
IMAGE_NAME = IMAGE_PATH.split('/')[-1]
img = cv2.imread(IMAGE_PATH, 0)
boolean_function_image = None
variance_image = None
final_image = None
cv2.imshow('Image', img)
cv2.namedWindow('Trackbars')
cv2.resizeWindow('Trackbars', 640, 240)
cv2.createTrackbar("C", "Trackbars", 0, 255, on_c_trackbar)
cv2.createTrackbar("T", "Trackbars", 0, 500, on_thresh_trackbar)
on_c_trackbar(1)
on_thresh_trackbar(1)
cv2.waitKey(0)
cv2.destroyAllWindows()
# # Save image
cv2.imwrite('results/' + IMAGE_NAME, final_image)