Python is a high-level Computer language mainly known for its easy understandability. It allows the programmer to express their ideas in fewer lines of code without decreasing readability. It may use for Software Development, Image Processing, Machine Learning & many more. OpenCV is a library of Python used to solve theoretical problems in a program or software.

  1. OpenCV
  2. How it Works
  3. Facial Recognition Using OpenCV
  4. Basic Applications of OpenCV

OpenCV with Python

OpenCV

A video and image-processing library with binding in Python, OpenCV, was built by Intel and first released in 2000. First, practically used on a car named Stanley, the vehicle won DARPA Grand Challenge in 2005. OpenCV may use in every aspect of video and image analysis, like detection of a face and facial recognition, license plate reading of a car, photo editing, robotic vision, path planning, and a whole lot more. The OpenCV also supports many algorithms related to Computer Visualization and Machine Learning and is expanding day by day. This being an open-source library, developers contribute to the documentation, library, and tutorials actively to improve the library.

How it Works

OpenCV function works with NumPy, A highly enhanced library for numerical procedures with a MATLAB-style syntax. All the OpenCV array structures may convert to and from NumPy arrays. It also makes it easier to participate with other libraries that use NumPy such as Matplotlib and SciPy.

Installation in Windows

The best support of a Python developer is pip. To download and install any library, open cmd (Command Prompt) and write:

pip install python-opencv2
pip install NumPy
pip install matplotlib

After installation, make sure to import the libraries in the framework by using these commands:

Import python-opencv2
Import NumPy
Import matplotlib
Note: There are multiple versions of OpenCV. OpenCv2 is used for this tutorial.

Load Image with OpenCV

Import cv2
# colored Image 
Img = cv2.imread (“goat.jpg”,1)
# Black&White (gray-scale) 
Img_1 = cv2.imread (“goat.jpg”,0)

The first thing first is to make sure to import cv2, as seen in the above code.

  • The name of the picture is ‘goat’.
  • The image may read by using cv2.imread() function.
  • As commented in the code, the first picture with parameter ‘1’ is a color picture. Picture may import in a Color or Black and White mode depending on the parameter set by the programmer. Parameter ‘0’ indicates Black & White mode.

Display Image with OpenCV

Print / Display image using OpenCV is quite a simple process. The function used to display image is cv2.imshow().

cv2.imshow (“goat”, img)

The cv2.imshow() function may use to display the image by opening a window. There are two parameters for the cv2.imshow() function; first is the name of the window (goat), and second is the image (img) object used to display the type of picture. There are some conditions to this function.

  • An 8-bit unsigned image may present as it is.
  • If the image is a 16-bit unsigned or 32-bit integer, 256 divide the pixels. That is, the value range [0,255*256], which is [0,255].
  • To display an image that is more significant than the screen resolution, NamedWindow() function may use before cv2.imshow()

Some more essential functions of OpenCV are.

Print ()

The shape of the image may refer to the shape of NumPy array. The result would be consist of a matrix that consists of rows and columns.

Print (img.shape)

Resized_image ()

A function uses to resize an image to the desired shape. Users may input the new shape using parameters.

Note: Image object used earlier is changed to resized_image from this point onward. Make sure to use, resized_image while getting the output.
resized_image = cv2.resize(img, (650,500))
cv2.imshow(“Goat”, resized_image)

WaitKey ()

After waiting for an event, WaitKey makes the window static until the user performs any operation. The parameter passed to it is the time in milliseconds, which is ‘0’ in most cases.

DestroyAllWindow ()

It destroys all open windows. To destroy a specific window DestroyWindow(WinName) may use it.

A program of discussed function is below:

import cv2
Img = cv2.imread (“Goat.jpg”,0)
resized_image = cv2.resize(img, (1024,768))
cv2.imshow(“Penguins”, resized_image)
# Print (img.shape)
cv2.waitKey(0)
cv2.destroyAllWindows()

Facial Recognition Using OpenCV

The algorithm used for face recognition in cell phones and laptops is built using the same library. The critical activity required to proceed with this activity is as below.

  • First, create a Cascade Classifier with an image; it gives the features of a face.
  • OpenCV reads the image and the features file. At this point, there are NumPy arrays at the significant data arguments.
  • Now search for the row & column values of the face NumPy BArray, an array with the face rectangle coordinates.
  • The last step contains displaying the image with the rectangular face container.

CascadeClassifier

CascadeClassifier is an XML file that contains the features of the face. It extracts the features of the face, as discussed in the key points.

Rectangular Face Container

To define a rectangle, another pre-defined library may help that, known as cv2.rectangle (). In, cv2.rectangle a rectangle may create by passing parameters such as an object, box outline in RGB and the width of it. Sample Code of the following instance is below.

 

import cv2

# Create a CascadeClassifier Object
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml"

# Reading the image as it is
img = cv2.imread("goat.jpg")

# Reading the image as gray scale image
gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# Search the co-ordintes of the image
faces = face_cascade.detectMultiScale(gray_img, scaleFactor = 1.05,minNeighbors=5)

for x,y,w,h in faces:
    img = cv2.rectangle(img, (x,y), (x+w,y+h),(0,255,0),3)

resized = cv2.resize(img, (int(img.shape[1]/7),int(img.shape[0]/7)))
cv2.imshow("Gray", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

Basic Applications of OpenCV

There are Images and video analysis boiled down to streamlining the source as much as possible. It usually begins with a conversion to grayscale, but it can also be a color filter, gradient, or a combination of these. From here, we can do all sorts of analysis and transformations to the source—some examples of what we can do at a basic level.

  • Background Subtracting.
  • Edge Detections.
  • Colour Filtering.
  • Feature match for an Object.
  • Face Recognition.
  • Motion Detection.
  • Plotting of Motion Detection Graph.

OpenCV is not only bound to image processing, but it is also widely used for performance measurement, mathematical tools, and arithmetic operations over images. There are machine learning areas where  OpenCV is widely contributing its processing, and there is enhancement being done by the open-source community to take OpenCV to new dimensions.