Author | Lazar Gugleta compile | Flin source | towardsdatascience

Computer vision and computer graphics are very popular now because they have broad connections to artificial intelligence, and their main common point is to use the same OpenCV library in order to have a high-level understanding of digital images or videos (CV) or generated images (CG) like humans.

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

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

Computer vision

Computer vision is an interdisciplinary field that involves enabling computers to gain a high-level understanding of digital images or video. From an engineering point of view, it attempts to automate tasks that the human visual system can perform, that is, it hopes to “see” the world as humans do.

Computer graphics

Computer graphics is a branch of computer science that studies how to generate images with the help of computers. Today, computer graphics is at the heart of many professional applications, including digital photography, film, video games, mobile phones and computer displays.

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 accelerate the use of machine awareness in commercial products.

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

Once we have that out of the way, we can start with my top 10 personal choices. (Write functions in Python)

imread/imshow

This function must be in the first place because it is essential to start your project with an image. As you can guess from the function name, it loads the 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
Copy the code

cvtColor

Once an image is loaded, it can also be converted to a different color scheme using different flags in cvtColor.

cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
Copy the code

Here are some other cvtColor flags: COLOR_BGR2GRAY, COLOR_BGR2HSV, COLOR_BGR2YUV, etc.

This is two-way, for example, COLOR_YUV2BGR is also possible.

resize

Sometimes you just need a different size image, so resize is the function you need.

cv2.resize(image, dimension, interpolation = cv2.INTER_AREA)
Copy the code

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

dimension = (width, height)
Copy the code

Interpolation is a method of resampling an image. In my case, it uses inter_area-based resampling between areas, and there are many more similar methods

  1. INTER_NEAREST interpolation
  2. INTER_LINEAR: Bilinear interpolation
  3. INTER_CUBIC: Bicubic interpolation over 4×4 pixel neighborhoods
  4. INTER_LANCZOS4Lanczos interpolation on 8×8 neighborhoods
    • Scc.ustc.edu.cn/zlsc/sugon/…

split/merge

Each image has 3 channels, if we want to divide them into different images, we can use the partition function to do so.

(channel_b, channel_g, channel_r) = cv2.split(img)
Copy the code

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

If you already split channels, but want to merge them together, you can use merge.

cv2.merge(channel_b, channel_g, channel_r)
Copy the code

vconcat / hconcat

Join (merge) images vertically and horizontally using vconcat() and hconcat().

V for vertical, H for horizontal.

cv2.vconcat([image1, image2])
cv2.hconcat([image1, image2])
Copy the code

ones/zeros

If you want to fill a three-dimensional image (matrix) with either 1 or 0 (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)
Copy the code

As an additional function, I want to add one thing here, which is the transpose function.

transpose

If we have a defined matrix mat to transpose, all we need 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)
Copy the code

Get the output:

[[1 4]  
 [2 5]  
 [3 6]]
#original input
[[1.2.3]
 [4.5.6]]
Copy the code

Next step

This is primarily for beginners, but next time we’ll cover more advanced features of OpenCV.

The original link: towardsdatascience.com/top-10-open…

Welcome to panchuangai blog: panchuang.net/

Sklearn123.com/

Welcome to docs.panchuang.net/