-
-
Notifications
You must be signed in to change notification settings - Fork 48.6k
Create gaussian blur algorithm (hacktoberfest) #13120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Please provide return type hint for the function: Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
"""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) |
There was a problem hiding this comment.
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 functiongaussian_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