Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Joiesmary authored Sep 1, 2022
1 parent 5364cb2 commit d3261cb
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,105 @@ program 12:


**
27) Implement a program to perform various edge detection techniques
a) Canny Edge detection
import cv2
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn')
loaded_image = cv2.imread("shape1.jpeg")
loaded_image = cv2.cvtColor(loaded_image,cv2.COLOR_BGR2RGB)
gray_image = cv2.cvtColor(loaded_image,cv2.COLOR_BGR2GRAY)
edged_image = cv2.Canny(gray_image, threshold1=30, threshold2=100)
plt.figure(figsize=(20,20))
plt.subplot(1,3,1)
plt.imshow(loaded_image, cmap="gray")
plt.title("original Image")
plt.axis("off")
plt.subplot(1,3,2)
plt.imshow(gray_image,cmap="gray")
plt.axis("off")
plt.title("GrayScale Image")
plt.subplot(1,3,3)
plt.imshow(edged_image, cmap="gray")
plt.axis("off")
plt.title("Canny Edge Detected Image")
plt.show()

![image](https://user-images.githubusercontent.com/19484531/187897964-f84a4108-f8ea-47c3-b22a-f3eea6deaec7.png)


b) Edge detection schemes - the gradient (Sobel - first order derivatives) based edge detector and the Laplacian (2nd order derivative, so it is extremely sensitive to noise) based edge detector.

import cv2
import numpy as np
import matplotlib.pyplot as plt
img0=cv2.imread("shape1.jpeg")
gray=cv2.cvtColor(img0,cv2.COLOR_BGR2GRAY)
img=cv2.GaussianBlur(gray,(3,3),0)
laplacian=cv2.Laplacian(img,cv2.CV_64F)
sobelx=cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5)
sobely=cv2.Sobel(img,cv2.CV_64F,0,1,ksize=5)

plt.subplot(2,2,1),plt.imshow(img,cmap='gray')
plt.title('Original'),plt.xticks([]),plt.yticks([])

plt.subplot(2,2,2),plt.imshow(laplacian,cmap='gray')
plt.title('Laplacian'),plt.xticks([]),plt.yticks([])

plt.subplot(2,2,3),plt.imshow(sobelx,cmap='gray')
plt.title('Sobel X'),plt.xticks([]),plt.yticks([])

plt.subplot(2,2,4),plt.imshow(sobely,cmap='gray')
plt.title('Sobel Y'),plt.xticks([]),plt.yticks([])
plt.show()
![image](https://user-images.githubusercontent.com/19484531/187898068-746c7880-ce68-4029-89b3-0c08867d454b.png)

c) Edge detection using Prewitt Operator
import cv2
import numpy as np
from matplotlib import pyplot as plt
imge=cv2.imread("shape.jpg")
gray=cv2.cvtColor(imge,cv2.COLOR_BGR2GRAY)
img_gaussian=cv2.GaussianBlur(gray,(3,3),0)

kernelx= np.array([[1,1,1], [0,0,0],[-1,-1,-1]])
kernely=np.array([[-1,0,1], [-1,0,1],[-1,0,1]])

img_prewittx = cv2.filter2D(img_gaussian, -1, kernelx)
img_prewitty = cv2.filter2D(img_gaussian, -1, kernely)

cv2.imshow("Original Image", img)
cv2.imshow("Prewitt x", img_prewittx)
cv2.imshow("Prewitt y", img_prewitty)
cv2.imshow("Prewitt", img_prewittx + img_prewitty)
cv2.waitKey()
![image](https://user-images.githubusercontent.com/19484531/187898258-a4f406cf-3e13-4721-b126-4d7de34ca3b3.png)


d) Roberts Edge Detection- Roberts cross operator


import cv2
import numpy as np
from scipy import ndimage
from matplotlib import pyplot as plt
roberts_cross_v=np.array([[1,0],[0,-1]])
roberts_cross_h=np.array([[0,1],[-1,0]])
img=cv2.imread("shape.jpg",0).astype('float64')
img/=255.0
vertical=ndimage.convolve(img,roberts_cross_v)
horizontal=ndimage.convolve(img,roberts_cross_h)

edged_img=np.sqrt(np.square(horizontal)+np.square(vertical))
edged_img*=255
cv2.imwrite("output.jpg",edged_img)
cv2.imshow("OutputImage",edged_img)
cv2.waitKey()
cv2.destroyAllWindows()
![image](https://user-images.githubusercontent.com/19484531/187898372-3192011c-e666-4666-a7b8-42ff18a6e85d.png)

0 comments on commit d3261cb

Please sign in to comment.