Creating NumPy Arrays

Familiarizing ourselves with the power of NumPy arrays allows us to easily manipulate images, since OpenCV uses NumPy arrays to store images. The operations covered in this lesson are only a small fraction of those available to you in the NumPy library. LinkedIn Learning provides several courses on NumPy, including this one, which I used as a refresher to prepare for this lecture.

NumPy arrays are similar to Python lists, though there are some notable differences. The size of a NumPy array must be specified at creation and cannot be changed. Furthermore, all elements of a NumPy array, unlike a list, must be of the same data type. What NumPy loses in flexibility, it gains in speed. NumPy arrays are much smaller than a list, when comparing arrays and list containing the same number of elements. NumPy arrays also support vectorization, which allows element-wise operations to occur without the use of a for-loop!

Specifying values of an Arrays

For the majority of this course, we will read in images from a file. Thus, most of the NumPy arrays that we are manipulating will be created when the image is read. However, there are a few instances where we will need to create a new array, such as when we apply kernel operators or pad an image (we will cover these very soon!).

The first method for creating a new array is to specify all of the elements of the array directly. Below are examples of one-dimensional and two-dimensional arrays.

arr1 = np.array([1,3,5,7,9])
arr2 = np.array([[1.0,3.1,5.2,7.1,9.8],[2.1,4.3,6.5,8.7,10.9]])
Uniformly Spaced Arrays

Similar to the range class Python, NumPy’s np.arange allows you to create an array specifying the first element, last element, and step size. A similar function is np.linspace. This creates an array of evenly spaced values. The arguments to this function are the first element, last element, and the number of points to be created over the range. Examples of both are shown below.

arr1 = np.arange(10,20,0.5)
arr2 = np.linspace(10,20,30)
Constant-Value Arrays

Let’s look at four (4) approaches for creating arrays filled with the same value. The first is np.zeros. As you probably guessed, this fills an entire array with zeros. The function needs to know the size of the array, which can be an integer for a one-dimensional array or a tuple when creating a multi-dimensional array. Similarly, NumPy’s np.ones creates an array filled with ones. The usage of the two is exactly the same.

arr1 = np.zeros(10)
arr2 = np.zeros((4,5))
arr3 = np.ones(12)

To create an array filled with values other than zeros and ones, use the np.full method. This function requires the size of the array, given as a tuple, and value for which the array should be filled.

arr1 = np.full( (4,5), 255)

If an array has already been created, you can change all of its its values to a constant value using an array’s fill method. This example changes all of the values in the array to 127.

arr2.fill(127)

Several other methods exist for creating arrays, such as np.random.rand and np.random.randint, though we are less likely to use those in this class.

Array Type, SIZE, and Shape

NumPy supports a wide range of data types. Given the input data, NumPy will pick an appropriate data type to store your data. However, you can also specify the data type at creation. For instance, OpenCV stores images in an unsigned 8-bit array. Therefore, in order to show a NumPy array as an image, it must be saved in this format. You can find out an array’s data type like so:

print(arr1.dtype)

Similarly, you can find out the dimensions of an array by viewing its shape.

print(arr1.shape)

Size will return the total number of elements in the array.

print(arr1.size)

To specific an array’s type at creation, you can include the argument dtype=TYPE. This works with any method that we have shown for creating an array. Here are a few examples:

arr1 = np.zeros( (4,5), dtype='uint8')
arr2 = np.full( (10,3), 300, dtype='int32')

Lastly, you may need to change the type of an array. Use the method np.ndarray.astype to change the data type.

arr2 = arr1.astype('float64')