Introduction to Python third-party cv2 library

Introduction to Python third-party cv2 library

CV2 refers to OpenCV2 (Open Source Computer Vision Library), which is an open source library platform computer vision library. It has powerful image processing functions and can realize many general algorithms in image processing and computer vision.

OpenCV official website documentation https://docs.opencv.org/4.1.2/d6/d00/tutorial_py_root.html
Chinese documentation http://www.woshicver.com/
 

Special reminder : use opencv_python when installing, but use cv2 when importing and using.

In the Windows environment, the Python module (library, package) installation command format, in cmd:

[py -XY -m] pip install [-i mirror URL] module (library, package) name

The [] part indicates optional

If multiple python versions are installed, XY represents the Python version, and the redundant part is discarded. For example, 3.8 is taken as 3.8.1, and 3.10 is taken as 3.10.5, that is, only the part before the second point is taken. Not required for only one python version installed.

Using the mirror URL can speed up the installation, the commonly used mirror URL

Tsinghua: https://pypi.tuna.tsinghua.edu.cn/simple

Alibaba Cloud: https://mirrors.aliyun.com/pypi/simple/

University of Science and Technology of China https://pypi.mirrors.ustc.edu.cn/simple/

【See https://blog.csdn.net/cnds123/article/details/104393385

I use it here (installed multiple python versions to install cv2 library for version 3.9.1 - opencv_python, using Alibaba Cloud mirror. It is opencv_python when installed, but import cv2 is used when importing.):

py -3.10 –m pip install -i https://mirrors.aliyun.com/pypi/simple/ opencv_python

See the picture below:

 Finally, check the installation

Note : When using the cv2 library, the file name and path cannot have Chinese characters. Once there are Chinese characters, there will be various inexplicable errors!

read image and display

Read a picture function: cv2.imread(filepath,flags)  

Where filepath represents the path of the image

Flags indicate what picture to read:

(1) cv2.IMREAD_COLOR: default parameter, read in a color picture, ignore the alpha channel (or write 1 directly)

(2) cv2.IMREAD_GRAYSCALE: read in grayscale images (or write 0 directly)

Display a picture function: cv2.imshow(window_name, image)  

Among them, the first parameter is the name of the window, and the second parameter is the image to be displayed

Keyboard binding function cv2.waitKey(delay)

The unit of delay is ms milliseconds. When delay takes a value greater than 0, the program waits for the user to press a key to close the graphic display window within the given delay time, or wait until the time is up to close the graphic display window. If delay is set to 0, it means waiting to press any key to close the graphics display window.

Without it, the window will close the graphics display window as soon as the program finishes.

Tip, the effect of this function can only be seen when you leave the IDLE environment, such as double-clicking the py script file to run it.

The source code for reading in pictures and displaying them is given below

import cv2
image1=cv2.imread(r"D:/cv2Demo/lotus2.png")
cv2.imshow("image1",image1)
cv2.waitKey(0)

Run it:

save the image

The function cv2.imwrite() is used to save the image to the specified file.

cv2.imwrite(filename, img [, paras])

Parameter Description:

filename: the path and name of the file to save, including the file extension

img: OpenCV image to save, nparray multidimensional array

paras: parameters of different encoding formats, optional

Convert color pictures to grayscale pictures and save

The source code is as follows:

import cv2
image1=cv2.imread(r"D:/cv2Demo/lotus2.png",0)
cv2.imwrite(r"D:/cv2Demo/lotus2As.png",image1)

image scaling

Image scaling function cv2.resize(image, image2,dsize)

Input the original image, output the new image, the size of the image)

cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])

Parameter Description:

scr: the input image for the transform operation

dsize: The size of the output image, a 2-tuple (width, height)

dst: the output image of the transform operation, optional

fx, fy: scale on x-axis and y-axis, real type, optional

interpolation: interpolation method, integer, optional

cv2.INTER_LINEAR: bilinear interpolation (default method)

cv2.INTER_AREA: Use pixel area relationship resampling to avoid ripples when shrinking the image

cv2.INTER_NEAREST: nearest neighbor interpolation

cv2.INTER_CUBIC: bicubic interpolation of 4x4 pixel neighborhood

cv2.INTER_LANCZOS4: Lanczos interpolation of 8x8 pixel neighborhood

The image is reduced to 1/4 of the original

The source code is as follows:

import cv2
image1=cv2.imread(r"D:/cv2Demo/lotus2.png")
print(image1.shape) #
height, width = image1.shape[:2]  # 获取图片的高度和宽度
image2=cv2.resize(image1,(int(0.5*width), int(0.5*height)))
print(image2.shape) #
cv2.imwrite(r"D:/cv2Demo/lotus2Quarter.png",image2)#保存

image flip

Image flip function cv2.flip(src, flipCode[, dst])

Parameter Description:

scr: the input image for the transform operation

flipCode: control parameter, integer (int), flipCode>0 horizontal flip, flipCode=0 vertical flip, flipCode<0 horizontal and vertical flip

dst: the output image of the transform operation, optional

Image flip source code:

import cv2
image1=cv2.imread(r"D:/cv2Demo/lotus2.png")
imgFlip1 = cv2.flip(image1, 1)  # 0垂直翻转 ;1水平翻转 ;-1水平和垂直翻转
cv2.imshow("image2",imgFlip1)
cv2.waitKey(0)

image rotation

Realize the right-angle multiple rotation function of the image cv2.rotate(src, rotateCode[, dst])

cv2.rotate() rotates a 2D array (array) in multiples of 90 degrees and is fast.

Parameter Description:

src: the input image for the transform operation

