Make your sketch using OpenCV in Python— easy project
In today’s very short blog we will see how we can make our sketch using OpenCV in the simplest way possible. This is going to be a very fun project for beginners. So without any further due.
Read the full article with source code here — https://machinelearningprojects.net/sketch-using-opencv/
Let’s do it…
Code for making a sketch using OpenCV…
import numpy as np
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
blur_gray = cv2.GaussianBlur(gray,(5,5),900)
edges = cv2.Canny(blur_gray,45,90)
ret,thre = cv2.threshold(edges,70,255,cv2.THRESH_BINARY_INV)
cam = cv2.VideoCapture(0)
ret,frame = cam.read()
cv2.imshow('Live Sketch', sketch(frame))
- Line 1–2 — Importing required libraries.
- Line 4–9 — Defining the sketch function. We will use this function to implement sketches using OpenCV.
- Line 5 — Convert the image from BGR to gray.
- Line 6 — Gaussian Blur the image, to remove Gaussian Noise.
- Line 7 — Find edges in the image using Canny Edge Detection.
- Line 8 — Threshold the image, to convert it to a binary image (just 0 and 255 pixels).
- Line 9 — Return the thresholded image.
- Line 11 — Instantiate the camera using cv2.VideoCapture(0). Here 0 means that it will use a webcam.
- Line 13 — Let’s start the loop.
- Line 14 — Read the image/frame from the camera.
- Line 15 — Show the sketch image returned by the sketch function when the current frame is passed into it.
- Line 16–17 — If someone hits the ESC key, break the code.
- Line 18–20 — If someone hits ENTER key, save the sketch.
- Line 22–23 — Close the webcam and destroy all open windows.
Do let me know if there’s any query regarding sketch using OpenCV by contacting me on email or LinkedIn.
For further code explanation and source code visit here — https://machinelearningprojects.net/sketch-using-opencv/
So this is all for this blog folks, thanks for reading it and I hope you are taking something with you after reading this and till the next time 👋…
Read my previous post: IMMORTAL SNAKE GAME IN PYTHON USING OPENCV