Last active
January 21, 2024 00:54
-
-
Save wsricardo/078183dc7892871ed867b49b02bfdfd4 to your computer and use it in GitHub Desktop.
Python OpenCV "Canny" Edge Detection Tests. https://wsricardo.blogspot.com
This file contains 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
""" | |
Created on Sat Jan 6 09:27:28 2024 | |
@author: wsricardo | |
Blog: https://wsricardo.blogspot.com | |
""" | |
from PIL import Image | |
import cv2 | |
import numpy as np | |
import random | |
def effect1( frame ): | |
# Edges | |
gray = cv2.cvtColor( frame , cv2.COLOR_BGR2GRAY ) | |
gray = cv2.medianBlur( gray, 5 ) | |
edges = cv2.adaptiveThreshold( | |
gray , | |
255, | |
cv2.ADAPTIVE_THRESH_MEAN_C, | |
cv2.THRESH_BINARY, 9, 9 | |
) | |
# Cartoonization | |
color =cv2.bilateralFilter( frame, 9, 250, 250 ) | |
cartoon = cv2.bitwise_and( color, color, mask=edges ) | |
return cartoon | |
def effect2( frame ): | |
""" | |
Find edges | |
""" | |
#mask = cv2.threshold(frame, | |
# 60, 255, 0) | |
out = cv2.Canny( frame , 100, 250 ) | |
return cv2.addWeighted( | |
cv2.convertScaleAbs( | |
cv2.bitwise_not( out ), | |
alpha= 1.8, beta=10 ), | |
5., out, 0, 2.0 ) | |
def effect3( frame ): | |
frame = cv2.GaussianBlur(frame, (3, 5), sigmaX=0.5, sigmaY=0.7) | |
frame = cv2.cvtColor( frame, cv2.COLOR_BGR2GRAY ) | |
#sobelx = cv2.Sobel( frame, ddepth=cv2.CV_64F, dx=1, dy=0, ksize=5 ) | |
#sobely = cv2.Sobel( frame, ddepth=cv2.CV_64F, dx=0, dy=1, ksize=5 ) | |
#frame = cv2.Canny( frame, 60, 130) | |
#frame = cv2.Sobel( frame, ddepth=cv2.CV_64F, dx=1, dy=1, ksize=5 ) | |
( thresh, frame ) = cv2.threshold( frame, 127, 255, cv2.THRESH_BINARY ) | |
#out = frame | |
#frame = cv2.addWeighted( frame, 5.0, frame, 0.1, 5.0 ) | |
return cv2.bitwise_not( frame ) | |
def effect4( frame ): | |
color = cv2.bilateralFilter( frame, 9, 250, 250 ) | |
frame = cv2.GaussianBlur(frame, (3, 5), sigmaX=0.5, sigmaY=0.7) | |
frame = cv2.cvtColor( frame, cv2.COLOR_BGR2GRAY ) | |
frame = cv2.Laplacian( frame , 5, cv2.CV_64F ) | |
frame = cv2.convertScaleAbs( frame, alpha=2.2, beta=10 ) | |
frame = cv2.addWeighted(frame, 5, frame, 0, 2.0) | |
mask = cv2.adaptiveThreshold( frame , 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 5, 5 ) | |
return cv2.bitwise_not( cv2.bitwise_and( frame, frame, mask=mask ) ) | |
def effectCartoon( frame ): | |
_ = None | |
color = cv2.bilateralFilter( frame, 9, 250, 250 ) | |
frame = cv2.GaussianBlur( frame, (3,5), sigmaX= 0.5, sigmaY=0.7 ) | |
gray = cv2.cvtColor( color, cv2.COLOR_BGR2GRAY ) | |
( ret, mask2 ) = cv2.threshold( gray, 80, 255, cv2.THRESH_BINARY ) | |
edges = cv2.Laplacian( gray, 5, cv2.CV_64F ) | |
edges = cv2.bitwise_not( edges ) | |
edges = cv2.convertScaleAbs( edges, alpha=2.8, beta=10 ) | |
edges = cv2.addWeighted( edges, 5, edges, 0, 2.0 ) | |
#edges = cv2.bitwise_not( edges ) | |
mask = cv2.adaptiveThreshold( edges , 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 5, 5) | |
_ = cv2.bitwise_and( frame, frame, mask=mask ) | |
return _ | |
v = cv2.VideoCapture(0) | |
#v = cv2.VideoCapture("Rainy City Night - Royalty Free Stock Footage.mp4") | |
frame_width = int( v.get( 3 ) ) | |
frame_height = int( v.get( 4 ) ) | |
#out = cv2.VideoWriter( 'rainny-walking-footage.avi', cv2.VideoWriter_fourcc( 'M', 'J', 'P', 'G' ), 10, ( frame_width, frame_height ) ) | |
while( True ): | |
ret, frame = v.read() | |
if ret == True: | |
frame_cartoon = effectCartoon( frame ) | |
#out.write( frame ) | |
#cv2.imshow( "Cam", frame ) | |
cv2.imshow( "Cam Cartoon ", | |
frame_cartoon ) | |
if cv2.waitKey( 1 ) & 0xFF == ord('q') : | |
break | |
else: | |
break | |
v.release() | |
#out.release() | |
cv2.destroyAllWindows() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment