This repository was archived by the owner on Nov 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 212
Scripts to perform edge detection with Sobel filter #230
Merged
powerexploit
merged 7 commits into
powerexploit:master
from
Namyalg:Edge-detection-with-sobel-filter
Sep 8, 2020
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d8ee4cc
Scripts to perform sobel-edge detection added
97c588a
Updated README
2f69b98
Updated README
de622ff
Updated README
3dd34c0
Updated README
bbb3842
Update sobel-edge-detect.py
Namyalg 73bad93
Update sobel-edge-detect.py
Namyalg 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,27 @@ | ||
|
||
## Canny Edge-detection ## | ||
|
||
- This script implements the sobel filter for edge detection without the use of the openCV in-built functions. | ||
- The basic principle used here is convolution. | ||
- A kernel, a matrix of size(3X3 or 5X5) is often chosen and is convoluted over the image.(which is a matrix of pixels) | ||
- In RGB images there are 3 channels, while in grayscale there is only 1 channel. | ||
|
||
## Implementation ## | ||
|
||
- To run the scripts the user needs to run - | ||
pip install opencv | ||
|
||
- The path to the image needs to be mentioned | ||
- The image is converted to grayscale and then blurred | ||
- The sobel kernel in the X-direction is [-1,-2,-1,0,0,0,1,2,1] | ||
- The sobel kernel in the Y-direction is [-1,0,1,-2,0,2,-1,0,1] | ||
|
||
## Working ## | ||
|
||
This is the example image to begin with : | ||
|
||
 | ||
|
||
This is the final image after running the filter : | ||
|
||
 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 76 additions & 0 deletions
76
Image-Processing/Sobel-edge-detection/sobel-edge-detect.py
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,76 @@ | ||
import numpy as np | ||
import cv2 | ||
from google.colab.patches import cv2_imshow | ||
from math import sqrt, atan | ||
|
||
#This script can be used for edge-detection without the use of any inbuilt openCV libraries | ||
#Here the sobel filter is applied after blurring the image. | ||
|
||
kernelblur = np.array([1/16, 1/8, 1/16, 1/8, 1/4, 1/8, 1/16, 1/8, 1/16]).reshape(3, 3) | ||
kernelsobelX = np.array([-1, -2, -1, 0, 0, 0, 1, 2, 1]).reshape(3, 3) | ||
kernelsobelY = kernelsobelX.transpose() | ||
|
||
def convolute(img, kernel, height, width): | ||
pixels = [] | ||
|
||
#pixels are extracted from the image converted to grayscale | ||
for i in range(height): | ||
for j in range(width): | ||
pixels.append(img[i, j]) | ||
|
||
#The pixels array is resized in accordance with the size of the image | ||
pixels = np.array(pixels).reshape(height, width) | ||
|
||
#To handle the edge cases, sentinel values are used | ||
#The pixels array is bound by zeros on all edges | ||
|
||
# 00000000 | ||
# 0PIXELS0 | ||
# 00000000 | ||
#This is done to ensure that the kernel is applied to all the pixels | ||
#Sentinel values to ensure the edges arent missed out | ||
#Along the rows and columns | ||
pixels = np.insert(pixels, [0, height], np.zeros(len(pixels[0])), axis = 0) | ||
pixels = np.insert(pixels, [0, width], np.zeros((len(pixels[:, 0]), 1)), axis = 1) | ||
|
||
#Convolution is applied here | ||
apply_filter = [] | ||
for i in range(1, height): | ||
for j in range(1, width): | ||
temp = pixels[i:i+3, j:j+3] | ||
product = np.multiply(temp, kernel) | ||
apply_filter.append(sum(sum(product))) | ||
|
||
#The pixels are converted back to the image | ||
apply_filter = np.array(apply_filter).reshape(height-1, width-1) | ||
return(apply_filter) | ||
|
||
def sobel(img): | ||
height = img.shape[0] | ||
width = img.shape[1] | ||
|
||
#The image is blurred, so the noise is removed | ||
blur = convolute(img, kernelblur, height, width) | ||
|
||
height = height - 1 | ||
width = width - 1 | ||
|
||
#The sobel filter is applied in the X and Y direction separately | ||
convoluted_Y = convolute(blur, kernelsobelX, height, width) | ||
convoluted_X = convolute(blur, kernelsobelY, height, width) | ||
|
||
#Every pixel in convoluted_X can be called gx, in convoluted_Y can be called gy, | ||
#The resultant will be sqrt(pow(gx**2) + pow(gy**2)) | ||
resultant = [] | ||
for i in range(height - 1): | ||
for j in range(width - 1): | ||
in_x = pow(convoluted_X[i, j], 2) | ||
in_y = pow(convoluted_Y[i, j], 2) | ||
gradient = sqrt(in_x + in_y) | ||
reusultant.append(grad) | ||
resultant = np.array(resultant).reshape(height-1, width-1) | ||
cv2_imshow(resultant) | ||
|
||
if __name__ == "__main__": | ||
img = cv2.imread("/path to image", 0) | ||
sobel(img) |
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.
Space around ==