From 266d889f3002ad445ae57961af436e3a47e8c53a Mon Sep 17 00:00:00 2001 From: LuisMelendez Date: Thu, 2 Oct 2025 20:33:10 -0600 Subject: [PATCH 1/2] Create gaussian_blur.py --- computer_vision/gaussian_blur.py | 59 ++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 computer_vision/gaussian_blur.py diff --git a/computer_vision/gaussian_blur.py b/computer_vision/gaussian_blur.py new file mode 100644 index 000000000000..81e2f5f9b12c --- /dev/null +++ b/computer_vision/gaussian_blur.py @@ -0,0 +1,59 @@ +import math +import copy +'''Mean thresholding algorithm for image processing +[More info on Wikipedia](https://en.wikipedia.org/wiki/Thresholding_(image_processing)) +''' +# Imagen de ejemplo: matriz 5x5 +image = [ + [10, 20, 30, 40, 50], + [20, 30, 40, 50, 60], + [30, 40, 50, 60, 70], + [40, 50, 60, 70, 80], + [50, 60, 70, 80, 90] +] + +def gaussian_kernel(size, sigma=1): + """Genera un kernel gaussiano de tamaño 'size' y desviación 'sigma'""" + kernel = [[0]*size for _ in range(size)] + center = size // 2 + s = 2 * sigma * sigma + sum_val = 0 + + for i in range(size): + for j in range(size): + x, y = i - center, j - center + kernel[i][j] = math.exp(-(x*x + y*y)/s) + sum_val += kernel[i][j] + + # Normalizar + for i in range(size): + for j in range(size): + kernel[i][j] /= sum_val + + return kernel + +def apply_gaussian_blur(image, kernel): + """Aplica el blur gaussiano a una imagen""" + height = len(image) + width = len(image[0]) + k_size = len(kernel) + k_center = k_size // 2 + new_image = copy.deepcopy(image) + + for i in range(height): + for j in range(width): + val = 0 + for ki in range(k_size): + for kj in range(k_size): + ni = i + ki - k_center + nj = j + kj - k_center + if 0 <= ni < height and 0 <= nj < width: + val += image[ni][nj] * kernel[ki][kj] + new_image[i][j] = int(val) + return new_image + +kernel = gaussian_kernel(3, sigma=1) +blurred_image = apply_gaussian_blur(image, kernel) + +for row in blurred_image: + print(row) From ed18f851db3e8af02812688f4520adad0e081b91 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 3 Oct 2025 02:35:43 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- computer_vision/gaussian_blur.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/computer_vision/gaussian_blur.py b/computer_vision/gaussian_blur.py index 81e2f5f9b12c..4d30c71b2e48 100644 --- a/computer_vision/gaussian_blur.py +++ b/computer_vision/gaussian_blur.py @@ -1,20 +1,22 @@ import math import copy -'''Mean thresholding algorithm for image processing + +"""Mean thresholding algorithm for image processing [More info on Wikipedia](https://en.wikipedia.org/wiki/Thresholding_(image_processing)) -''' +""" # Imagen de ejemplo: matriz 5x5 image = [ [10, 20, 30, 40, 50], [20, 30, 40, 50, 60], [30, 40, 50, 60, 70], [40, 50, 60, 70, 80], - [50, 60, 70, 80, 90] + [50, 60, 70, 80, 90], ] + def gaussian_kernel(size, sigma=1): """Genera un kernel gaussiano de tamaño 'size' y desviación 'sigma'""" - kernel = [[0]*size for _ in range(size)] + kernel = [[0] * size for _ in range(size)] center = size // 2 s = 2 * sigma * sigma sum_val = 0 @@ -22,7 +24,7 @@ def gaussian_kernel(size, sigma=1): for i in range(size): for j in range(size): x, y = i - center, j - center - kernel[i][j] = math.exp(-(x*x + y*y)/s) + kernel[i][j] = math.exp(-(x * x + y * y) / s) sum_val += kernel[i][j] # Normalizar @@ -32,6 +34,7 @@ def gaussian_kernel(size, sigma=1): return kernel + def apply_gaussian_blur(image, kernel): """Aplica el blur gaussiano a una imagen""" height = len(image) @@ -52,6 +55,7 @@ def apply_gaussian_blur(image, kernel): new_image[i][j] = int(val) return new_image + kernel = gaussian_kernel(3, sigma=1) blurred_image = apply_gaussian_blur(image, kernel)