OpenCV Notes - ndarray in Numpy

Posted by jozard on Thu, 22 Aug 2019 04:26:37 +0200

OpenCV Notes

ndarray in Numpy

The Python API for OpenCV is Numpy-based and is an open-source numeric computation extension of Python for storing and processing multidimensional arrays. Its core data structure is ndarray.

ndarray object

1. Construct a two-dimensional ndarray
First learn how to construct a two-bit ndarry.To construct a two-dimensional array (matrix), the most basic information you need to know is the number of rows (high) and columns (wide) and their data types, such as uint8, int32, float32, float64, and so on.To construct a two-dimensional array of float type with 2 rows and 3 columns all 0, the code is as follows:

import numpy as np
result=np.zeros((2,3),np.float)
print type(result)
# <type 'numpy.ndarray'>
print result
# [[0. 0. 0.]
#  [0. 0. 0.]]
print result[0]
# [0. 0. 0.]
print result[1]
# [0. 0. 0.]
print type(result[0])
# <type 'numpy.ndarray'>

Construct an integer matrix with 2 rows, 3 columns and all 1. The result is as follows:

import numpy as np
result=np.ones((2,3),np.int)
print type(result)
print result
# [[1 1 1]
#  [1 1 1]]
print result[0]
# [1 1 1]
print result[1]
# [1 1 1]

Initialize an integer matrix:

import numpy as np
result=np.array([[1,2,3,4],[5,6,7,8]],dtype=int)
print type(result)
#<type 'numpy.ndarray'>
print result
# [[1 2 3 4][5 6 7 8]]
print result[0]
# [1 2 3 4]
print result[1]
# [5 6 7 8]

1. Construct a two-dimensional ndarray
Three-dimensional arrays can be understood as each element is a two-dimensional array. By initializing an integer array of 222 as a column, the three-dimensional array can be understood as two two two-dimensional arrays of 2*2, as follows:

import numpy as np

result = np.array([[[1,2],[3,4]],[[11,12],[13,14]]],np.int)
print result
# [[[ 1  2]
#   [ 3  4]]
#
#  [[11 12]
#   [13 14]]]
print result[0]
# [[1 2]
#  [3 4]]
print result[1]
# [[11 12]
#  [13 14]]

After understanding the construction method of ndarry, learn the member variables and member functions of ndarry to get its basic information and values.
3.ndarry member variables
With shape s, you can get the number of rows and columns for a result as follows:

import numpy as np
result=np.array([[1,2,3,4],[5,6,7,8]],dtype=int)
print result.shape
# (2L, 4L)

Using dtype, you can get member variables for result as follows:

import numpy as np
result=np.array([[1,2,3,4],[5,6,7,8]],dtype=int)
print result.dtype
# int32

3. Access the values in the two-dimensional ndarry, using the following two-dimensional array as an example:
result =np.array([[1,2,3],[4,5,6]],np.int)
1. Get the values in row 2, column 3, such as

import numpy as np
result =np.array([[1,2,3],[4,5,6]],np.int)
print result[1,2]
#6
#Equal to the following
print result[1][2]
#6

2. Get the full value of the first row

import numpy as np
result =np.array([[1,2,3],[4,5,6]],np.int)
print result[0,:]
print result[0,::]
print result[0][::]
print result[0][:]
# [1 2 3]
# [1 2 3]
# [1 2 3]
# [1 2 3]

3. Get all the values in column 2

import numpy as np
result =np.array([[1,2,3],[4,5,6]],np.int)
print result[:,1]
print result[::,1]
# [2 5]
# [2 5]

4. Access the values in the three-dimensional ndarry, using the following three-dimensional arrays as an example:

result =np.array([[[1,1,1],[2,2,2],[3,3,3]],[[10,10,10],[20,20,20],[30,30,30]]],np.int)

1. Get the values of column 2 of all two-dimensional arrays in a three-dimensional array as follows:

import numpy as np
result =np.array([[[1,2,3],[4,5,6],[7,8,9]],[[11,12,13],[24,25,26],[37,38,39]]],np.int)
print result[:,:,1]
# [[ 2  5  8]
#  [12 25 38]]

2. Get all the values of the first two-dimensional array in a three-dimensional array

import numpy as np
result =np.array([[[1,2,3],[4,5,6],[7,8,9]],[[11,12,13],[24,25,26],[37,38,39]]],np.int)
print result[0,:,:]
# [[1 2 3]
#  [4 5 6]
#  [7 8 9]]

Topics: OpenCV Python