OpenCV is a cross-platform library for real-time computer vision. It was initially developed by Intel and later released as Open Source product.
I would try to explain the library and create a simple application. Further, I will try to extend the application as an object tracking device.
First thing first – I am using python, specifically python 2.7 and already installed it on my windows laptop. OpenCV requires numpy and matplotlib ( optional) libraries to be present in the python environment. Please make them available.
I have already downloaded the opencv-3.4.3-vc14_vc15.exe from the official website and as soon as I executed the exe file – it is extracting the files to a directory called opencv at the same location. I moved the opencv to root directory c.
The folder C:\opencv\build\python\2.7\x86 has cv2.pyd file, Please copy this to python root python27\Lib\site-packages folder. With this step installation of opencv2 to python is complete. This can be verified by import cv2 on python prompt.
Opencv documentation here has a quite extensive explanation to handle images. I would try to use one or two of them before diving deeper into the library.
Loading and Saving of Image
One pretty neat example is loading image – There is two version of this example, one without matplotlib and other with matplotlib.
Example 1 – This example is reading and Image and showing. The waitKey and destroyAllWindows are to close the Image windows on pressing esc (27) key. The Image path must be in forward (/) slashes or double backslashes (\\) – otherwise, it may throw an error.
Example 2 – This uses matpltlib to show the image using this library. matplotlob gives greater control over the image.
The Output of the two examples is the following.
Capture, Load and Save Video from Webcam
Capturing live stream from a video camera is most basic operation in the real-time computer vision applications.
cap = Cv2.VideoCapture(0) method encapsulates the camera attached to the computer. 0 is the default camera, it can be -1 or 1 depending on the secondary camera.
cap.read() method returns a captured frame, which can be saved in a file or some other operation like filtering, the transformation of the image can be done.
########################################################## #VideoCapture1.py - Show Video from a webcam. ########################################################## import numpy as np import cv2 as cv cap = cv.VideoCapture(0) while(True): # Capture frame-by-frame ret, frame = cap.read() # Our operations on the frame come here gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) # Display the resulting frame cv.imshow('frame',gray) if cv.waitKey(1) & 0xFF == ord('q'): break # When everything done, release the capture cap.release() cv.destroyAllWindows() ########################################################## #VideoCapture2.py - Play A video from a file. ########################################################## import numpy as np import cv2 as cv cap = cv.VideoCapture('C:/opencv/videos/output.avi') while(cap.isOpened()): ret, frame = cap.read() gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) cv.imshow('frame',gray) if cv.waitKey(1) & 0xFF == ord('q'): break cap.release() cv.destroyAllWindows() ########################################################## #VideoCapture3.py - Save the Video from Webcam. ########################################################## import numpy as np import cv2 as cv cap = cv.VideoCapture(0) # Define the codec and create VideoWriter object fourcc = cv.VideoWriter_fourcc('M','J','P','G') #fourcc = cv.VideoWriter_fourcc(*'XVID') out = cv.VideoWriter('C:/opencv/videos/output.avi',fourcc, 20.0, (640,480)) while(cap.isOpened()): ret, frame = cap.read() if ret==True: # Filp The Frame # frame = cv.flip(frame,0) # write the frame out.write(frame) cv.imshow('frame',frame) if cv.waitKey(1) & 0xFF == ord('q'): break else: break # Release everything if job is finished cap.release() out.release() cv.destroyAllWindows()
There are multiple application of capturing and loading images using OpenCV. We can use the image or video captured for face detection, face tracking, identification of abnormality in medical scans, marketing campaigning etc.