Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions computer_vision/gaussian_blur.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import math
import copy

Check failure on line 2 in computer_vision/gaussian_blur.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

computer_vision/gaussian_blur.py:1:1: I001 Import block is un-sorted or un-formatted

"""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):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file computer_vision/gaussian_blur.py, please provide doctest for the function gaussian_kernel

Please provide return type hint for the function: gaussian_kernel. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: size

Please provide type hint for the parameter: sigma

"""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):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file computer_vision/gaussian_blur.py, please provide doctest for the function apply_gaussian_blur

Please provide return type hint for the function: apply_gaussian_blur. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: image

Please provide type hint for the parameter: 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)
Loading