OpenCV is a bunch of stuff mainly dealing with processing images and videos on your computer. This is a standard library for Computer Vision for Python tasks. In this article, I will introduce you to a tutorial on OpenCV with Python.
What is OpenCV?
Perhaps this is the fundamental question that comes to mind. Well, that means “Open Source Computer Vision Library” launched by some avid coders in 1999 to incorporate image processing into a wide variety of coding languages. OpenCV is not limited to Python only, it also supports C and C++.
OpenCV with Python
My expertise is in Machine Learning and Python. So I will explain all the concepts of this computer vision library with context to Machine Learning and Python. So let’s get started with some fundamentals of OpenCV with python by looking at how it works with a practical example:
import cv2 cap = cv2.VideoCapture(0) while(True): ret, frame = cap.read() cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
The above script is to create a video capture, then insert it into a loop where the frames are played and displayed one by one with imshow, the conditional checks for the exit command, cap.release and then cv2.destroyAllWindows outside the loop then takes care of the final cleanup. The result should be a video stream with the default OpenCV user interface:
Colorspaces in OpenCV
There are several color spaces and transformations available, and the topic deserves its message. For now, let’s just turn the previous example into another color space:
import cv2 cap = cv2.VideoCapture(0) while(True): ret, frame = cap.read() # Change colorspace: gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) cv2.imshow('frame', gray) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
Transformations in OpenCV with Python
You’ll find that most of the examples use a cascade (incremental steps) of transformations and operations in the main loop, so for example, here’s a more complex set:
import cv2 cap = cv2.VideoCapture(0) while(True): ret, frame = cap.read() # --------CASCADE--------- # Convert to Greyscale gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Denoise (also try bluring, this is expensive ) denoised = cv2.fastNlMeansDenoising(gray, h=3, templateWindowSize=7, searchWindowSize=21) # Apply threshold thresholded = cv2.adaptiveThreshold(denoised, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 115, 1) # Mirror image mirrored = cv2.flip(thresholded, 1) # --------***--------- cv2.imshow('frame', mirrored) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
User Interface Using OpenCV with Python
OpenCV also provides very simple User Interface tools that you can use to create prototypes, here is an example:
OpenCV with Python for Machine Learning
Beyond basic image and video manipulation, OpenCV is a popular method for machine learning and computer vision in python, once again there is a lot to offer, like the detection of objects:
I hope you liked this article on Computer vision with Python. Feel free to ask your valuable questions in the comments section below. You can also follow me on Medium to learn every topic of Machine Learning and Python.