为了账号安全,请及时绑定邮箱和手机立即绑定

我正在尝试将一些 python 代码转换为 C#,这个数组语法是什么?

我正在尝试将一些 python 代码转换为 C#,这个数组语法是什么?

喵喵时光机 2023-09-19 14:31:53
我正在尝试将 python 中的几行代码转换为 C#。在 C# 版本中,我对以 heatmap_center 变量开头的列表/数组语法感到困惑。在下面的片段中,heatmap_with_borders 变量的用途是什么?这是Python片段:def extract_keypoints(heatmap, all_keypoints, total_keypoint_num):    heatmap[heatmap < 0.1] = 0    heatmap_with_borders = np.pad(heatmap, [(2, 2), (2, 2)], mode='constant')    heatmap_center = heatmap_with_borders[1:heatmap_with_borders.shape[0]-1, 1:heatmap_with_borders.shape[1]-1]    heatmap_left = heatmap_with_borders[1:heatmap_with_borders.shape[0]-1, 2:heatmap_with_borders.shape[1]]    heatmap_right = heatmap_with_borders[1:heatmap_with_borders.shape[0]-1, 0:heatmap_with_borders.shape[1]-2]    heatmap_up = heatmap_with_borders[2:heatmap_with_borders.shape[0], 1:heatmap_with_borders.shape[1]-1]    heatmap_down = heatmap_with_borders[0:heatmap_with_borders.shape[0]-2, 1:heatmap_with_borders.shape[1]-1]    heatmap_peaks = (heatmap_center > heatmap_left) &\                    (heatmap_center > heatmap_right) &\                    (heatmap_center > heatmap_up) &\                    (heatmap_center > heatmap_down)    heatmap_peaks = heatmap_peaks[1:heatmap_center.shape[0]-1, 1:heatmap_center.shape[1]-1]    keypoints = list(zip(np.nonzero(heatmap_peaks)[1], np.nonzero(heatmap_peaks)[0]))  # (w, h)    keypoints = sorted(keypoints, key=itemgetter(0))    suppressed = np.zeros(len(keypoints), np.uint8)    keypoints_with_score_and_id = []    keypoint_num = 0    for i in range(len(keypoints)):        if suppressed[i]:            continue        for j in range(i+1, len(keypoints)):            if math.sqrt((keypoints[i][0] - keypoints[j][0]) ** 2 +                         (keypoints[i][1] - keypoints[j][1]) ** 2) < 6:                suppressed[j] = 1
查看完整描述

2 回答

?
ITMISS

TA贡献1871条经验 获得超8个赞

np.pad 似乎在整个热图周围添加了 2 个“像素”边框。因此,如果热图为 124x124,则结果将是 128x128 热图。

现在我们来简化一下

heatmap_center = heatmap_with_borders[1:heatmap_with_borders.shape[0]-1, 1:heatmap_with_borders.shape[1]-1]

我们将从

heatmap_with_borders[1:heatmap_with_borders.shape[0]-1

1:heatmap_with_borders.shape[0]-1表示将项目从1拉到维度-1的大小

在我们的 128,128 示例中,维度将为 128,所以我们说:从 1 到 127

具体如此简化

heatmap_center = heatmap_with_borders[1:127, 1:127]

因此,您只需跳过 2 像素边框的外部 1“像素”。在 C# 中,没有原生的“拉出列表的一部分”语法,因此您需要一个类似的方法

getSublist(heatmap, start1, end1, start2, end2)

所以你的电话看起来像

heatmap_center = getSublist(heatmap, 1, heatmap.Length - 1, 1, heatmap[0].Length -1)

我的建议是,如果您非常关心 C#,请自行实现此功能,以便在 C# 中轻松使用它,然后如果速度太慢,请找到具有类似功能的库。


查看完整回答
反对 回复 2023-09-19
?
不负相思意

TA贡献1777条经验 获得超10个赞

至于您的具体问题“在下面的片段中, heatmap_with_borders 变量的用途是什么?”

看起来它添加了 2 个“像素”边框。该算法似乎根据相邻像素制作某种热图。假设需要 1 像素偏移裁剪,然后查看中心裁剪和 1 像素裁剪哪个更大。因此,边框可以“填充”热图,从而使所有轻微的“作物”保持相同的大小和范围。


查看完整回答
反对 回复 2023-09-19
  • 2 回答
  • 0 关注
  • 61 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信