cft

Top 10 OpenCV Functions Everyone Has To Know About

We take a look at the library called OpenCV, which is used for both Computer Graphics and Vision.


user

Lazar Gugleta

3 years ago | 3 min read

Computer Vision and Computer Graphics are really popular right now since they are vastly connected to Artificial Intelligence and the main thing they have in common is that they use the same library OpenCV in order to perform high-level understanding from digital images or videos (CV) or generating images (CG).

That is why today we are going to take a look at that same library that fuels these big fields in Computer Science and see what are some functions that you can benefit majorly from!

Original photo by creativeart

Before we go into the powerful functions of OpenCV, let’s take a look at the definitions of Computer Vision, Graphics, and OpenCV to understand better what we are doing here.

Computer Vision

Computer vision is an interdisciplinary field that deals with how computers can be made to gain a high-level understanding of digital images or videos. From the perspective of engineering, it seeks to automate tasks that the human visual system can do.

Computer Graphics

Computer graphics is a branch of computer science that deals with generating images with the aid of computers. Today, computer graphics is a core technology in digital photography, film, video games, cell phone and computer displays, and many specialized applications.

OpenCV

OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in commercial products.

The library provides tools for processing and analyzing the content of images, including recognizing objects in digital photos (such as faces and figures of people, text, etc.), tracking the movement of objects, converting images, applying machine learning methods, and identifying common elements in various images.

Once we got that out of the way, we can begin with the top 10 Functions of my personal choice. (Code written with the functions is going to be in Python)

imread/imshow

This function has to be first since it is essential to starting your project with an image. As you can guess from the name of the function, it loads an image in the BGR (Blue-Green-Red) format.

import cv2
import matplotlib.pyplot as plotimage = cv2.imread('data.png') #load image
plot.imshow(image) #show image

cvtColor

Once you load the image, you can also convert it to different color schemes using different flags in cvtColor.

cv2.cvtColor(image,cv2.COLOR_BGR2RGB)

Here are some other flags for cvtColor: COLOR_BGR2GRAY, COLOR_BGR2HSV, and COLOR_BGR2YUV, etc.
This goes both ways, so COLOR_YUV2BGR, for example, is also possible.

resize

Sometimes you just need an image with a different size than the original so this is the function you need.

cv2.resize(image, dimension, interpolation = cv2.INTER_AREA)

It takes the original image and with dimension creates a new one. Dimension is defined as:

dimension = (width, height)

Interpolation is the way it resamples a picture, in my concrete example it uses INTER_AREA — resampling using pixel area relation and there are more of those like

  1. INTER_NEAREST: Nearest neighbor interpolation
  2. INTER_LINEAR: Bilinear interpolation
  3. INTER_CUBIC: Bicubic interpolation over 4×4 pixel neighborhood
  4. INTER_LANCZOS4: Lanczos interpolation over 8×8 neighborhood

split/merge

Each picture has 3 channels and if we want to split each of them into separate images, we can do that by using split functions.

(channel_b, channel_g, channel_r) = cv2.split(img)

If the image is in the BGR format, it will separate each channel into those three variables you define.

After you have already split the channels and you want to merge them back together, you use merge.

cv2.merge(channel_b, channel_g, channel_r)

vconcat/hconcat

Use vconcat(), hconcat() to concatenate (combine) images vertically and horizontally. v means vertical and h means horizontal.

cv2.vconcat([image1, image2])
cv2.hconcat([image1, image2])

ones/zeros

If you want to fill an image (Mat) with ones or zeros for all three dimensions because Mat requires 3 layers/dimensions for a color image.

size = 200, 200, 3
m = np.zeros(size, dtype=np.uint8)
n = np.ones(size, dtype=np.uint8)

As a bonus function, there is one thing I want to add here and that is transpose function.

transpose

If we have a defined matrix mat that we want to transpose, all we have to do is use this function on it:

import numpy as np  
mat = np.array([[1, 2, 3], [4, 5, 6]])
mat_transpose = mat.transpose()
print(mat_tranpose)

We get the output:

[[1 4]  
[2 5]
[3 6]]
#original input
[[1, 2, 3]
[4, 5, 6]]

We are done!

Next steps

This is mostly for beginners, but next time we will take a look at more advanced features of OpenCV.

Upvote


user
Created by

Lazar Gugleta


people
Post

Upvote

Downvote

Comment

Bookmark

Share


Related Articles