File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import math
2+ import copy
3+ '''Mean thresholding algorithm for image processing
4+ [More info on Wikipedia](https://en.wikipedia.org/wiki/Thresholding_(image_processing))
5+ '''
6+ # Imagen de ejemplo: matriz 5x5
7+ image = [
8+ [10 , 20 , 30 , 40 , 50 ],
9+ [20 , 30 , 40 , 50 , 60 ],
10+ [30 , 40 , 50 , 60 , 70 ],
11+ [40 , 50 , 60 , 70 , 80 ],
12+ [50 , 60 , 70 , 80 , 90 ]
13+ ]
14+
15+ def gaussian_kernel (size , sigma = 1 ):
16+ """Genera un kernel gaussiano de tamaño 'size' y desviación 'sigma'"""
17+ kernel = [[0 ]* size for _ in range (size )]
18+ center = size // 2
19+ s = 2 * sigma * sigma
20+ sum_val = 0
21+
22+ for i in range (size ):
23+ for j in range (size ):
24+ x , y = i - center , j - center
25+ kernel [i ][j ] = math .exp (- (x * x + y * y )/ s )
26+ sum_val += kernel [i ][j ]
27+
28+ # Normalizar
29+ for i in range (size ):
30+ for j in range (size ):
31+ kernel [i ][j ] /= sum_val
32+
33+ return kernel
34+
35+ def apply_gaussian_blur (image , kernel ):
36+ """Aplica el blur gaussiano a una imagen"""
37+ height = len (image )
38+ width = len (image [0 ])
39+ k_size = len (kernel )
40+ k_center = k_size // 2
41+ new_image = copy .deepcopy (image )
42+
43+ for i in range (height ):
44+ for j in range (width ):
45+ val = 0
46+ for ki in range (k_size ):
47+ for kj in range (k_size ):
48+ ni = i + ki - k_center
49+ nj = j + kj - k_center
50+ if 0 <= ni < height and 0 <= nj < width :
51+ val += image [ni ][nj ] * kernel [ki ][kj ]
52+ new_image [i ][j ] = int (val )
53+ return new_image
54+
55+ kernel = gaussian_kernel (3 , sigma = 1 )
56+ blurred_image = apply_gaussian_blur (image , kernel )
57+
58+ for row in blurred_image :
59+ print (row )
You can’t perform that action at this time.
0 commit comments