重新格式化一个 numpy 数组
Reformatting a numpy array
我遇到了一些代码(可能会回答我的 this 问题)。这是代码(来自 Vivek Maskara 对我的问题的解决方案):
import cv2 as cv
import numpy as np
def read(image_path, label):
image = cv.imread(image_path)
image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
image_h, image_w = image.shape[0:2]
image = cv.resize(image, (448, 448))
image = image / 255.
label_matrix = np.zeros([7, 7, 30])
for l in label:
l = l.split(',')
l = np.array(l, dtype=np.int)
xmin = l[0]
ymin = l[1]
xmax = l[2]
ymax = l[3]
cls = l[4]
x = (xmin + xmax) / 2 / image_w
y = (ymin + ymax) / 2 / image_h
w = (xmax - xmin) / image_w
h = (ymax - ymin) / image_h
loc = [7 * x, 7 * y]
loc_i = int(loc[1])
loc_j = int(loc[0])
y = loc[1] - loc_i
x = loc[0] - loc_j
if label_matrix[loc_i, loc_j, 24] == 0:
label_matrix[loc_i, loc_j, cls] = 1
label_matrix[loc_i, loc_j, 20:24] = [x, y, w, h]
label_matrix[loc_i, loc_j, 24] = 1 # response
return image, label_matrix
您能否解释一下这部分代码的工作原理以及它的具体作用:
if label_matrix[loc_i, loc_j, 24] == 0:
label_matrix[loc_i, loc_j, cls] = 1
label_matrix[loc_i, loc_j, 20:24] = [x, y, w, h]
label_matrix[loc_i, loc_j, 24] = 1 # response
我将首先创建并解释一个简化的示例,然后再解释您指出的部分。
首先,我们创建名为 label_matrix
:
的 ndarray
import numpy as np
label_matrix = np.ones([2, 3, 4])
print(label_matrix)
此代码意味着您将获得一个包含 2 个数组的数组,这 2 个数组中的每个数组将包含 3 个数组,而这 3 个数组中的每个数组将包含 4 个元素。
因为我们使用了 np.ones
,所以所有这些元素的值都是 1
。
所以,打印 label_matrix
会输出这个:
[[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]]
现在,我们将更改label_matrix
的第一个数组包含的第一个数组的前4个元素的值。
要访问 label_matrix
的第一个数组,我们这样做:label_matrix[0]
要访问 label_matrix
的第一个数组包含的第一个数组,我们这样做:label_matrix[0, 0]
要访问 label_matrix
的第一个数组包含的第一个数组的第一个元素,我们这样做:label_matrix[0, 0, 0]
要访问 label_matrix
的第一个数组包含的第一个数组的第二个元素,我们这样做:label_matrix[0, 0, 1]
等等
所以,现在,我们将更改 label_matrix
的第一个数组包含的第一个数组的前 4 个元素的值:
label_matrix[0, 0, 0] = 100
label_matrix[0, 0, 1] = 200
label_matrix[0, 0, 2] = 300
label_matrix[0, 0, 2] = 400
label_matrix
的输出:
[[[100. 200. 300. 400.]
[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]]
[[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]]]
但我们可以这样写,而不是写 4 行代码:
label_matrix[0, 0, 0:4] = [100,200,300,400]
写作label_matrix[0, 0, 0:4]
表示:
在label_matrix
的第一个数组包含的第一个数组中,select 4个第一个元素(从索引0到4(不包括4))
所以现在你知道每一行的意思了。
我将解释您指出的代码部分:
if label_matrix[loc_i, loc_j, 24] == 0:
:
测试索引 24 处的元素(第 23 个元素)是否具有值 0
如果是,那么:
label_matrix[loc_i, loc_j, cls] = 1
:
将值 1
分配给索引 cls
处的元素。 (如果名为 cls
的变量的值为 4
,它会将值 1
赋给 label_matrix
的第一个数组包含的第一个数组的索引 4 处的元素。
label_matrix[loc_i, loc_j, 20:24] = [x, y, w, h]
:
说“x==100”、“y==200”、“w==300”和“h==400”。因此,在第一个数组 label_matrix
包含的第一个数组中,将值 100
分配给索引 20
处的元素,将值 200
分配给索引 [=46] 处的元素=]、300
在索引 22
和 400
到索引 23
label_matrix[loc_i, loc_j, 24] = 1
:
在label_matrix
的第一个数组包含的第一个数组中,将值1
赋值给索引24
处的元素
我遇到了一些代码(可能会回答我的 this 问题)。这是代码(来自 Vivek Maskara 对我的问题的解决方案):
import cv2 as cv
import numpy as np
def read(image_path, label):
image = cv.imread(image_path)
image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
image_h, image_w = image.shape[0:2]
image = cv.resize(image, (448, 448))
image = image / 255.
label_matrix = np.zeros([7, 7, 30])
for l in label:
l = l.split(',')
l = np.array(l, dtype=np.int)
xmin = l[0]
ymin = l[1]
xmax = l[2]
ymax = l[3]
cls = l[4]
x = (xmin + xmax) / 2 / image_w
y = (ymin + ymax) / 2 / image_h
w = (xmax - xmin) / image_w
h = (ymax - ymin) / image_h
loc = [7 * x, 7 * y]
loc_i = int(loc[1])
loc_j = int(loc[0])
y = loc[1] - loc_i
x = loc[0] - loc_j
if label_matrix[loc_i, loc_j, 24] == 0:
label_matrix[loc_i, loc_j, cls] = 1
label_matrix[loc_i, loc_j, 20:24] = [x, y, w, h]
label_matrix[loc_i, loc_j, 24] = 1 # response
return image, label_matrix
您能否解释一下这部分代码的工作原理以及它的具体作用:
if label_matrix[loc_i, loc_j, 24] == 0:
label_matrix[loc_i, loc_j, cls] = 1
label_matrix[loc_i, loc_j, 20:24] = [x, y, w, h]
label_matrix[loc_i, loc_j, 24] = 1 # response
我将首先创建并解释一个简化的示例,然后再解释您指出的部分。
首先,我们创建名为 label_matrix
:
import numpy as np
label_matrix = np.ones([2, 3, 4])
print(label_matrix)
此代码意味着您将获得一个包含 2 个数组的数组,这 2 个数组中的每个数组将包含 3 个数组,而这 3 个数组中的每个数组将包含 4 个元素。
因为我们使用了 np.ones
,所以所有这些元素的值都是 1
。
所以,打印 label_matrix
会输出这个:
[[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]]
现在,我们将更改label_matrix
的第一个数组包含的第一个数组的前4个元素的值。
要访问 label_matrix
的第一个数组,我们这样做:label_matrix[0]
要访问 label_matrix
的第一个数组包含的第一个数组,我们这样做:label_matrix[0, 0]
要访问 label_matrix
的第一个数组包含的第一个数组的第一个元素,我们这样做:label_matrix[0, 0, 0]
要访问 label_matrix
的第一个数组包含的第一个数组的第二个元素,我们这样做:label_matrix[0, 0, 1]
等等
所以,现在,我们将更改 label_matrix
的第一个数组包含的第一个数组的前 4 个元素的值:
label_matrix[0, 0, 0] = 100
label_matrix[0, 0, 1] = 200
label_matrix[0, 0, 2] = 300
label_matrix[0, 0, 2] = 400
label_matrix
的输出:
[[[100. 200. 300. 400.]
[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]]
[[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]]]
但我们可以这样写,而不是写 4 行代码:
label_matrix[0, 0, 0:4] = [100,200,300,400]
写作label_matrix[0, 0, 0:4]
表示:
在label_matrix
的第一个数组包含的第一个数组中,select 4个第一个元素(从索引0到4(不包括4))
所以现在你知道每一行的意思了。
我将解释您指出的代码部分:
if label_matrix[loc_i, loc_j, 24] == 0:
:
测试索引 24 处的元素(第 23 个元素)是否具有值 0
如果是,那么:
label_matrix[loc_i, loc_j, cls] = 1
:
将值 1
分配给索引 cls
处的元素。 (如果名为 cls
的变量的值为 4
,它会将值 1
赋给 label_matrix
的第一个数组包含的第一个数组的索引 4 处的元素。
label_matrix[loc_i, loc_j, 20:24] = [x, y, w, h]
:
说“x==100”、“y==200”、“w==300”和“h==400”。因此,在第一个数组 label_matrix
包含的第一个数组中,将值 100
分配给索引 20
处的元素,将值 200
分配给索引 [=46] 处的元素=]、300
在索引 22
和 400
到索引 23
label_matrix[loc_i, loc_j, 24] = 1
:
在label_matrix
的第一个数组包含的第一个数组中,将值1
赋值给索引24