OpenCV-Python learning (16) - OpenCV image rotation angle calculation (NumPy trigonometric function)

1. Learning Objectives

  1. Learn to use NumPy's trigonometric functions (sin(), cos(), tan()) ;
  2. Learn to use NumPy's inverse trigonometric functions (arcsin(), arccos(), arctan()) ;

2. Description of input parameters of trigonometric functions

parameter illustrate
x array_like represents the angle, in radians (2π = 360°) Note: The input here is in radians, and you need to convert the angles into radians through np.pi for input .
out ndarray, None, or ndarray and None optional . Indicates where the results are stored. If provided, it must have the shape the input broadcasts to. If not provided or None, returns a newly allocated array. The length of the tuple (which can only be used as a keyword argument) must be equal to the number of outputs.
where array_like is optional . Indicates that this condition is broadcast over the input. Where the condition is true, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if the uninitialized out array is created with out=None by default, the locations where the condition is False will remain uninitialized.
**quargs Indicates that for other keyword-only arguments, see the ufunc documentation .

3. Trigonometric function return parameter description

parameter illustrate
y array_like. Represents the sine of each element of x. If x is a scalar, then this is a scalar.

4. Function instructions

4.1 Instructions for using numpy.sin()

numpy.sin(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'sin'>

4.2 Instructions for using numpy.cos()

numpy.cos(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'cos'>

4.3 Instructions for using numpy.tan()

numpy.tan(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'tan'>

5. Examples of NumPy's trigonometric functions (sin(), cos(), tan())

5.1 Example code

import numpy as np

rad_pi_every_deg = np.pi / 180
# 每一度的弧度值
print('一度的弧度值:',rad_pi_every_deg)
# 每一度的弧度值对应的角度
print('每一度的弧度值对应的角度:',np.rad2deg(rad_pi_every_deg))

# 计算30度弧度或三角函数
methods = [np.sin, np.cos, np.tan]
vals = list(map(lambda method: np.round(method(rad_pi_every_deg * 30),1),methods))
print('计算30度弧度或三角函数:',vals)

# 计算0,30,45,60,90度的正弦,余弦,正切的值
degs = [0,30,45,60,90]
# 将角度转对应弧度
degs = list(map(lambda val: rad_pi_every_deg * val,degs))
# 计算0,30,45,60,90度的正弦值
vals_sin = np.sin(degs)
print('计算0,30,45,60,90度的正弦值:', vals_sin)
# 计算0,30,45,60,90度的余弦值
vals_cos = np.cos(degs)
print('计算0,30,45,60,90度的余弦值:', vals_cos)
# 计算0,30,45,60,90度的正切值
vals_tan = np.tan(degs)
print('计算0,30,45,60,90度的正切值:', vals_tan)

5.2 Example running results

一度的弧度值: 0.017453292519943295
每一度的弧度值对应的角度: 1.0
计算30度弧度或三角函数: [0.5, 0.9, 0.6]
计算0,30,45,60,90度的正弦值: [0.         0.5        0.70710678 0.8660254  1.        ]
计算0,30,45,60,90度的余弦值: [1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01
 6.12323400e-17]
计算0,30,45,60,90度的正切值: [0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00
 1.63312394e+16]

6. Inverse trigonometric function input parameter description

parameter type of data illustrate
x array variable Indicates the y-coordinate on the unit circle.
out n-dimensional array, None, a tuple of n-dimensional arrays, optional parameters Indicates where the specified result is stored. If this parameter is provided, its dimension must be consistent with the broadcasted dimension of the input array. If this parameter is not provided or the parameter value is None, a newly created array will be returned. If this parameter is a tuple, its length must be consistent with the number of return values.
where Array variable, optional parameter Indicates that True is used to mark the position where function calculation is performed, and False is used to mark that this position does not perform function calculation, and directly returns the input value as it is, usually using the default value.

7. Inverse trigonometric function return parameter description

parameter type of data illustrate
y n-dimensional array Represents the value of the inverse trigonometric function for each element in x. The result is in radians and falls within the closed interval [-pi/2, pi/2]. If x is a scalar, then this computed value is also a scalar. Note [return value: in radians]

7. Instructions for Inverse Trigonometric Functions

7.1 Instructions for using numpy.arcsin()

numpy.arcsin(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'arcsin'>

7.2 Instructions for using numpy.arccos()

numpy.arccos(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'arccos'>

7.3 Instructions for using numpy.arctan()

numpy.arctan(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'arctan'>

8. NumPy's inverse trigonometric functions (arcsin(), arccos(), arctan()) examples

8.1 Example code

import numpy as np

rad_pi_every_deg = np.pi / 180
# 每一度的弧度值
print('一度的弧度值:',rad_pi_every_deg)
# 每一度的弧度值对应的角度
print('每一度的弧度值对应的角度:',np.rad2deg(rad_pi_every_deg))

# 计算30度弧度或三角函数
methods = [np.sin, np.cos, np.tan]
vals = list(map(lambda method: np.round(method(rad_pi_every_deg * 30),1),methods))
print('计算30度弧度或三角函数:',vals)

# 计算0,30,45,60,90度的正弦,余弦,正切的值
degs = [0,30,45,60,90]
# 将角度转对应弧度
degs = list(map(lambda val: rad_pi_every_deg * val,degs))
# 计算0,30,45,60,90度的正弦值
vals_sin = np.sin(degs)
print('计算0,30,45,60,90度的正弦值:', vals_sin)
# 计算0,30,45,60,90度的余弦值
vals_cos = np.cos(degs)
print('计算0,30,45,60,90度的余弦值:', vals_cos)
# 计算0,30,45,60,90度的正切值
vals_tan = np.tan(degs)
print('计算0,30,45,60,90度的正切值:', vals_tan)

# 计算0,30,45,60,90度的反正弦,反余弦,反正切的值,注意【返回值:以弧度为单位】
# 计算0,30,45,60,90度的反正弦值
vals_arcsin = np.rad2deg(np.arcsin(vals_sin))
print('计算0,30,45,60,90度的反正弦值:', vals_arcsin)
# 计算0,30,45,60,90度的反余弦值
vals_arccos = np.rad2deg(np.arccos(vals_cos))
print('计算0,30,45,60,90度的反余弦值:', vals_arccos)
# 计算0,30,45,60,90度的反正切值
vals_arctan = np.rad2deg(np.arctan(vals_tan))
print('计算0,30,45,60,90度的反正切值:', vals_arctan)

8.2 Output results of inverse trigonometric functions

计算0,30,45,60,90度的反正弦值: [ 0. 30. 45. 60. 90.]
计算0,30,45,60,90度的反余弦值: [ 0. 30. 45. 60. 90.]
计算0,30,45,60,90度的反正切值: [ 0. 30. 45. 60. 90.]

9. Conversion functions between radians and degrees

function illustrate Instructions
numpy.degrees() Converts an angle from radians to degrees. numpy.degrees(x, /, out=None, *, where=True, casting=‘same_kind’, order=‘K’, dtype=None, subok=True[, signature, extobj]) = <ufunc ‘degrees’>
numpy.rad2deg() Converts an angle from radians to degrees. numpy.rad2deg(x, /, out=None, *, where=True, casting=‘same_kind’, order=‘K’, dtype=None, subok=True[, signature, extobj]) = <ufunc ‘rad2deg’>
numpy.radians() Converts an angle from degrees to radians. numpy.radians(x, /, out=None, *, where=True, casting=‘same_kind’, order=‘K’, dtype=None, subok=True[, signature, extobj]) = <ufunc ‘radians’>
numpy.deg2rad() Converts an angle from degrees to radians. numpy.deg2rad(x, /, out=None, *, where=True, casting=‘same_kind’, order=‘K’, dtype=None, subok=True[, signature, extobj]) = <ufunc ‘deg2rad’>

10. Summary

  1. numpy.deg2rad(x) equals x * pi / 180;
  2. numpy.radians(x) equals x * pi / 180;
  3. The input value of the trigonometric function is radian, so when the trigonometric function of finding an angle, the degree must be converted into radian ;
  4. The return value of the inverse trigonometric function is in radians, so to get the angle of the inverse trigonometric function, you must convert radians to degrees .

Guess you like

Origin blog.csdn.net/m0_38082783/article/details/127921217