Skip to content

Instantly share code, notes, and snippets.

@rahit
Last active December 23, 2023 02:17
Show Gist options
  • Star 44 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save rahit/c078cabc0a48f2570028bff397a9e154 to your computer and use it in GitHub Desktop.
Save rahit/c078cabc0a48f2570028bff397a9e154 to your computer and use it in GitHub Desktop.
Canny, Prewitt and Sobel Edge detection using opencv
"""
edges.py: Canny, Prewitt and Sobel Edge detection using opencv
"""
__author__ = "K.M. Tahsin Hassan Rahit"
__email__ = "tahsin.rahit@gmail.com"
import cv2
import numpy as np
img = cv2.imread('messi5.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_gaussian = cv2.GaussianBlur(gray,(3,3),0)
#canny
img_canny = cv2.Canny(img,100,200)
#sobel
img_sobelx = cv2.Sobel(img_gaussian,cv2.CV_8U,1,0,ksize=5)
img_sobely = cv2.Sobel(img_gaussian,cv2.CV_8U,0,1,ksize=5)
img_sobel = img_sobelx + img_sobely
#prewitt
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("Canny", img_canny)
cv2.imshow("Sobel X", img_sobelx)
cv2.imshow("Sobel Y", img_sobely)
cv2.imshow("Sobel", img_sobel)
cv2.imshow("Prewitt X", img_prewittx)
cv2.imshow("Prewitt Y", img_prewitty)
cv2.imshow("Prewitt", img_prewittx + img_prewitty)
cv2.waitKey(0)
cv2.destroyAllWindows()
@shreyas-afk
Copy link

thanks for this short guide it helped me a lot 👍

@stefaneadna
Copy link

Obrigada. Isso me ajudou muito! Sucesso pra você!

@gregoriusjimmy
Copy link

thanks dude

@MuadDev
Copy link

MuadDev commented Jan 3, 2022

Thanks!

@yusf1013
Copy link

Thanks!

@sehrish777
Copy link

really appreciate, such well-written code.
I need to apply another filter to my images dataset. Can you please help me for writing a code for high-boost filtering, please?

@PColunga87
Copy link

Thanks a lot

@coderthatcantcode
Copy link

Thank you

@fahimanabila
Copy link

Thank you very much!

@Binqie
Copy link

Binqie commented Oct 18, 2023

Thanks. Helped me in university project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment