【问题标题】:Reformatting a numpy array重新格式化一个 numpy 数组
【发布时间】:2021-05-12 01:30:12
【问题描述】:

我遇到了一些代码(可能会回答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

【问题讨论】:

    标签: python python-3.x numpy numpy-ndarray


    【解决方案1】:

    我将首先创建并解释一个简化的示例,然后解释您指出的部分。

    首先,我们创建名为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的第一个数组包含的第一个数组中,选择第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 分配给索引21 处的元素,300 在索引@987654359 处@ 和 400 索引 23

    label_matrix[loc_i, loc_j, 24] = 1:

    label_matrix的第一个数组所包含的第一个数组中,将值1赋给索引24处的元素

    【讨论】:

      猜你喜欢
      • 2015-06-17
      • 1970-01-01
      • 1970-01-01
      • 2021-01-09
      • 1970-01-01
      • 2014-01-27
      • 2015-05-19
      • 1970-01-01
      • 2021-12-18
      相关资源
      最近更新 更多