Top ten opencv functions that everyone should know

Time:2021-9-15

By Lazar gugleta
Compile Flin
Source: towardsdatascience

Computer vision and computer graphics are very popular now because they are widely connected with artificial intelligence. Their main common point is to use the same opencv library to have a high-level understanding of digital images or video (CV) or generated images (CG) like humans.

That’s why today we want to take a look at what functions you can benefit most from the same library in the field of computer science!

Before discussing the power of OpenCV, let’s look at the definitions of computer vision, computer graphics and OpenCV to better understand what we are doing here.

computer vision

Computer vision is an interdisciplinary field, which involves how to make computers obtain a high-level understanding of digital images or videos. From an engineering point of view, it attempts to automate the tasks that human visual system can complete, that is, it wants to “understand” the world like human beings.

computer graphics

Computer graphics is a branch of computer science. It studies how to generate images with the help of computers. Nowadays, computer graphics is the core technology in many professional application fields, such as digital photography, film, video game, mobile phone and computer display.

OpenCV

Opencv (open source computer vision library) is an open source computer vision and machine learning software library. Opencv is established to provide a general infrastructure for computer vision applications and accelerate the application of machine perception in commercial products.

The library provides tools for processing and analyzing image content, including identifying objects in digital photos (such as human faces, graphics, text, etc.), tracking object motion, converting images, applying machine learning methods, and identifying common elements in various images.

Once we solve this problem, we can start with the top ten functions of my personal choice. (write functions in Python)

imread/imshow

This function must be in the first place, because it is essential to start your project with a picture. As you can guess from the function name, it loads an image in BGR (blue green red) format.

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

cvtColor

After loading an image, you can also convert it to a different color scheme using different flags in cvtcolor.

cv2.cvtColor(image,cv2.COLOR_BGR2RGB)

Here are some other flags of cvtcolor: color_ BGR2GRAY、COLOR_ Bgr2hsv and color_ Bgr2yuv et al.

This is bidirectional, for example, color_ Yuv2bgr is also possible.

resize

Sometimes you only need an image different from the original size, so resize is the function you need.

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

It takes the original image and creates a new image by size. Dimensions are defined as:

dimension = (width, height)

Interpolation is a method of resampling pictures. In my example, it is based onINTER_AREAThere are more similar methods for inter region resampling

  1. INTER_NEAREST:   Nearest neighbor interpolation
  2. INTER_LINEAR:   bilinear interpolation
  3. INTER_CUBIC:   four × Bicubic interpolation on 4-pixel neighborhood
  4. INTER_LANCZOS4:   eight × Lanczos interpolation on 8 neighborhood

split/merge

Each picture has three channels. If we want to divide them into different pictures, we can use the segmentation function.

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

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

If you have divided channels but want to merge them together, you can use merge.

cv2.merge(channel_b, channel_g, channel_r)

vconcat / hconcat

usevconcat()hconcat()Connect (merge) images vertically and horizontally.

V is vertical and H is horizontal.

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

ones/zeros

If you want to fill 1 or 0 in the three-dimensional image (matrix) (because the matrix requires the color image to have 3 dimensions).

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

As an additional function, I want to add something here, that is, the transpose function.

transpose

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

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

Get output:

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

Next step

This is mainly for beginners, but in the next lesson, we will introduce the more advanced features of OpenCV.

Original link:https://towardsdatascience.com/top-10-opencv-functions-everyone-has-to-know-about-945f33de8f6f

Welcome to panchuang AI blog:
http://panchuang.net/

Official Chinese document of sklearn machine learning:
http://sklearn123.com/

Welcome to panchuang blog resources summary station:
http://docs.panchuang.net/