Skip to content

Instantly share code, notes, and snippets.

@salmagro
Created January 19, 2023 17:36
Show Gist options
  • Save salmagro/6144788c38b3cad174473060bec4d1b2 to your computer and use it in GitHub Desktop.
Save salmagro/6144788c38b3cad174473060bec4d1b2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# coding: utf-8
# # CODE FOR CANNY EDGE DETECTION USING LIVE WEBCAM
#
# Live convolution: https://setosa.io/ev/image-kernels/
#
# Convolution arithmetics: https://github.com/vdumoulin/conv_arithmetic
#
# Kernels https://en.wikipedia.org/wiki/Kernel_(image_processing)
#
# # Import all the necessary libraries
# In[1]:
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import numpy as np
import cv2
# # Let's get Live Camera Canny
# In[2]:
def LiveCamEdgeDetection_canny(image_color):
threshold_1 = 30
threshold_2 = 80
image_gray = cv2.cvtColor(image_color, cv2.COLOR_BGR2GRAY)
canny = cv2.Canny(image_gray, threshold_1, threshold_2)
return canny
# # Let's get Live Camera Laplacian
# In[3]:
def LiveCamEdgeDetection_Laplace(image_color):
image_gray = cv2.cvtColor(image_color, cv2.COLOR_BGR2GRAY)
laplacian = cv2.Laplacian(image_gray, cv2.CV_64F)
return laplacian
# # Let's get Live Camera Sobel y
# In[4]:
def LiveCamEdgeDetection_sobely(image_color):
image_gray = cv2.cvtColor(image_color, cv2.COLOR_BGR2GRAY)
y_sobel = cv2.Sobel(image_gray, cv2.CV_64F, 1, 0, ksize = 7)
return y_sobel
# # Let's get Live Camera Sobel x
# In[5]:
def LiveCamEdgeDetection_sobelx(image_color):
image_gray = cv2.cvtColor(image_color, cv2.COLOR_BGR2GRAY)
x_sobel = cv2.Sobel(image_gray, cv2.CV_64F, 0, 1, ksize = 7)
return x_sobel
# In[6]:
def Cartoon(image_color):
dst = cv2.stylization(image_color, sigma_s=60, sigma_r=0.5)
return dst
# # Main calling function to initialize webcam and apply edge detection
# In[10]:
cap = cv2.VideoCapture(1)
cap.set(3,640) # adjust width
cap.set(4,480) # adjust height
while True:
ret, frame = cap.read() # Cap.read() returns a ret bool to indicate success.
#cv2.imshow('Live Edge Detection', LiveCamEdgeDetection_sobely(frame))
#cv2.imshow('Live Edge Detection', LiveCamEdgeDetection_sobelx(frame))
#cv2.imshow('Live Edge Detection', LiveCamEdgeDetection_canny(frame))
cv2.imshow('Live Edge Detection', Cartoon(frame))
cv2.imshow('Webcam Video', frame)
if cv2.waitKey(1) == 13: #13 Enter Key
break
cap.release() # camera release
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment