如何将一个热表示转换为稀疏表示

2024-06-16 14:01:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个语义分段项目中工作,我有3 classes。我将图像注释到每个类的带有标签的地面真相面具中,并将它们转换成lif文件(绘制多边形并标记)

下面是如何从lif文件中提取地面真相面具的代码

def get_mask(self,image,shapes):
  if not no_mask:
  contours=[[] for i in range(self.data_general_params['label_shape'][2]] #based on number of classes
  if self.labellist=None:
     labellist=[]
  else:
     labellist=self.labellist
  stack=[]
#shapes is a variable that reads the lif file information such as labelname its x,y points etc in a list of tuple
  for i,shape in enumerate(shapes):
    if shape[0]=='plasticswelling':
      if not shape[0] in labellist:
          labellist.append(shape[0])
      shape2draw=[[x[0] % image.shape[1],x[1]] for x in shape[1]]
      contours[labellist.index('plasticswelling')].append(np.array(shape2draw,dtype=np.int32).reshape(-1,1,2))
    if shape[0]=='wirebreak':
      if not shape[0] in labellist:
          labellist.append(shape[0])
      shape2draw=[[x[0] % image.shape[1],x[1]] for x in shape[1]]
      contours[labellist.index('wirebreak')].append(np.array(shape2draw,dtype=np.int32).reshape(-1,1,2))
    if shape[0]=='whitemarks':
      if not shape[0] in labellist:
          labellist.append(shape[0])
      shape2draw=[[x[0] % image.shape[1],x[1]] for x in shape[1]]
      contours[labellist.index('whitemarks')].append(np.array(shape2draw,dtype=np.int32).reshape(-1,1,2))
    for i in (range(3)):
      lab=np.zeros(list(image.shape[:2]+[1],dtype=np.uint32)
      stack.append(cv2.drawCountours(lab[:,:,:1],contours[i],-1,1,-1).astype(np.float32))
    lab=np.concatenate(tuple([st for st in stack]),axis=2)
return image,lab #lab is groundtruth-mask

图像的形状返回(128,128,1)和掩码的形状(128,128,3)

输入图像为灰度图像,遮罩形状基于第三维中的类数

但是,随着我的类数量的增加,由于一个热编码,它导致了内存问题。 所以我想把它转换成apt,表示稀疏的\u分类的\u熵。 我想把它转换成整数目标,但我不知道怎么做

你知道这种图像数据的标签/遮罩应该是什么样子吗?或者我应该如何将它从一个热编码目标转换为整数目标


Tags: in图像imageselfforifnplab