Welcome to the OpenCV Guide Part-1

Shubham D
Shubham D
Jun 20 · 8 min read

Hello folks,

Hope you all are well, In the coming blogs I will try to help you to kick start your Open CV course. This is the first blog in the series and in this blog I have mainly focused on the foundation of the OpenCV, so we will answer the below mentioned question in this blog.

How to install OpenCV?

How to load and display the image?

Can we read and display the video in same way as image?

How to resize the image?

How to add shapes and text within the image?

How to crop the image?

How to rotate or shift the image?

Introduction:

OpenCV is stand for the Open-Source Computer Vision Library. It is python library use for solving the computer vision problems. By using OpenCV we can easily add the different images or sometimes we can crop the image or we can detect the edges within the image or we can use it for the face detection etc. Basically we can perform the computer vision problems very easily by using the OpenCV library.

Programming History of OpenCV:

As per the Wikipedia

OpenCV is written in C++ and its primary interface is in C++, but it still retains a less comprehensive though extensive older C interface. All of the new developments and algorithms appear in the C++ interface. There are bindings in Python, Java and MATLAB/OCTAVE.

QnA Series:

Ok now it is enough with the introduction stuff .Check the below mentioned Q&A series which will cover all your queries. Now without wasting time let’s get started.

1) How to install OpenCV?

In this module we need the Numpy as well as Matplotlib for the future use. so, we can install all this by executing the below mentioned command in the command prompt (Here I am assuming that python 3.0 is already installed in your system):

1) pip install numpy

2) pip install matplotlib

3) pip install opencv-contrib-python

If there is any issue while installation, please refer this link for the details

2) How to load and display the image?

First we have to import some important libraries as follow:

Now we will try to read the image by using the im.read and display the image by using the cv2_imshow (you can also use the cv.imshow if there is any issue as I am using the google Collab so prefer to use cv2_imshow).

Output for the above mentioned code is:

3) Can we read and display the video in same way as image?

Yes we can, but what is the difference between the images and video?

Actually, the simplest answer is that a photo is a single image while video is a series of images played in sequence at a specified frame rate, usually from software playing a video file (digital) or hardware playing a tape (analog). In other words, video has movement.

Same thing we will observed in the OpenCV when we try to read and display the video we got the sequence of the images and we can perform the same operation which can perform on the image.

Let’s see how it will happened.

Output of the above mentioned code is :

Frame 1 of the video
Frame 2 of the video

I have shown the first 2 frames of the video. But in the output you will get the all possible frame present in the video.

Also, one important thing if we have to capture the live video then we can use cv.VideoCapture(0). If we have 2 cameras connected with system then we can use cv.VideoCapture(1) and we can increase the number further w.r.t the number of cameras.

4) How to resize the image?

4.1. Shrinking the image:

Some Time we get the images which are of the high resolution so it is very difficult to perform the any operation on such image because it will consume more space and impact the performance. In such case we will resize and rescale the image to avoid such issues. cv.resize() Preferable interpolation methods are cv.INTER_AREA for shrinking of original image.

Below mention is the function which we can use for the resizing of the image, video and live video.

Now, we will try to resize the image by 50%.

Output for the above mentioned code is:

Original Image:

Post Resizing:

4.2. Enlarging the image:

Above we have seen the shrinking of the image. Now we will focus on the enlarging of the image.

We will use cv.INTER_CUBIC & cv.INTER_LINEAR for enlarging. cv.INTER_CUBIC is slow as compare to the cv.INTER_LINEAR but the output is comparatively better in case of cv.INTER_CUBIC than cv.INTER_LINEAR. By default, the interpolation method cv.INTER_LINEAR is used for all resizing purposes.

Output for the above mentioned code is:

4.3. Video Resizing:

Now we will check the resizing of the video.

Output for the above mentioned code is:

Post Resizing the video

below mentioned is the another function which is helpful in case of the live video.

5) How to add shapes and text within the image?

Here, we will try to add the different shapes like the rectangle ,circle and also experiment with the thickness of the shape.

5.1. Adding Rectangle in the image:

Below mentioned is the code here we have taken the original_img as the Input image on which we will draw the rectangle. Starting pixel point of the rectangle is(200,200) and it is upto the (300,300). While color is B=200,G=100 and R=150.

Check the below mentioned code and it’s output

Output of the above mentioned code:

5.2. Adding Rectangle with the full thickness:

Below mentioned is the code here we have taken the original_img as the Input image on which we will draw the rectangle. Starting pixel point of the rectangle is(0,0) and it is upto the center of the image. While color is B=200,G=155 and R=40. Here we have taken the thickness=-1 so whole the rectangle is fill with the color.

Output of the above mentioned code:

5.3. Adding Circle with the full thickness:

Below mentioned is the code here we have taken the original_img as the Input image on which we will draw the circle. center pixel point of the circle is(450,450) and radius of the circle is 40. While color is B=25,G=0 and R=100. Here we have taken the thickness=-1 so whole the circle is fill with the color.

Output of the above mentioned code:

5.4. Adding Line in the image:

Below mentioned is the code here we have taken the original_img as the Input image on which we will draw the rectangle. Starting pixel point of the line is (300,300) and endpoint pixel for the line is (500,500).

Output of the above mentioned code:

5.5. Adding the Text in the image:

Here, we will add the text in the blank image by using the cv.putText() method where we input the text, pixel place where we have to put the text, we can also select font type like FONT_HERSHEY_SIMPLEX, FONT_HERSHEY_PLAIN, etc., font Scale, color, thickness.

Also we can create the blank image by using the numpy which is also mentioned in the below mentioned code.

Output of the above mentioned code:

6) How to crop the image?

We can easily crop the particular part of the image by using the below mentioned code. Here only you have to mentioned the pixel which you have to crop.

Output of the above mentioned code:

Original image:

Crop part from the image:

7) How to rotate or shift the image?

If we want to shift the images then check the below mentioned function which is very useful for the same:

Here we are trying to rotate the image by using the above mentioned function.

Output for the above mentioned code:

Now, we will try to rotated the above already rotated image.

From the above output we got to know that if we try to rotate the already rotated image then the part of the image which is not visible in earlier image same is also missing in the current output image.

Also, we can flip the image upside down. Check the below mentioned code and it’s output.

Output of the above mentioned code:

Summary:

If you have read the blog till now then you must be familiar with the OpenCV. Rest of the topics of the OpenCV is cover in the next blog. Check link for the next blog where we will focus on some advanced stuff of the OpenCV.

Profile:

Thanks for reading! do appreciate my hard work by clapping. I’m always open to constructive feedback — if you have follow-up ideas for this analysis, comment it below or reach out via LinkedIn.

References:

1)https://www.youtube.com/watch?v=oXlwWbU8l2o&t=8707s

2)https://docs.opencv.org/master/d6/d00/tutorial_py_root.html

Nerd For Tech

From Confusion to Clarification

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Shubham D

Written by

Shubham D

Data Science and Machine Learning Enthusiast

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.