@@ -35,13 +35,26 @@ def detect_contours(self, image):
35
35
return image
36
36
return image
37
37
38
- def add_gaussian_noise (self , image , mean = 0 , var = 0.01 ):
38
+ def add_gaussian_noise (self , image , progress_callback = None ):
39
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 )
40
+ if len (image .shape ) == 3 : # Color image
41
+ row , col , ch = image .shape
42
+ sigma = np .sqrt (0.01 )
43
+ gauss = np .random .normal (0 , sigma , (row , col , ch ))
44
+ noisy = image + gauss .reshape (row , col , ch ) * 255
45
+ elif len (image .shape ) == 2 : # Grayscale image
46
+ row , col = image .shape
47
+ sigma = np .sqrt (0.01 )
48
+ gauss = np .random .normal (0 , sigma , (row , col ))
49
+ noisy = image + gauss * 255
50
+
51
+ # Clip the values to be between 0 and 255
52
+ noisy = np .clip (noisy , 0 , 255 ).astype (np .uint8 )
53
+
54
+ if progress_callback :
55
+ progress_callback (row * col )
56
+
57
+ return noisy
45
58
return image
46
59
47
60
def mean_filter (self , image , n = 3 , m = 3 ):
0 commit comments