- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleProcessors.py
More file actions
Latest commit
103 lines (83 loc) · 3.31 KB
/
Copy pathExampleProcessors.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
fromSimpleFramework.SimpleFrameworkImageApplierimportImageProcessor
importcv2
importnumpyasnp
classPaddingProcessor(ImageProcessor):
defname(self):
return"Add Padding"
defstep_names(self):
return ["Padding 20px"]
defapply1(self, images):
result= {}
fork, imginimages.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
returnresult
classGaussianBlurProcessor(ImageProcessor):
defname(self):
return"Gaussian Blur"
defstep_names(self):
return ["Apply 5x5 Kernel"]
defapply1(self, images):
result= {}
fork, imginimages.items():
blurred=cv2.GaussianBlur(img, (5, 5), sigmaX=0)
result[f"{k} + Blurred"] =blurred
returnresult
classAddNoiseProcessor(ImageProcessor):
defname(self):
return"Add Gaussian Noise"
defstep_names(self):
return ["Mean=0, Var=0.01"]
defapply1(self, images):
result= {}
fork, imginimages.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)
returnresult
classRotateProcessor(ImageProcessor):
defname(self):
return"Rotate Image"
defstep_names(self):
return ["Rotate 45°"]
defapply1(self, images):
result= {}
fork, imginimages.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
returnresult
classCartoonEffectProcessor(ImageProcessor):
defname(self):
return"Cartoon Effect"
defstep_names(self):
return ["Edge-Preserved Filter + Color Quantization"]
defapply1(self, images):
result= {}
fork, imginimages.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
returnresult