Skip to content

Added Robert EdgeDetection code #84

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
20 changes: 17 additions & 3 deletions EdgeDetection/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,21 @@

Implementation of the following Edge Detection methods:

* Canny Edge Detection
* Robert Edge Detection
* Canny Edge Detection

* Robert Edge Detection :
The idea behind the Roberts operator is to approximate the gradient of an image through discrete differentiation which is achieved by computing the sum of the squares of the differences between diagonally adjacent pixels.
```
So, the Robert Mask kernels are :
Rx = [[ 1, 0],
0, -1]]
Ry = [[ 0, 1],
[-1, 0]]
```
The explaination can be found [here](https://en.wikipedia.org/wiki/Roberts_cross).

Original image | Robert Edge Detected
:-------------------------:|:-------------------------:
<img width="350" height="350" src="https://github.com/Bhumika-Kothwal/Image-Processing-OpenCV/blob/edge-detection/EdgeDetection/Robert_EdgeDetection_img.jpg">|<img width="350" height="350" src="https://github.com/Bhumika-Kothwal/Image-Processing-OpenCV/blob/edge-detection/EdgeDetection/Robert_EdgeDetection_output.jpg">
* Prewitt Edge Detection
* Sobel Edge Detection
* Sobel Edge Detection
59 changes: 59 additions & 0 deletions EdgeDetection/Robert_EdgeDetection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""
Robert Edge Detection
"""

import cv2 as cv
import numpy as np
import math

# function to compute convolution between kernel and original image
def compute_convolution(input_image, kernel):
width = input_image.shape[0]
height = input_image.shape[1]

# Middle of the kernel
offset = len(kernel) // 2

# Create empty output array
output_image = np.empty((width,height))
output_image.fill(0)

# Compute convolution between value and Robert kernels
for x in range(offset, width - offset):
for y in range(offset, height - offset):
for a in range(len(kernel)):
for b in range(len(Rx)):
xn = x + a - offset
yn = y + b - offset
value = input_image[xn][yn]
output_image[x][y] += value * kernel[a][b]
output_image[x][y] = math.sqrt(output_image[x][y]**2)
return output_image



#-----------------------Main Function----------------------#

# reading an image in grayscale
img_gray = cv.imread('Robert_EdgeDetection_img.jpg', 0)

# Robert Mask Operator
Rx = [[ 1, 0],
[ 0, -1,]]

Ry = [[ 0, 1],
[-1, 0]]

# computing image formed by convolution with kernel Rx
robert_x = compute_convolution(img_gray, Rx)
# computing image formed by convolution with kernel Ry
robert_y = compute_convolution(img_gray, Ry)

# getting the final edge detected image by combining the two convolutions using gradient formula
output_img = np.empty((robert_x.shape[0], robert_x.shape[1]))
for i in range(robert_x.shape[0]):
for j in range(robert_x.shape[1]):
output_img[i][j] = math.sqrt(robert_x[i][j]**2 + robert_y[i][j]**2)

#saving edge detected image
cv.imwrite("Robert_EdgeDetection_output.jpg", output_img)
Binary file added EdgeDetection/Robert_EdgeDetection_img.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added EdgeDetection/Robert_EdgeDetection_output.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.