简体   繁体   English

如何使用opencv python将二维numpy数组转换为轮廓

[英]how to convert 2d numpy array into contours using opencv python

i am trying to convert a 2d array into contours so that i can use drawcounter function of opencv .so if is there any way to convert 2d array into contours then please let suggest me any solution.我正在尝试将二维数组转换为轮廓,以便我可以使用 opencv 的 drawcounter 函数。所以如果有任何方法可以将二维数组转换为轮廓,那么请给我建议任何解决方案。

2d array:二维数组:

[[ 5.961938   -0.39692658 -0.02718444 ... -0.50327307  0.05710772
  -0.07550404]
 [ 5.9570045  -0.39401022 -0.0331709  ... -0.5008669   0.04731249
  -0.07948248]
 [ 5.9526873  -0.3914584  -0.03840905 ... -0.49876142  0.03874166
  -0.08296361]
 ...
 [ 6.7853484  -0.6472809  -0.22270808 ... -0.44317752 -0.18152599
  -0.35107702]
 [ 6.794132   -0.65010595 -0.22126621 ... -0.44905755 -0.17756268
  -0.3512323 ]
 [ 6.8041706  -0.6533345  -0.21961835 ... -0.4557776  -0.17303322
  -0.35140973]]

now i want to convert this array into contours .现在我想将此数组转换为轮廓。

let me show the code so that it will help to understand .让我显示代码,以便它有助​​于理解。 basically i am using self-human-parsing method on pre-trained model.基本上我在预训练模型上使用自我人类解析方法。 now i want to extract the contours of all classes of outputs.现在我想提取所有输出类别的轮廓。

output = model(image.cuda())
upsample = torch.nn.Upsample(size=input_size, mode='bilinear', 
              align_corners=True)
upsample_output = upsample(output[0][-1][0].unsqueeze(0))
upsample_output = upsample_output.squeeze()
upsample_output = upsample_output.permute(1, 2, 0)  # CHW -> HWC

logits_result = transform_logits(upsample_output.data.cpu().numpy(), c, s, w, h, input_size=input_size)
parsing_result = np.argmax(logits_result, axis=2)

i had used this code from github and i don't have any idea that what they are trying to do.我使用了来自 github 的这段代码,但我不知道他们想要做什么。 i need only the contours of all classes .我只需要所有类的轮廓。

I would use reshape function as mentioned in the comments我会使用评论中提到的重塑功能

    my_array = [[ 5.961938   -0.39692658 -0.02718444 ... -0.50327307  0.05710772
  -0.07550404]
 [ 5.9570045  -0.39401022 -0.0331709  ... -0.5008669   0.04731249
  -0.07948248]
 [ 5.9526873  -0.3914584  -0.03840905 ... -0.49876142  0.03874166
  -0.08296361]
 ...
 [ 6.7853484  -0.6472809  -0.22270808 ... -0.44317752 -0.18152599
  -0.35107702]
 [ 6.794132   -0.65010595 -0.22126621 ... -0.44905755 -0.17756268
  -0.3512323 ]
 [ 6.8041706  -0.6533345  -0.21961835 ... -0.4557776  -0.17303322
  -0.35140973]]

    #then create an opencv contour like follows

    contour = np.array(my_array).reshape(len(my_array), 1, 2)

I didnt test the code.我没有测试代码。 Note that contours' shape is (x, 1, 2) where x is length of your array请注意,轮廓的形状是 (x, 1, 2) 其中 x 是数组的长度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM