Introduction to OpenCV



OpenCV stands for Open Source Computer Vision .It is a library that is designed to solve computer vision problems ,image processing and other machine learning projects.

OpenCV is useful in identifying objects,text and faces .OpenCV is used along with numpy,keras and other libraries.

OpenCV has more than 47 thousand people of user community and estimated number of downloads exceeding 18 million. The library is used extensively in companies, research groups and by governmental bodies.

Along with well-established companies like Google, Yahoo, Microsoft, Intel, IBM, Sony, Honda, Toyota that employ the library, there are many startups such as Applied Minds, VideoSurf, and Zeitera, that make extensive use of OpenCV


Application:


Following are the applications of OpenCV: 


Face Recognition
Automated surveillance
Defect detection in manufacturing
Medical Diagnosis
Gesture Recognition
Motion Tracking

Installation:

Open CV can be installed using the following pip command

pip install opencv-python 


Working with images and videos:

OpenCV can be imported as import cv2


1.Reading images and videos:

a.Reading image:


import cv2

img=cv2.imread("lion.jpg")

cv2.imshow("Original",img)

cv2.waitKey() 



Statement 1 is used to import opencv 

Statement 2 imread is used to read image using image name or location of image

Statement 3 imshow is used to show image or video in a window called “Image”

Statement 4 waitkey is used to wait for a specific milliseconds to close window if 0 then window is closed after a keystroke


Note: When you read images greater than your monitor screen they go way off screen .

Currently OpenCV doesnt have any inbuilt method to process large images

b.Reading video:

Reading video is different from reading images


import cv2

cap=cv2.VideoCapture(0)

#Captures webacam of your desktop

while True:

    _,frame=cap.read()

    #capture frames from video stream

    cv2.imshow("Original",frame)

    if cv2.waitKey(20) & 0xFF==ord('f'):

        break

cap.release()

cv2.destroyAllWindows()



capture  is an instance of VideoCapture class .

VideoCapture can contain integer values or location of video.

Integer 0 depicts webcam,other number represents access to other cameras connected to the device.

By using capture.read() we grab frame by frame of video.isTrue checks whether frame is read successfully.

Later we show frame by frame in infinite while loop using imshow()

To close window ,it can be closed automatically after 20 milliseconds or by entering ‘f’

Later we release capture instance using release()

destroyAllWindows() closes the window memory.