1
+ import cv2
2
+ import numpy as np
3
+ from scipy import ndimage
4
+
5
+ class FilterImage :
6
+ def gaussian_filter (self , image ):
7
+ if image is not None :
8
+ return cv2 .GaussianBlur (image , (15 , 15 ), 0 )
9
+ return image
10
+
11
+ def high_pass_filter (self , image ):
12
+ if image is not None :
13
+ kernel = np .array ([[- 1 , - 1 , - 1 ],
14
+ [- 1 , 8 , - 1 ],
15
+ [- 1 , - 1 , - 1 ]])
16
+ return cv2 .filter2D (image , - 1 , kernel )
17
+ return image
18
+
19
+ def histogramme_filter (self , image ):
20
+ if image is not None :
21
+ if len (image .shape ) == 3 :
22
+ gray_image = cv2 .cvtColor (image , cv2 .COLOR_BGR2GRAY )
23
+ return cv2 .equalizeHist (gray_image )
24
+ elif len (image .shape ) == 2 :
25
+ return cv2 .equalizeHist (image )
26
+ return image
27
+
28
+ def detect_contours (self , image ):
29
+ if image is not None :
30
+ gray = cv2 .cvtColor (image , cv2 .COLOR_BGR2GRAY )
31
+ blurred = cv2 .GaussianBlur (gray , (5 , 5 ), 0 )
32
+ edges = cv2 .Canny (blurred , 50 , 150 )
33
+ contours , _ = cv2 .findContours (edges , cv2 .RETR_EXTERNAL , cv2 .CHAIN_APPROX_SIMPLE )
34
+ cv2 .drawContours (image , contours , - 1 , (0 , 255 , 0 ), 2 )
35
+ return image
36
+ return image
37
+
38
+ def add_gaussian_noise (self , image , mean = 0 , var = 0.01 ):
39
+ if image is not None :
40
+ row , col , ch = image .shape
41
+ sigma = var ** 0.5
42
+ gauss = np .random .normal (mean , sigma , (row , col , ch ))
43
+ noisy = image + gauss .reshape (row , col , ch )
44
+ return np .clip (noisy , 0 , 255 ).astype (np .uint8 )
45
+ return image
46
+
47
+ def mean_filter (self , image , n = 3 , m = 3 ):
48
+ if image is not None :
49
+ kernel = np .ones ((n , m ), np .float32 ) / (n * m )
50
+ return cv2 .filter2D (image , - 1 , kernel )
51
+ return image
52
+
53
+ def low_pass_filter (self , image ):
54
+ if image is not None :
55
+ H = np .array ([[1 , 2 , 1 ], [2 , 4 , 2 ], [1 , 2 , 1 ]]) / 16
56
+ return cv2 .filter2D (image , - 1 , H )
57
+ return image
58
+
59
+ def min_max_smoothing (self , image , progress_callback = None ):
60
+ if len (image .shape ) == 3 :
61
+ result = np .zeros_like (image )
62
+ for i in range (3 ):
63
+ result [:,:,i ] = self ._min_max_smoothing_channel (image [:,:,i ], progress_callback )
64
+ return result
65
+ else :
66
+ return self ._min_max_smoothing_channel (image , progress_callback )
67
+
68
+ def _min_max_smoothing_channel (self , channel , progress_callback = None ):
69
+ rows , cols = channel .shape
70
+ result = np .zeros_like (channel )
71
+
72
+ for i in range (1 , rows - 1 ):
73
+ for j in range (1 , cols - 1 ):
74
+ neighborhood = channel [i - 1 :i + 2 , j - 1 :j + 2 ]
75
+ min_val = np .min (neighborhood )
76
+ max_val = np .max (neighborhood )
77
+ result [i , j ] = (min_val + max_val ) / 2
78
+
79
+ if progress_callback :
80
+ progress_callback (cols )
81
+
82
+ return result
83
+
84
+ def median_filter (self , image , size = 3 ):
85
+ if image is not None :
86
+ return cv2 .medianBlur (image , size )
87
+ return image
88
+
89
+
90
+ def hybrid_median_filter (self , image ):
91
+ if len (image .shape ) == 3 :
92
+ result = np .zeros_like (image )
93
+ for i in range (3 ):
94
+ result [:,:,i ] = self ._hybrid_median_channel (image [:,:,i ])
95
+ return result
96
+ else :
97
+ return self ._hybrid_median_channel (image )
98
+
99
+ def _hybrid_median_channel (self , channel ):
100
+ m1 = ndimage .median_filter (channel , footprint = np .array ([[0 ,1 ,0 ],[1 ,1 ,1 ],[0 ,1 ,0 ]]))
101
+ m2 = ndimage .median_filter (channel , footprint = np .array ([[1 ,0 ,1 ],[0 ,1 ,0 ],[1 ,0 ,1 ]]))
102
+ return np .median ([m1 , m2 , channel ], axis = 0 )
103
+
104
+ def morph_operation (self , image , operation , kernel_size = 3 , iterations = 1 ):
105
+ kernel = np .ones ((kernel_size , kernel_size ), np .uint8 )
106
+ if operation == 'dilate' :
107
+ return cv2 .dilate (image , kernel , iterations = iterations )
108
+ elif operation == 'erode' :
109
+ return cv2 .erode (image , kernel , iterations = iterations )
110
+ elif operation == 'open' :
111
+ return cv2 .morphologyEx (image , cv2 .MORPH_OPEN , kernel , iterations = iterations )
112
+ elif operation == 'close' :
113
+ return cv2 .morphologyEx (image , cv2 .MORPH_CLOSE , kernel , iterations = iterations )
114
+ else :
115
+ return image
0 commit comments