-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageProcessor_class.py
More file actions
175 lines (140 loc) · 5.59 KB
/
ImageProcessor_class.py
File metadata and controls
175 lines (140 loc) · 5.59 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import cv2
import numpy as np
class ImageProcessor:
"""Class for handling image processing operations"""
@staticmethod
def add_noise(image, intensity=0.05):
"""Add random noise to image"""
row, col, ch = image.shape
mean = 0
sigma = intensity * 255
gauss = np.random.normal(mean, sigma, (row, col, ch))
gauss = gauss.reshape(row, col, ch)
noisy = image + gauss
return np.clip(noisy, 0, 255).astype(np.uint8)
@staticmethod
def remove_noise(image, ksize=5):
"""Remove noise using Non-Local Means Denoising"""
return cv2.fastNlMeansDenoisingColored(image, None, 10, 10, 7, 21)
@staticmethod
def mean_filter(image, ksize=5):
"""Apply mean filter"""
return cv2.blur(image, (ksize, ksize))
@staticmethod
def median_filter(image, ksize=5):
"""Apply median filter"""
return cv2.medianBlur(image, ksize)
@staticmethod
def gaussian_filter(image, ksize=5):
"""Apply Gaussian filter"""
return cv2.GaussianBlur(image, (ksize, ksize), 0)
@staticmethod
def gaussian_noise(image, mean=0, sigma=25):
"""Add Gaussian noise to image"""
row, col, ch = image.shape
gauss = np.random.normal(mean, sigma, (row, col, ch))
gauss = gauss.reshape(row, col, ch)
noisy = image + gauss
return np.clip(noisy, 0, 255).astype(np.uint8)
@staticmethod
def erosion(image, ksize=5):
"""Apply erosion operation"""
# Convert to grayscale if it's not already
if len(image.shape) > 2:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
else:
gray = image.copy()
# Threshold to get binary image
_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# Create kernel and apply erosion
kernel = np.ones((ksize, ksize), np.uint8)
eroded = cv2.erode(binary, kernel, iterations=1)
# Convert back to BGR if original was color
if len(image.shape) > 2:
return cv2.cvtColor(eroded, cv2.COLOR_GRAY2BGR)
return eroded
@staticmethod
def dilation(image, ksize=5):
"""Apply dilation operation"""
# Convert to grayscale if it's not already
if len(image.shape) > 2:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
else:
gray = image.copy()
# Threshold to get binary image
_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# Create kernel and apply dilation
kernel = np.ones((ksize, ksize), np.uint8)
dilated = cv2.dilate(binary, kernel, iterations=1)
# Convert back to BGR if original was color
if len(image.shape) > 2:
return cv2.cvtColor(dilated, cv2.COLOR_GRAY2BGR)
return dilated
@staticmethod
def opening(image, ksize=5):
"""Apply opening operation (erosion followed by dilation)"""
if len(image.shape) > 2:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
else:
gray = image.copy()
# Threshold to get binary image
_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
kernel = np.ones((ksize, ksize), np.uint8)
opened = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)
if len(image.shape) > 2:
return cv2.cvtColor(opened, cv2.COLOR_GRAY2BGR)
return opened
@staticmethod
def closing(image, ksize=5):
"""Apply closing operation (dilation followed by erosion)"""
if len(image.shape) > 2:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
else:
gray = image.copy()
# Threshold to get binary image
_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
kernel = np.ones((ksize, ksize), np.uint8)
closed = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel)
if len(image.shape) > 2:
return cv2.cvtColor(closed, cv2.COLOR_GRAY2BGR)
return closed
@staticmethod
def boundary_extraction(image, ksize=5):
"""Extract boundaries from the image"""
if len(image.shape) > 2:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
else:
gray = image.copy()
# Threshold to get binary image
_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
kernel = np.ones((ksize, ksize), np.uint8)
# Boundary is original - eroded
eroded = cv2.erode(binary, kernel, iterations=1)
boundary = binary - eroded
if len(image.shape) > 2:
return cv2.cvtColor(boundary, cv2.COLOR_GRAY2BGR)
return boundary
@staticmethod
def region_filling(image, ksize=5):
"""Fill holes in the regions"""
if len(image.shape) > 2:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
else:
gray = image.copy()
# Threshold to get binary image
_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# Invert image for flood fill
inverted = cv2.bitwise_not(binary)
# Create a mask slightly larger than the image
h, w = binary.shape[:2]
mask = np.zeros((h + 2, w + 2), np.uint8)
# Flood fill from the borders
floodfill = inverted.copy()
cv2.floodFill(floodfill, mask, (0, 0), 255)
# Invert floodfilled image
floodfill_inv = cv2.bitwise_not(floodfill)
# Combine the original and floodfilled image
filled = binary | floodfill_inv
if len(image.shape) > 2:
return cv2.cvtColor(filled, cv2.COLOR_GRAY2BGR)
return filled