rotateCode: enumeration, specify the rotation angle.

  cv2.ROTATE_90_CLOCKWISE: rotate 90 degrees clockwise

  cv2.ROTATE_180: Rotate 180 degrees

  cv2.ROTATE_90_COUNTERCLOCKWISE: rotate 90 degrees counterclockwise

dst: It is the output image with the same size and depth as the src image. It is an optional parameter.

The image is rotated 90 degrees clockwise Source code:

import cv2
image1=cv2.imread(r"D:/cv2Demo/lotus2.png")
image2 = cv2.rotate(image1,cv2.ROTATE_90_CLOCKWISE) #顺时针旋转90度
cv2.imshow("image2",image2)
cv2.waitKey(0)

The following introduces the image rotation at any angle , first introduces the use of two functions

Get the rotation transformation matrix function cv2.getRotationMatrix2D(center, angle, scale)

Parameter Description:

center: Rotation center coordinates, two-tuple (x0, y0)

angle: rotation angle, the unit is angle, counterclockwise is a positive number, clockwise is a negative number

scale: scaling factor

Use the transformation matrix to transform the image function cv2.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])

in:

src - the input image.

M - Transformation matrix.

dsize - the size of the output image.

flags - combination of interpolation methods (int type!)

borderMode - the border pixel mode (int type!)

borderValue - (Important!) The border padding value; by default, it is 0.

The following introduces three implementation methods for image rotation at any angle

Method 1. Image rotation at any angle source code (with clipping)

 

​​​​​​​​The source code is as follows:

#图像旋转任意角度源码(有剪裁)
import cv2

#第一个参数穿opencv读取的图像,第二个参数传入需要旋转的角度
def rotate_bound(image, angle):
    height, width = image.shape[:2]     # 取前两个值(H,W) 
    # 第一个参数旋转中心,第二个参数旋转角度,第三个参数缩放比例 
    center = ( width//2,height//2)  # 以图像中心为旋转中心 
    scale = 1                 # 等比例旋转,即旋转后尺度不变 
    # 获得旋转矩阵 
    M = cv2.getRotationMatrix2D(center, -angle, scale)
 
    # 进行仿射变换,其中
    #“,borderValue=(255,255,255)”定义边界填充色彩白色,省略默认黑色,相当于borderValue=(0,0,0)
    return cv2.warpAffine(image, M, (width, height),borderValue=(255,255,255))

image1 = cv2.imread(r"D:/cv2Demo/lotus2.png")
image2 = rotate_bound(image1, 45)
cv2.imshow('image', image2)  #显示图片
cv2.waitKey(0)

Method 2. Image rotation at any angle source code (no clipping)

The source code is as follows:

#图像旋转任意角度源码(无剪裁)
import numpy as np
import cv2
#第一个参数穿opencv读取的图像,第二个参数传入需要旋转的角度
def rotate_bound(image, angle):
    # 获取图像的尺寸,并确定中心
    (h, w) = image.shape[:2]
    (cX, cY) = (w // 2, h // 2)

    # 获取旋转矩阵(应用角度的负数以顺时针旋转)
    # 获取正弦和余弦(即矩阵的旋转分量)
    M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
    cos = np.abs(M[0, 0])
    sin = np.abs(M[0, 1])

    #计算图像的新边界尺寸
    nW = int((h * sin) + (w * cos))
    nH = int((h * cos) + (w * sin))

    # 调整矩阵
    M[0, 2] += (nW / 2) - cX
    M[1, 2] += (nH / 2) - cY

    #进行仿射变换,其中
    #“,borderValue=(255,255,255)”定义边界填充色彩白色,省略默认黑色,相当于borderValue=(0,0,0)
    return cv2.warpAffine(image, M, (nW, nH),borderValue=(255,255,255))

image1=cv2.imread(r"D:/cv2Demo/lotus2.png")
image2 = rotate_bound(image1, 45)
cv2.imshow('image', image2)  #显示图片
cv2.waitKey(0)
#cv2.imwrite(r"D:/cv2Demo/lotus2AsSave.png", image2) #保存

Method 3. Image rotation at any angle source code (no clipping 2)

The source code is as follows (please compare with Method 2):

#图像旋转任意角度源码(无剪裁之二)
import cv2
import numpy as np 
 
def opencv_rotate(img, angle):
    h, w = img.shape[:2]  # 图像的(行数,列数,色彩通道数)  
    center = (w / 2, h / 2)
    scale = 1.0
    # 1 获取M矩阵
    # cv2.getRotationMatrix2D(获得仿射变化矩阵)
    M = cv2.getRotationMatrix2D(center, -angle, scale)
    # 2 扩大画布,新的宽高,radians(angle) 把角度转为弧度 sin(弧度)
    new_H = int(w * np.fabs(np.sin(np.radians(angle))) + h * np.fabs(np.cos(np.radians(angle))))
    new_W = int(h * np.fabs(np.sin(np.radians(angle))) + w * np.fabs(np.cos(np.radians(angle))))
    # 3 平移
    M[0, 2] += (new_W - w) / 2
    M[1, 2] += (new_H - h) / 2
 
    # cv2.warpAffine(进行仿射变化)
    rotate = cv2.warpAffine(img, M, (new_W, new_H), borderValue= (0, 0, 0))
    return rotate
 
image1=cv2.imread(r"D:/cv2Demo/lotus2.png")
image2 = opencv_rotate(image1, 45)
cv2.imshow('image', image2)  #显示图片
cv2.waitKey(0)
#cv2.imwrite(r"D:/cv2Demo/lotus2AsSave.png", image2) #保存

 OK!

Guess you like

Origin blog.csdn.net/cnds123/article/details/126547307