-
-
Notifications
You must be signed in to change notification settings - Fork 360
Convolutions of Images 2d Python implementation #819
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
Merged
leios
merged 13 commits into
algorithm-archivists:master
from
rzalawad:convolutions_2d_in_python
Aug 24, 2021
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
aa16911
Added python code for convolutions 2d. Modified 2d.md file to include…
rzalawad bc3583f
Updated list comprehension code to numpy code. Changed if statements …
rzalawad 2c99b9f
fixed minor errors
rzalawad 2180a0d
solved minor errors
rzalawad 31e4c19
Changed circle size. Removed test values for x and y
rzalawad f8082c7
Update contents/convolutions/2d/2d.md
rzalawad 9f8f60a
Update contents/convolutions/2d/2d.md
rzalawad da087f8
added name to contributors.md
rzalawad 8659d17
Merge branch 'convolutions_2d_in_python' of github.com:Ridham177/algo…
rzalawad 5718536
resolved conflict
rzalawad c1ad3c0
removed name from contributors.md. will add next commit
rzalawad 023c5b9
added name to contributors.md
rzalawad 17cf36d
added name
rzalawad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import numpy as np | ||
from contextlib import suppress | ||
|
||
|
||
def convolve_linear(signal, filter, output_size): | ||
out = np.zeros(output_size) | ||
sum = 0 | ||
|
||
for i in range(output_size[0]): | ||
for j in range(output_size[1]): | ||
for k in range(max(0, i-filter.shape[0]), i+1): | ||
for l in range(max(0, j-filter.shape[1]), j+1): | ||
with suppress(IndexError): | ||
sum += signal[k, l] * filter[i-k, j-l] | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
out[i, j] = sum | ||
sum = 0 | ||
|
||
return out | ||
|
||
|
||
def create_gaussian_kernel(kernel_size): | ||
kernel = np.zeros((kernel_size, kernel_size)) | ||
|
||
# The center must be offset by 0.5 to find the correct index | ||
center = kernel_size*0.5 + 0.5 | ||
|
||
sigma = np.sqrt(0.1*kernel_size) | ||
|
||
def kernel_function(x, y): | ||
return np.exp(-((x-center+1)**2 + (y-center+1)**2)/(2*sigma**2)) | ||
|
||
kernel = np.fromfunction(kernel_function, (kernel_size, kernel_size)) | ||
return kernel / np.linalg.norm(kernel) | ||
|
||
|
||
def create_sobel_operators(): | ||
Sx = np.dot([[1.0], [2.0], [1.0]], [[-1.0, 0.0, 1.0]]) / 9 | ||
Sy = np.dot([[-1.0], [0.0], [1.0]], [[1.0, 2.0, 1.0]]) / 9 | ||
|
||
return Sx, Sy | ||
|
||
def sum_matrix_dimensions(mat1, mat2): | ||
return (mat1.shape[0] + mat2.shape[0], | ||
mat1.shape[1] + mat2.shape[1]) | ||
|
||
def compute_sobel(signal): | ||
Sx, Sy = create_sobel_operators() | ||
|
||
Gx = convolve_linear(signal, Sx, sum_matrix_dimensions(signal, Sx)) | ||
Gy = convolve_linear(signal, Sy, sum_matrix_dimensions(signal, Sy)) | ||
|
||
return np.sqrt(np.power(Gx, 2) + np.power(Gy, 2)) | ||
|
||
|
||
def create_circle(image_resolution, grid_extents, radius): | ||
out = np.zeros((image_resolution, image_resolution)) | ||
|
||
for i in range(image_resolution): | ||
x_position = ((i * grid_extents / image_resolution) | ||
- 0.5 * grid_extents) | ||
for j in range(image_resolution): | ||
y_position = ((j * grid_extents / image_resolution) | ||
- 0.5 * grid_extents) | ||
if x_position ** 2 + y_position ** 2 <= radius ** 2: | ||
out[i, j] = 1.0 | ||
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. There should probably be another way to do that one, but that not really that necessary to implement a clearer way. |
||
|
||
return out | ||
|
||
|
||
def main(): | ||
|
||
# Random distribution in x | ||
x = np.random.rand(100, 100) | ||
|
||
# Gaussian signals | ||
def create_gaussian_signals(i, j): | ||
return np.exp(-(((i-50)/100) ** 2 + | ||
((j-50)/100) ** 2) / .01) | ||
y = np.fromfunction(create_gaussian_signals, (100, 100)) | ||
|
||
# Normalization is not strictly necessary, but good practice | ||
x /= np.linalg.norm(x) | ||
y /= np.linalg.norm(y) | ||
|
||
# full convolution, output will be the size of x + y | ||
full_linear_output = convolve_linear(x, y, sum_matrix_dimensions(x, y)) | ||
|
||
# simple boundaries | ||
simple_linear_output = convolve_linear(x, y, x.shape) | ||
|
||
np.savetxt("full_linear.dat", full_linear_output) | ||
np.savetxt("simple_linear.dat", simple_linear_output) | ||
|
||
# creating simple circle and 2 different Gaussian kernels | ||
circle = create_circle(50, 2, 0.5) | ||
|
||
circle = circle / np.linalg.norm(circle) | ||
|
||
small_kernel = create_gaussian_kernel(3) | ||
large_kernel = create_gaussian_kernel(25) | ||
|
||
small_kernel_output = convolve_linear(circle, small_kernel, | ||
sum_matrix_dimensions(circle, | ||
small_kernel)) | ||
|
||
large_kernel_output = convolve_linear(circle, large_kernel, | ||
sum_matrix_dimensions(circle, | ||
large_kernel)) | ||
|
||
np.savetxt("small_kernel.dat", small_kernel_output) | ||
np.savetxt("large_kernel.dat", large_kernel_output) | ||
|
||
circle = create_circle(50, 2, 0.5) | ||
|
||
# Normalization | ||
circle = circle / np.linalg.norm(circle) | ||
|
||
# using the circle for sobel operations as well | ||
sobel_output = compute_sobel(circle) | ||
|
||
np.savetxt("sobel_output.dat", sobel_output) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I hate the fact that there are four indices and ranges of maximums, but I don't think there is any other way to do that and not be confusing.