Python

Examples of OpenCV Library in Python

Programmingempire

This article provides an introduction to OpenCV Library in Python and demonstrates some examples of using this library.

In fact, OpenCV (Open Source Computer Vision) is a cross-platform library. Also, it works on both images and videos. In the case of the Python programming language, the OpenCV library is available with the cv2 package. The cv2 package contains methods to read and display images among other image processing methods. In order to use computer vision functions, we need to import the cv2 package.

Although OpenCV has many applications in image processing, computer vision, and machine learning, the most prominent application of the OpenCV library is CCTV footage analysis for surveillance.

The following list shows some Examples of OpenCV Library in Python

The following code shows a simple program to read and display an image.

import cv2
myimage=cv2.imread('tree.jpg', cv2.IMREAD_COLOR)
cv2.imshow('Tree Image', myimage)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

Image Displayed with OpenCV Library in Python
Image Displayed with OpenCV Library in Python

imread(imagepath, flags)

As shown above, there are four methods of cv2 package used in the code. In order to read an image, we use the imread() method. This method has the following parameters.

  • The first parameter imagepath indicates the path of the image file from which the image is to be loaded.
  • While the second parameter flags represent the read modes. For instance, IMREAD_COLOR reads a colored image. Whereas a value of 0 for flags load a grey image as shown below.
myimage=cv2.imread('tree.jpg', 0)
Reading a grey image
Reading a grey image

imshow(image_title, image)

In order to show the image, we use imshow() method. This method takes the following parameters.

  • Basically, image_title represents the title of the image window.
  • The second parameter image represents the image instance that will be shown.

waitKey(time_interval)

Once, we display an image, we need a method to close it. Basically, the waitKey() method allows the image to appear for the specified time. Whenever we use a value of 0, the image will appear for an infinite time until we press a key. Otherwise, it will display the image for a specified number of milliseconds. For instance, the following code will display the image for seven seconds.

cv2.waitKey(7000)

destroyAllWindows()

In order to close all windows which were open using OpenCV, we use this method.

Other Useful Methods of OpenCV Library in Python

Although OpenCV library contains a large number of methods, some important ones are given below.

rectangle(imagename, start, end, color, width)

Generally, we use the rectangle() function to draw a rectangle over the image. However, we can also use to mask a specific part of the image. The following code shows both of these examples.

import cv2
myimage=cv2.imread('tree.jpg')
cv2.rectangle(myimage, (10, 10), (320, 350), (255,255,0),10)
cv2.imshow('Rectange Over Tree Image', myimage)

cv2.rectangle(myimage, (100, 100), (190, 200), (34,54,160),-1)
cv2.imshow('Masking the Image', myimage)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

Using rectangle() function of OpenCV
Using rectangle() function of OpenCV

The rectangle() function takes five parameters. In the first place, we specify the image instance. After that, the start, and end coordinates of the rectangle should be given. Further, we specify the color in Blue-Green-Red (BGR) format. Finally, we specify the thickness of the rectangle. In case a value of -1 is given for thickness, a filled rectangle will be created. Therefore, we can mask a part of the image.

circle(imagename, center, radius, color, width)

Likewise, we can use the circle() function to draw a circle over the image. Firstly, we specify the name of image instance. After that, the next two parameters are the x and y coordinates of the center of circle and its radius respectively. The fourth parameter is the color in BGR format. Finally, the thickness of the circle should be given or -1 for a filled circle. The following code demonstrates the circle() function.

import cv2
myimage=cv2.imread('tree.jpg')
cv2.circle(myimage, (170, 170), 160, (6, 73, 218), 4)
cv2.imshow('Circle Over Tree Image', myimage)

cv2.circle(myimage, (140, 140), 36, (6, 233, 100), -1)
cv2.imshow('Masking the Image', myimage)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

Using circle() Function of OpenCV
Using circle() Function of OpenCV

line(imagename, start, end, color, thickness)

As an illustration, we draw a line over an image by using the line() function. The following code shows an example of drawing two lines over an image. As can be seen, we need to pass the name of the image instance, start, and end coordinates, color in BGR format, and the thickness of the line as parameters.

import cv2
myimage=cv2.imread('tree.jpg')
cv2.line(myimage, (0, 0), (340, 375), (180, 36, 218), 6)
cv2.line(myimage, (330, 0), (0, 368), (180, 36, 218), 6)
cv2.imshow('Line Over Tree Image', myimage)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

Using line() Function of OpenCV
Using line() Function of OpenCV

Further Reading

Deep Learning Tutorial

Text Summarization Techniques

How to Implement Inheritance in Python

Find Prime Numbers in Given Range in Python

Running Instructions in an Interactive Interpreter in Python

Deep Learning Practice Exercise

Python Practice Exercise

Deep Learning Methods for Object Detection

Understanding YOLO Algorithm

What is Image Segmentation?

ImageNet and its Applications

Image Contrast Enhancement using Histogram Equalization

Transfer Learning and its Applications

Examples of OpenCV Library in Python

Examples of Tuples in Python

Python List Practice Exercise

Understanding Blockchain Concepts

Edge Detection Using OpenCV

Predicting with Time Series

Example of Multi-layer Perceptron Classifier in Python

Measuring Performance of Classification using Confusion Matrix

Artificial Neural Network (ANN) Model using Scikit-Learn

Popular Machine Learning Algorithms for Prediction

Long Short Term Memory – An Artificial Recurrent Neural Network Architecture

Python Project Ideas for Undergraduate Students

Creating Basic Charts using Plotly

Visualizing Regression Models with lmplot() and residplot() in Seaborn

Data Visualization with Pandas

A Brief Introduction of Pandas Library in Python

A Brief Tutorial on NumPy in Python

programmingempire

You may also like...