python - 重新格式化一个 numpy 数组
问题描述
我遇到了一些代码(可能会回答我的这个问题)。这是代码(来自 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
解决方案
我将首先创建并解释一个简化的示例,然后解释您指出的部分。
首先,我们创建名为 的 ndarray 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.]]]
现在,我们将更改 的第一个数组包含的第一个数组的前 4 个元素的值label_matrix
。
要访问 的第一个数组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]
等等
所以,现在,我们将更改第一个数组包含的第一个数组的前 4 个元素的值label_matrix
:
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
,选择第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
index 处的元素cls
。(如果名为的变量cls
具有 value 4
,它将将该值分配1
给 的第一个数组包含的第一个数组的索引 4 处的元素label_matrix
)
label_matrix[loc_i, loc_j, 20:24] = [x, y, w, h]
:
说“x==100”、“y==200”、“w==300”和“h==400”。因此,在 的第一个数组包含的第一个数组中label_matrix
,将 value 分配给100
index 处的元素,将20
value分配给 index200
处的元素21
,300
在 index22
和400
index23
label_matrix[loc_i, loc_j, 24] = 1
:
在 的第一个数组所包含的第一个数组中label_matrix
,将值赋给1
索引处的元素24
推荐阅读
- webpack - 如何在没有警告的情况下正确安装 webpack?
- html - animation.done 调用了另一个元素
- java - 来自 MappingCassandraConverter 的 NPE
- c++ - 如何为下拉列表组合框 MFC 设置文本?
- php - libxml_disable_entity_loader(false) 用于 SoapClient 的错误,即“SOAP-ERROR: Parsing WSDL: Couldn't load from”
- laravel - Laravel 根据数据表中的值在模式弹出窗口中显示复选框状态
- delphi - 如何在 mORMot 上使用数据库存储过程?
- kotlin - Kotlin:使用基本身份验证和参数发布请求导致服务器错误
- html - 一个有效的 HTML 自定义元素可能包含两个连字符或破折号,例如。
? - java - PDFBox:ExtractImages JPEG2000 图像未提取