OpenCV Tutorial For Beginners- Part 1

OpenCV is a library written in C++ for image and video processing, and it supports many languages such as C++, Java, and python. It is the most used library for interacting with images if you build software that uses these operations. This library has official documentation but is hard to understand, so I’m here to help you learn this library.

1. Installing The IDE & OpenCV

Before starting this tutorial, you need to install an IDE on your machine. There are a lot of them, like Pycharm & Jupyter Notebook but I will stick with the Jupyter Notebook.

After installing the Jupyter Notebook, you need to install the OpenCV library to start the development. Type the following command in the Jupyter cell:

!pip3 install opencv-python

If you complete these two steps and the installation goes successfully, we will start working with simple commands of OpenCV.

2. Import Image

The first thing you need to learn in OpenCV is importing a picture in your code and showing it on the screen because this library is developed to work with images and video. Let’s see a simple example:

import cv2 as cv #Import OpenCV

img = cv.imread("Image.jpg") #Import the image

cv.imshow("Image", img) #Show the image

cv.waitKey(0) #Milliseconds time to show the image

It will automatically show the image on your screen after running the previous code. You probably still don’t understand the code but let me explain what we already did.

First, you’ve imported the OpenCV library, which is cv2 as an alias of cv. That means we don’t need to use cv2 every time we call this library to use it.

Next, we read the image using the .imread() function provided by OpenCV, and you will pass the image name and store the results inside a variable we call img.

We use the function .imshow() to show the image, and we pass first the window name that will show the image. In this case, the window name is Image, and we pass the variable that stores the image, which is img.

Finally, we use the function .waitKey(0), responsible for the time in milliseconds to show the image. If you’ve passed 100 instead of 0, then you will see the window for 100 milliseconds, and it will disappear automatically, 

The number 0 means you will see it for an infinite time until you press any key on your keyboard to stop it or close the window yourself.

3. Using The Camera

OpenCV also works with the camera since the video is just a bunch of images called frames that continuously shows on the screen. You will watch it as a video. Let’s see a simple example:

import cv2 as cv #Import OpenCV

video = cv.VideoCapture(0) #Running the camera
  
while(True): #Looping through the frames
    
    ret, frame = video.read() #Read the frames
  
    cv.imshow('frame', frame) #Show the frames
       
    if cv.waitKey(1) == ord('q'): #Stop the video
        break

video.release() #Close the camera
cv.destroyAllWindows() #Close the window

You will see a pop-up screen after running the code showing you the front camera of your laptop. This code seems complicated, but let me explain what every line is doing.

We always start the code importing the OpenCV library, and then we run the camera using the .VideoCapture(0) function and store it in the video variable. The number 0 in the closed brackets means we need OpenCV to open the default camera of your laptop.

Next, we loop over the frames in the video variable using the while() loop and pass the True value to keep opening the camera and the looping.

You will need to capture the video frames one by one using the function video.read() and store them in the frame variable.

After that, you will show these frames using the .imshow() function and pass the window name followed by the frame variable.

Later, you would like to stop the video. You will need to use the cv.waitKey(1) and evaluate if the user presses the q button on the keyboard or not. 

If he pressed the q button, it would break the loop and stop this whole process. The number 1 inside the cv.waitKey(1) means to display a new frame every 1ms.

Pressing the key q will stop the process, but the camera is still working behind the scene, so you need to use video.release() function to stop the camera and the cv.destroyAllWindows() function to close the windows.

You can apply the same of the videos, but you need to specify the location of the video inside your computer in the VideoCapture() instead of the 0 value.

3. Resize an Image

If you’ve chosen a big picture in the previous example, you probably couldn’t see the whole content of the picture. Sometimes you need to make the image much smaller, and OpenCV can perform this action very quickly:

import cv2 as cv #Import OpenCV

img = cv.imread("image.jpg") #Import the image

scale = 60 #The scale of changing the image

width = int(img.shape[1] * scale / 100) #Change the width
height = int(img.shape[0] * scale / 100) #Change the height

dim = (width, height) #New dimention

resized_img = cv.resize(img, dim, cv.INTER_AREA) #Resize the image

cv.imshow("Resized_Image", resized_img) #Show the image

cv.waitKey(0) #Wait for an infinite time
cv.destroyAllWindows() #Close the window

You will notice that the window pop-up that shows the image is tiny, and the code runs successfully without any error. Let’s see how it works.

The very first two lines are for importing the library and the image. Then we choose a value out of 100, which is 60, to change the dimension percentage of the picture.

The command img.shape[1] is used to get the image’s width, and the command img.shape[0] is for the height, and we will use a mathematic equation to reduce the percentage by 60%, as you see in the code.

You need to store these two new values inside a tuple, as you can see in the variable dim (dimension), which you will need like this in the next step.

We resize the image using the .resize() function and pass the image you want to change its dimension, the new dimension, and the built-in OpenCV algorithm cv.INTER_AREA will use a technique to change its size.

Finally, show the image with the .imshow() function for an infinite time using the .waitKey(0), and when you press any key on your keyboard, it will close the window by using the .destroyAllWindows() function.

Conclusion

Thanks for reading! OpenCV is an extensive library with many other functionalities you can apply to images and videos. Next in the following parts, we are going to see how you can be an intermediate user in computer vision.

Scroll to Top