如何在numpy数组中正确使用切片?

xfyts7mz  于 2023-08-05  发布在  其他
关注(0)|答案(2)|浏览(71)

我有一段代码,它必须根据一组坐标和置信度在图像的几个像素上生成高斯分布。

import numpy as np
def gen_single_joint_heatmap(x, y, conf, img_size, sigma=0.6, eps=1e-6):
heatmap = np.zeros(img_size)  # Outside the gaussians all values should be zero

for center_x, center_y, confidence in zip(x, y, conf):  # center of the gaussians and confidences of the centers
    if confidence < eps:
        continue
    
    # The centers are scaled between 0 and 1, so multiply by width and height to obtain the real coordinates
    center_x *= img_size[0]
    center_y *= img_size[1]

    # creating a range in which the gaussian is computed
    start_x = max(0, int(center_x - 3 * sigma))
    end_x = min(img_size[0], int(center_x + 3 * sigma))  # sigma is set to 0.6

    start_y = max(0, int(center_y - 3 * sigma))
    end_y = min(img_size[1], int(center_y + 3 * sigma))

    x = np.arange(start_x, end_x, 1, dtype=np.float32)
    y = np.arange(start_y, end_y, 1, dtype=np.float32)
    
    # if the x and y are 0, then continue
    if not (len(x) and len(y)):
        continue

    y = y[:, None]
    
    # Compute the gaussian
    patch = np.exp(-(x - center_x) ** 2 + (y - center_y) ** 2 / 2 / sigma ** 2)
    patch *= confidence

    print('x_length', end_x - start_x, end_y - start_y)
    print(heatmap[start_y:end_y, start_x:end_x].shape)
    # save the patch if it's higher than values of the heatmap
    heatmap[start_y:end_y, start_x:end_x] = np.maximum(heatmap[start_y:end_y, start_x:end_x], patch)
return heatmap

字符串
为此,我使用了这段代码,但由于某种原因,它不起作用。问题显然出在最后一个作业上。不幸的是,我得到了这个错误:operands could not be broadcast together with shapes (3,0) (3,3),但是如果我打印长度(以print('x_length', end_x - start_x, end_y - start_y)为单位),我得到的输出是x_length 3 3,而热图的形状是(3, 0)。有谁知道我为什么会出现这个错误吗?
编辑:
根据要求,阵列的形状为:完整的heatmap为1920 x1080,除以整数比例因子,而patch(通常)是3x 3形状的阵列,但有时由于整数舍入而可能更大。关于@hpaulj提到的问题,我认为可能是这样的,但由于某种原因end_x - start_x是3,但维度仍然是0。

Traceback (most recent call last):
  File "/home/samuel/Desktop/internship_implementation/main.py", line 238, in <module>
    main()
  File "/home/samuel/Desktop/internship_implementation/main.py", line 190, in main
    dataset = dataset[0]
  File "/home/samuel/Desktop/internship_implementation/data/pc3dDataset.py", line 60, in __getitem__
    frames = full_preprocess(full_path, data, self.sample_number, self.image_padding, self.channels,
  File "/home/samuel/Desktop/internship_implementation/pipelines/preprocess.py", line 17, in full_preprocess
    data = generate_heatmap(data, image_padding, img_size, channels, scale_factor, epsilon, sigma)
  File "/home/samuel/Desktop/internship_implementation/pipelines/preprocessing/keypoints_heatmap_related.py", line 241, in generate_heatmap
    frames_heatmap[i] = gen_frame_heatmap(rearranged_skeletons[i], new_img_size, epsilon, sigma)
  File "/home/samuel/Desktop/internship_implementation/pipelines/preprocessing/keypoints_heatmap_related.py", line 222, in gen_frame_heatmap
    new_skeletons[channel] = gen_single_joint_heatmap(skeleton[i], skeleton[i + 1], skeleton[i + 2],
  File "/home/samuel/Desktop/internship_implementation/pipelines/preprocessing/keypoints_heatmap_related.py", line 213, in gen_single_joint_heatmap
    heatmap[start_y:end_y, start_x:end_x] = np.maximum(heatmap[start_y:end_y, start_x:end_x], patch)
ValueError: operands could not be broadcast together with shapes (3,0) (3,3)

pkmbmrz7

pkmbmrz71#

这可能是因为你的x和y输入本身就是1D数组,因此它们的形状被广播到你的'patch'变量,最终是1D形状(len(x),)。
举例来说:

import numpy as np

x = np.arange(0, 10, 1)
y = np.arange(0, 10, 1)

center_x, center_y = 2, 2
sigma = 0.2

# Compute the gaussian
patch = np.exp(-(x - center_x) ** 2 + (y - center_y) ** 2 / 2 / sigma ** 2)

print(patch.shape) # outputs (10,)

字符串
您应该使用np.meshgrid将1D数组转换为2D数组:

import numpy as np

x = np.arange(0, 10, 1)
y = np.arange(0, 10, 1)

x, y = np.meshgrid(x, y) # Here

center_x, center_y = 2, 2
sigma = 0.2

# Compute the gaussian
patch = np.exp(-(x - center_x) ** 2 + (y - center_y) ** 2 / 2 / sigma ** 2)

print(patch.shape) # Outputs (10, 10)

brgchamk

brgchamk2#

我想看到更多的信息,数组形状,完整的追溯等,但我的猜测是,有问题在于:

np.maximum(heatmap[start_y:end_y, start_x:end_x], patch)

字符串
具体来说

heatmap[start_y:end_y, start_x:end_x]


形状为(3,0)。这是因为start_x已经在数组的末尾,而start_x:end_x从数组的末尾“步进”出来,导致大小为0的维度。

相关问题