Opencv Python learns the basic processing and geometric transformation of pictures

Posted by versatilewt on Tue, 08 Feb 2022 10:58:28 +0100

1, Basic image processing -- operation

All the following functions are omitted cv2.

1.1.1 addition operation

As has been said before, for a computer, an image is just a matrix.

The addition of images is the addition of matrices.

We use the add(a, b) function to realize the image addition operation.

Note: when a + B < = 255, a+b = a+b; When a + b > 255, a+b = 255.

If their sum exceeds 255, it will be truncated.

Dst = add(image1,image2) # that is, Dst is the result of adding two images

Dst = add(image1,6) # that is, Dst is the value of each value in the image1 matrix plus 6

1.1.2 subtraction

We use the subtract(a, b) function to realize the image subtraction.

Note: when A-B > = 0, a-b = a-b; When A-B < 0, a-b = 0.

  1. b) If the difference is less than 0, it will be truncated.

Example: 1-6 = 0; 5 -6 = 0.

The use method of this function is the same as above (addition operation).

1.1.3 multiplication

There are two kinds of matrix multiplication, one is pure mathematical matrix multiplication, and the other is matrix point multiplication

The first one can be obtained by using dot(a, b) in numpy module.

The calling method is: DST = numpy dot(a, b)

The second is matrix point multiplication. The function to realize it is in opencv module, which is multiply(a, b). {omit cv2.}

The calling method is the same as above.

And when the product is greater than 255, it will be truncated at 255 as in addition.

Dst = multiply(a, b)

1.1.4 division operation

We use the divide(a, b) function to divide the image.

Note: the final result obtained after division is an integer, because the range of pixels is between 0 and 255 and is an integer.

It uses rounding to approximate.

Dst = divide(a, b)

1.1.5 bitwise AND

We use bitwise_and(src1. src2) to realize the bitwise and operation of the image.

An image is a matrix. In essence, this function divides each value of the two matrices into 8 binary numbers for bitwise and operation.

src1 is matrix 1 and src2 is matrix 2.

Call method:

Dst = bitwise_and(src1, src2) #Dst is the result matrix after bitwise sum operation of src1 and src2

1.1.6 by bit or

We use bitwise_or(src1. src2) to realize the bitwise OR operation of the image.

Bitwise OR operation is similar to bitwise and operation.

Call method:

Dst = bitwise_or(src1, src2) #Dst is the result matrix after bitwise OR operation of src1 and src2

1.1.7 bitwise non

We use bitwise_not(src1) to realize the bitwise non operation of the image.

Call method:

Dst = bitwise_not(src1) #Dst is the result matrix of src1 bitwise non operation

Generally used in black-and-white images.

1.1.8 bitwise XOR

We use bitwise_xor(src1. src2) to realize the bitwise XOR operation of the image.

Call method:

Dst = bitwise_xor(src1, src2) #Dst is the result matrix after bitwise XOR operation of src1 and src2

2, Geometric transformation of image

2.1 affine transformation

Affine transformation means that an image can be translated, scaled and rotated through a series of transformations.

The function for affine transformation provided by Opencv module is warpAffine().

Affine transformation requires a mapping matrix M with a size of 2x3.

This function calls the following method:

Dst = warpAffine(src, M, dsize)

src is the original image.

M is the mapping matrix.

dsize indicates the size of the output image.

Dst is the output image (that is, the image after affine transformation)

2.1.1 pan and zoom

For translation and scaling, their essence is the same, just change the mapping matrix M.

M = np.array([0.8, 0, 120;

                        0, 0.5, 20])

The mapping matrix indicates that the picture changes from 0.8 times horizontally to 0.5 times vertically, 120 units to the right and 20 units to the down.

In short, that's it.

When calling, first obtain the vertical length and horizontal length of the picture

h,w = image.shape[0:2]

Creating mapping matrix M

import numpy as np

M = np.float32([[1,0,120], [0,1,20]])

Then use affine function

image_Move = cv.warpAffine(image, M, (w,h))

The image obtained in this way_ Move is the image after panning and zooming.

The following code shows two processed images, one is translated and the other is reduced.

import cv2 as cv
import numpy as np

image = cv.imread("E:/dahai.jpeg")

h, w = image.shape[0:2]
print(h)    # Longitudinal distance
print(w)    # Lateral distance
M = np.float32([[1,0,120], [0,1,20]]) #120 right, 20 down, pan

image_Move = cv.warpAffine(image, M, (w,h))

cv.imshow("image",image)
cv.imshow("image_move",image_Move)

M2 = np.float32([[0.8, 0, 0], [0, 0.5, 0]]) # The horizontal direction is 0.8 times of the original, and the vertical direction is 0.5 times of the original
image_Suo = cv.warpAffine(image, M2, (w,h)) # Double the size
cv.imshow("image_Suo", image_Suo)

cv.waitKey()
cv.destroyAllWindows()

2.1.2 rotation

When you want to rotate the image, the mapping matrix is not easy to give. Here, we can use the getRotationMatrix2D() function provided in the opencv module to get the mapping matrix required to rotate the image.

Call method:

M = getRotationMatrix2D(center, angle, scale)

