What will we cover in this tutorial?
How to convert a webcam stream into a black and white line drawing using OpenCV and Python. Also, how to adjust the parameters while running the live stream.
See result here.
The things you need to use
There are two things you need to use in order to get a good line drawing of your image.
- GaussianBlur to smooth out the image, as detecting lines is sensitive to noise.
- Canny that detects the lines.
The Gaussian blur is advised to use a 5×5 filter. The Canny then has to threshold parameters. To find the optimal values for your setting, we have inserted two trackbars where you can set them to any value as see the results.
You can read more about Canny Edge Detection here.
If you need to install OpenCV please read this tutorial.
The code is given below.
import cv2 import numpy as np # Setup camera cap = cv2.VideoCapture(0) # Set a smaller resolution cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) def nothing(x): pass canny = "Canny" cv2.namedWindow(canny) cv2.createTrackbar('Threshold 1', canny, 0, 255, nothing) cv2.createTrackbar('Threshold 2', canny, 0, 255, nothing) while True: # Capture frame-by-frame _, frame = cap.read() frame = cv2.flip(frame, 1) t1 = cv2.getTrackbarPos('Threshold 1', canny) t2 = cv2.getTrackbarPos('Threshold 2', canny) gb = cv2.GaussianBlur(frame, (5, 5), 0) can = cv2.Canny(gb, t1, t2) cv2.imshow(canny, can) frame[np.where(can)] = 255 cv2.imshow('WebCam', frame) if cv2.waitKey(1) == ord('q'): break # When everything done, release the capture cap.release() cv2.destroyAllWindows()
Do you know what the 5 key success factors every programmer must have?
How is it possible that some people become programmer so fast?
While others struggle for years and still fail.
Not only do they learn python 10 times faster they solve complex problems with ease.
What separates them from the rest?
I identified these 5 success factors that every programmer must have to succeed:
- Collaboration: sharing your work with others and receiving help with any questions or challenges you may have.
- Networking: the ability to connect with the right people and leverage their knowledge, experience, and resources.
- Support: receive feedback on your work and ask questions without feeling intimidated or judged.
- Accountability: stay motivated and accountable to your learning goals by surrounding yourself with others who are also committed to learning Python.
- Feedback from the instructor: receiving feedback and support from an instructor with years of experience in the field.
I know how important these success factors are for growth and progress in mastering Python.
That is why I want to make them available to anyone struggling to learn or who just wants to improve faster.
With the Python Circle community, you can take advantage of 5 key success factors every programmer must have.
Be part of something bigger and join the Python Circle community.