Overview of OpenCV.

Overview of OpenCV.

OpenCV is a huge open-source library for computer vision, machine learning, and image processing. It can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrated with various libraries, such as Numpy which is a highly optimized library for numerical operations, then whatever operations one can do in Numpy can be combined with OpenCV.

Install the library:

OpenCV can be used by many programming language. In Python, there is a library 'opencv-python' from which we can use cv2 module. To install the library:

$  pip install opencv-python

We need Numpy also for some practices. So, also install numpy:

$   pip install numpy

Lets take some example for better understanding.

1. Create the image by using numpy.

# import cv2
# import numpy


Python uses image data in the form of a Numpy array, i.e. [Height, Width, Channel] format. Every Pixel is an array. So by using array, we can create (not click or capture) a image.

No alt text provided for this image
numpy.zeros((500,500,3),numpy.uint8)  

numpy.zeros() function create the array of given size having all the element zero(0). Now apply the crop operation and assign different color combination values (0-255, 0-255,0-255). Now using the cv2 module, we can open the image.

cv2.imshow('image',img5)      # open the image with window name 'image'
cv2.waitKey(0)                # wait for any key to stop the process
cv2.destroyAllWindows()       # close the window

If we want to save the image in our memory, then imwrite() function will perform this action.

2. Collapse two images.

Here , I have three images of different pixel or size. Lets collapse these images and create something new.

As we know, image is an array in form of Numpy having some rows and column. So we can iperform many operation using numpy function.

cv2.imread('path/of/image')   # to open the image 


No alt text provided for this image

There is an image of size(457,500,3) as shown in the image. Now in numpy, there are functions vtsack() and hstack() by which we can add two arrays vertically and horizontally.

No alt text provided for this image
numpy.hstack((arr1,arr2))

3. Swap a portion of image with other:

cv2.cvtColor(img , color)

cvtColor() convert the color of the image.

No alt text provided for this image

Now , we can swap a portion of the image with other image. In numpy, there is a function copy() that copy the entire array. After copying, we have two same images. So we can take dimension and then swap that portion using slicing in the array.

We can slice the rows and column and swap them.

No alt text provided for this image

This is the fun activity to learn cv2 easily.

Thank you for reading.

To view or add a comment, sign in

More articles by Lalit Yadav

  • K-Means Clustering and UseCases in Security Domain.

    K means is one of the most popular Unsupervised Machine Learning Algorithms Used for Solving Classification Problems. K…

  • Automatic Number Plate Recognition Using CNN

    Automatic Number Plate Recognition (ANPR) is a highly accurate system capable of reading vehicle number plates without…

  • Use Cases of JavaScript

    JavaScript is a text-based programming language used both on the client-side and server-side that allows you to make…

  • Face Recognition Using OpenCV.

    In this article, I am going to discuss about the face recognition using openCV. Here, I am using LBPH model for face…

  • Confusion Matrix & Cyber Security

    Confusion matrix is a fairly common term when it comes to machine learning. Today I would be trying to relate the…

  • GUI Applications in Docker Container

    Docker is an open source containerization platform. It enables developers to package applications into…

  • Machine Learning Model in docker container

    In this article, we are going to discuss about the installing procedure of the docker, start the docker service, how to…

  • Google Cloud Platform(GCP)

    Recently,I attended a workshop on the GCP organised under the metorship of Mr. Vimal Daga sir at LinuxWorld Pvt.

  • INTEGRATION OF DOCKER WITH ANSIBLE

    Ansible: Now lets starts with ansible.What is ansible and what is the us of it ? Ansible is an open-source automation…

  • Flutter Audio And VideoPlayer

    Task : 1st (create an app in which there are some audio and video files present in assests,local and on internet). Lets…

Insights from the community

Others also viewed

Explore topics