-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleProcessors.py
More file actions
103 lines (83 loc) · 3.31 KB
/
ExampleProcessors.py
File metadata and controls
103 lines (83 loc) · 3.31 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
from SimpleFramework.SimpleFrameworkImageApplier import ImageProcessor
import cv2
import numpy as np
class PaddingProcessor(ImageProcessor):
def name(self):
return "Add Padding"
def step_names(self):
return ["Padding 20px"]
def apply1(self, images):
result = {}
for k, img in images.items():
result[f"{k}"] = img
padded = cv2.copyMakeBorder(img, 20, 20, 20, 20,
cv2.BORDER_CONSTANT,
value=[255, 255, 255])
result[f"{k} + Padded"] = padded
return result
class GaussianBlurProcessor(ImageProcessor):
def name(self):
return "Gaussian Blur"
def step_names(self):
return ["Apply 5x5 Kernel"]
def apply1(self, images):
result = {}
for k, img in images.items():
blurred = cv2.GaussianBlur(img, (5, 5), sigmaX=0)
result[f"{k} + Blurred"] = blurred
return result
class AddNoiseProcessor(ImageProcessor):
def name(self):
return "Add Gaussian Noise"
def step_names(self):
return ["Mean=0, Var=0.01"]
def apply1(self, images):
result = {}
for k, img in images.items():
# normalize image and add noise
img_float = img / 255.0
noise = np.random.normal(0, 0.1, img_float.shape)
noisy = np.clip(img_float + noise, 0, 1) * 255
result[f"{k} + Noise"] = noisy.astype(np.uint8)
return result
class RotateProcessor(ImageProcessor):
def name(self):
return "Rotate Image"
def step_names(self):
return ["Rotate 45°"]
def apply1(self, images):
result = {}
for k, img in images.items():
(h, w) = img.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, 45, 1.0)
rotated = cv2.warpAffine(img, M, (w, h))
result[f"{k} + Rotated"] = rotated
return result
class CartoonEffectProcessor(ImageProcessor):
def name(self):
return "Cartoon Effect"
def step_names(self):
return ["Edge-Preserved Filter + Color Quantization"]
def apply1(self, images):
result = {}
for k, img in images.items():
# Convert to gray and apply median blur
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
blurred = cv2.medianBlur(gray, 7)
# Detect edges
edges = cv2.adaptiveThreshold(blurred, 255,
cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY, blockSize=9, C=2)
# Color quantization
data = np.float32(img).reshape((-1, 3))
criteria = (cv2.TERM_CRITERIA_EPS +
cv2.TERM_CRITERIA_MAX_ITER, 20, 0.001)
_, labels, palette = cv2.kmeans(
data, 8, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
quant = palette[labels.flatten()].reshape(
img.shape).astype(np.uint8)
# Combine edges and quantized
cartoon = cv2.bitwise_and(quant, quant, mask=edges)
result[f"{k} + Cartoon"] = cartoon
return result