Center is the center point of rotation

Angle indicates the rotation angle. Positive number means counterclockwise rotation and negative number means clockwise rotation.

Scale represents the transformation scale, and the output image size becomes the scale times of the original image

The calling method is the same as before, but the way to get the mapping matrix M is changed.

If you use it, it's still the same.

First get the mapping matrix,

Then perform affine transformation,

Then display the image (remember to delay).

Example:

import cv2 as cv

image = cv.imread("E:/dahai.jpeg")
h,w = image.shape[0:2]   # h is the vertical length and w is the transverse length
print(image.shape)
print(h)
print(w)
# w1,h1 = image.shape[:2-geometric change of image]
# print(w1)
# print(h1)

M = cv.getRotationMatrix2D((w/2,h/2), 30, 0.6)
#An affine matrix is created with (w/2-geometric change of image, h/2-geometric change of image) as the center, rotating 10 ° counterclockwise, and the size is 0.8 times of the original image

image_Xuan = cv.warpAffine(image, M, (w,h))

cv.imshow("image_Xuan",image_Xuan)
cv.imwrite("E:/xuanzhuang.jpeg",image_Xuan)
cv.waitKey()
cv.destroyAllWindows()

    

 

2.2 remapping

The operation of placing the pixels in one image to the specified position in another image is called remapping.

Remapping gets a new image by modifying the position of pixels.

The function of mapping function is to find the position of new image pixels in the original image.

The remap() function in Opencv can be used for our custom remapping.

Call method:

Dst = remap(src, map1, map2, interpolation)

src represents the original image

map1 represents the x value of point (x, y)

map2 represents the y value of point (x, y)

Interpolation means interpolation method. Generally, we fill in CV2 INTER_ Linear is enough

In short,

Remapping is to read the data of the image first to form a matrix.

Then create two matrices (X and Y) of the same size as the image matrix.

Then traverse the image matrix and X, Y matrix in turn, and put the information of the image matrix into the X, Y matrix according to a certain law.

Then use the remapping function Dst = remap(image, X, Y, cv2.INTER_LINEAR)

Dst is the mapped image matrix.

2.2.1 copying

import cv2 as cv
import numpy as np

image = cv.imread("E:/dahai.jpeg")
h,w = image.shape[0:2]   #h is the longitudinal distance and w is the transverse distance
print(h,w,sep=',')#View vertical and horizontal distance
#The next two lines create a 0 matrix of size (h, w)
x = np.zeros((h,w), np.float32)
y = np.zeros((h,w), np.float32)

for i in range(h):#i is vertical and j is horizontal
    for j in range(w):
        x.itemset((i,j), j)
        y.itemset((i,j), i)

rst = cv.remap(image, x, y, cv.INTER_LINEAR)
cv.imshow("rst",rst)
cv.imshow("image",image)
cv.imwrite("E:/pycharm/dahai-xy.jpeg",rst)
cv.waitKey()
cv.destroyAllWindows()

2.2.2 turnover

If you flip, you only need to change the above traversal part.

Flip around the y axis:

import cv2 as cv
import numpy as np

image = cv.imread("E:/dahai.jpeg")
h,w = image.shape[0:2]   #h is the longitudinal distance and w is the transverse distance
print(h,w,sep=',')#View vertical and horizontal distance
#The next two lines create a 0 matrix of size (h, w)
x = np.zeros((h,w), np.float32)
y = np.zeros((h,w), np.float32)

for i in range(h):#i is vertical and j is horizontal
    for j in range(w):
        x.itemset((i,j), w-1-j)    # Flip around the y axis
        y.itemset((i,j), i)

rst = cv.remap(image, x, y, cv.INTER_LINEAR)
cv.imshow("rst",rst)
cv.imshow("image",image)
cv.imwrite("E:/pycharm/dahai-xy.jpeg",rst)
cv.waitKey()
cv.destroyAllWindows()

Flip around the x axis: (skip at the end)

import cv2 as cv
import numpy as np

image = cv.imread("E:/dahai.jpeg")
h,w = image.shape[0:2]   #h is the longitudinal distance and w is the transverse distance
print(h,w,sep=',')#View vertical and horizontal distance
#The next two lines create a 0 matrix of size (h, w)
x = np.zeros((h,w), np.float32)
y = np.zeros((h,w), np.float32)

for i in range(h):#i is vertical and j is horizontal
    for j in range(w):
        x.itemset((i,j), j)
        y.itemset((i,j), h-1-i)

Flip around both x and y axes:

import cv2 as cv
import numpy as np

image = cv.imread("E:/dahai.jpeg")
h,w = image.shape[0:2]   #h is the longitudinal distance and w is the transverse distance
print(h,w,sep=',')#View vertical and horizontal distance
#The next two lines create a 0 matrix of size (h, w)
x = np.zeros((h,w), np.float32)
y = np.zeros((h,w), np.float32)

for i in range(h):#i is vertical and j is horizontal
    for j in range(w):
        x.itemset((i,j), w-1-j)
        y.itemset((i,j), h-1-i)

This is most of the content of image geometric transformation.

Topics: Python OpenCV Computer Vision