numpy.ndarray.astype
https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.astype.html
method
ndarray.astype(dtype, order='K', casting='unsafe', subok=True, copy=True)
Copy of the array, cast to a specified type.
数组的副本,强制转换为指定的类型。
1. Parameters
dtype : str or dtype
Typecode or data-type to which the array is cast.
数组转换为的 typecode or data-type。
order : {‘C’, ‘F’, ‘A’, ‘K’}, optional
Controls the memory layout order of the result. ‘C’ means C order, ‘F’ means Fortran order, ‘A’ means ‘F’ order if all the arrays are Fortran contiguous, ‘C’ order otherwise, and ‘K’ means as close to the order the array elements appear in memory as possible. Default is ‘K’.
控制结果的内存布局顺序。‘C’ 表示 C 顺序,‘F’ 表示 Fortran 顺序,‘A’ 表示 ‘F’ 顺序 (if all the arrays are Fortran contiguous),否则 ‘C’ 顺序,‘K’ 表示与数组元素出现的顺序接近在内存中的顺序。Default is ‘K’.
contiguous [kənˈtɪɡjuəs]:adj. 连续的,邻近的,接触的
casting : {‘no’, ‘equiv’, ‘safe’, ‘same_kind’, ‘unsafe’}, optional
Controls what kind of data casting may occur. Defaults to ‘unsafe’ for backwards compatibility.
控制可能发生的数据类型转换。向后兼容,默认为 ‘unsafe’。
‘no’ means the data types should not be cast at all.
‘equiv’ means only byte-order changes are allowed.
‘safe’ means only casts which can preserve values are allowed.
‘same_kind’ means only safe casts or casts within a kind, like float64 to float32, are allowed.
‘unsafe’ means any data conversions may be done.
preserve [prɪˈzɜːv]:vt. 保存,保护,维持,腌,禁猎 n. 保护区,禁猎地,加工成的食品,(某人或群体的) 专门活动
subok : bool, optional
If True, then sub-classes will be passed-through (default), otherwise the returned array will be forced to be a base-class array.
如果为 True,则子类将被传递 (默认),否则,返回的数组将被强制为基类数组。
force [fɔːs]:n. 力量,武力,魄力 vt. 促使,推动,强迫,强加
copy : bool, optional
By default, astype always returns a newly allocated array. If this is set to false, and the dtype, order, and subok requirements are satisfied, the input array is returned instead of a copy.
默认情况下,astype 始终返回新分配的数组。如果将其设置为 false,并且满足 dtype, order, and subok 要求,则返回输入数组,而不是副本。
2. Returns:
arr_t : ndarray
Unless copy is False and the other conditions for returning the input array are satisfied (see description for copy input parameter), arr_t is a new array of the same shape as the input array, with dtype, order given by dtype, order.
除非 copy 为 False 并且满足返回输入数组的其他条件 (请参见复制输入参数的说明),否则 arr_t 是形状与输入数组相同的新数组,其类型为 dtype,由 dtype 指定的顺序为 order。
3. Raises
ComplexWarning
When casting from complex to float or int. To avoid this, one should use a.real.astype(t)
.
当从复数转换为浮点数或整数时。为了避免这种情况,应该使用 a.real.astype(t)
。
4. Examples
>>> x = np.array([1, 2, 2.5])
>>> x
array([1. , 2. , 2.5])
>>>
>>> x.astype(int)
array([1, 2, 2])
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Yongqiang Cheng
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
current_directory = os.path.dirname(os.path.abspath(__file__))
import numpy as np
# import tensorflow as tf
import cv2
import time
print(16 * "++--")
print("current_directory:", current_directory)
PIXEL_MEAN = [123.68, 116.779, 103.939] # R, G, B. In TensorFlow, channel is RGB. In OpenCV, channel is BGR.
print("Python list")
print("PIXEL_MEAN:", PIXEL_MEAN)
print("type(PIXEL_MEAN):", type(PIXEL_MEAN))
print("type(PIXEL_MEAN[0]):", type(PIXEL_MEAN[0]), "\n")
PIXEL_MEAN_array = np.array(PIXEL_MEAN)
print("NumPy array")
print("PIXEL_MEAN_array:", PIXEL_MEAN_array)
print("type(PIXEL_MEAN_array):", type(PIXEL_MEAN_array))
print("type(PIXEL_MEAN_array[0]):", type(PIXEL_MEAN_array[0]))
print("PIXEL_MEAN_array.dtype:", PIXEL_MEAN_array.dtype, "\n")
image_array = np.array(
[[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]], [[21, 22, 23], [24, 25, 26], [27, 28, 29], [30, 31, 32]]])
print("image_array:", image_array)
print("type(image_array):", type(image_array))
print("type(image_array[0]):", type(image_array[0]))
print("image_array.dtype:", image_array.dtype, "\n")
image_array_fusion = image_array + np.array(PIXEL_MEAN)
print("image_array_fusion:", image_array_fusion)
print("type(image_array_fusion):", type(image_array_fusion))
print("type(image_array_fusion[0]):", type(image_array_fusion[0]))
print("image_array_fusion.dtype:", image_array_fusion.dtype, "\n")
image_array_fusion_int64 = image_array_fusion.astype(np.int64)
print("image_array_fusion_int64:", image_array_fusion_int64)
print("type(image_array_fusion_int64):", type(image_array_fusion_int64))
print("type(image_array_fusion_int64[0]):", type(image_array_fusion_int64[0]))
print("image_array_fusion_int64.dtype:", image_array_fusion_int64.dtype, "\n")
image_array_fusion_int32 = image_array_fusion.astype(np.int32)
print("image_array_fusion_int32:", image_array_fusion_int32)
print("type(image_array_fusion_int32):", type(image_array_fusion_int32))
print("type(image_array_fusion_int32[0]):", type(image_array_fusion_int32[0]))
print("image_array_fusion_int32.dtype:", image_array_fusion_int32.dtype, "\n")
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py --gpu=0
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
Python list
PIXEL_MEAN: [123.68, 116.779, 103.939]
type(PIXEL_MEAN): <type 'list'>
type(PIXEL_MEAN[0]): <type 'float'>
NumPy array
PIXEL_MEAN_array: [123.68 116.779 103.939]
type(PIXEL_MEAN_array): <type 'numpy.ndarray'>
type(PIXEL_MEAN_array[0]): <type 'numpy.float64'>
PIXEL_MEAN_array.dtype: float64
image_array: [[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
[[21 22 23]
[24 25 26]
[27 28 29]
[30 31 32]]]
type(image_array): <type 'numpy.ndarray'>
type(image_array[0]): <type 'numpy.ndarray'>
image_array.dtype: int64
image_array_fusion: [[[124.68 118.779 106.939]
[127.68 121.779 109.939]
[130.68 124.779 112.939]
[133.68 127.779 115.939]]
[[144.68 138.779 126.939]
[147.68 141.779 129.939]
[150.68 144.779 132.939]
[153.68 147.779 135.939]]]
type(image_array_fusion): <type 'numpy.ndarray'>
type(image_array_fusion[0]): <type 'numpy.ndarray'>
image_array_fusion.dtype: float64
image_array_fusion_int64: [[[124 118 106]
[127 121 109]
[130 124 112]
[133 127 115]]
[[144 138 126]
[147 141 129]
[150 144 132]
[153 147 135]]]
type(image_array_fusion_int64): <type 'numpy.ndarray'>
type(image_array_fusion_int64[0]): <type 'numpy.ndarray'>
image_array_fusion_int64.dtype: int64
image_array_fusion_int32: [[[124 118 106]
[127 121 109]
[130 124 112]
[133 127 115]]
[[144 138 126]
[147 141 129]
[150 144 132]
[153 147 135]]]
type(image_array_fusion_int32): <type 'numpy.ndarray'>
type(image_array_fusion_int32[0]): <type 'numpy.ndarray'>
image_array_fusion_int32.dtype: int32
Process finished with exit code 0