【问题标题】:How to work with .mat file from BSDS500 dataset?如何使用 BSDS500 数据集中的 .mat 文件?
【发布时间】:2026-02-10 19:00:02
【问题描述】:

我想根据 BSDS500 数据集的 .mat 文件中的基本事实创建图像。我想将来自地面实况文件的图像与我的程序生成的图像进行比较。我阅读了内容,其中一个文件的输出是:

 [[array([[(array([[ 1,  1,  1, ...,  1,  1,  1],
       [ 1,  1,  1, ...,  1,  1,  1],
       [ 1,  1,  1, ...,  1,  1,  1],
       ...,
       [36, 36, 36, ..., 36, 36, 36],
       [36, 36, 36, ..., 36, 36, 36],
       [36, 36, 36, ..., 36, 36, 36]], dtype=uint16), array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ...,
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]], dtype=uint8))]],
      dtype=[('Segmentation', 'O'), ('Boundaries', 'O')])
  array([[(array([[ 1,  1,  1, ...,  1,  1,  1],
       [ 1,  1,  1, ...,  1,  1,  1],
       [ 1,  1,  1, ...,  1,  1,  1],
       ...,
       [11, 11, 11, ..., 11, 11, 11],
       [11, 11, 11, ..., 11, 11, 11],
       [11, 11, 11, ..., 11, 11, 11]], dtype=uint16), array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ...,
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]], dtype=uint8))]],
      dtype=[('Segmentation', 'O'), ('Boundaries', 'O')])
  array([[(array([[ 1,  1,  1, ...,  1,  1,  1],
       [ 1,  1,  1, ...,  1,  1,  1],
       [ 1,  1,  1, ...,  1,  1,  1],
       ...,
       [14, 14, 14, ..., 14, 14, 14],
       [14, 14, 14, ..., 14, 14, 14],
       [14, 14, 14, ..., 14, 14, 14]], dtype=uint16), array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ...,
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]], dtype=uint8))]],
      dtype=[('Segmentation', 'O'), ('Boundaries', 'O')])
  array([[(array([[1, 1, 1, ..., 1, 1, 1],
       [1, 1, 1, ..., 1, 1, 1],
       [1, 1, 1, ..., 1, 1, 1],
       ...,
       [6, 6, 6, ..., 6, 6, 6],
       [6, 6, 6, ..., 6, 6, 6],
       [6, 6, 6, ..., 6, 6, 6]], dtype=uint16), array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ...,
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]], dtype=uint8))]],
      dtype=[('Segmentation', 'O'), ('Boundaries', 'O')])
  array([[(array([[1, 1, 1, ..., 1, 1, 1],
       [1, 1, 1, ..., 1, 1, 1],
       [1, 1, 1, ..., 1, 1, 1],
       ...,
       [9, 9, 9, ..., 9, 9, 9],
       [9, 9, 9, ..., 9, 9, 9],
       [9, 9, 9, ..., 9, 9, 9]], dtype=uint16), array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ...,
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]], dtype=uint8))]],
      dtype=[('Segmentation', 'O'), ('Boundaries', 'O')])]]

我不知道如何在我的代码中使用它来检查我的分段程序是否运行良好。

【问题讨论】:

    标签: python matlab


    【解决方案1】:

    让我们加载 matfile:

    import io
    file = io.loadmat(matfilePath)
    

    file是一个字典,它的键'groundTruth'有相关信息。在下面的 sn-p 中,edges 是图像的轮廓,segmap 是分割图。边缘数组已标准化,因此您必须将它们乘以 255 才能获得图像。

    edges = file['groundTruth'][0][0][0][0][1]
    segmap = file['groundTruth'][0][0][0][0][0]
    segmap_uint8 = segmap_data.astype(np.uint8) # convert to uint8 to prevent lossy conversion when saving image
    
    edges_255 = edges * 255
    import imageio
    # save images
    imageio.imwrite(os.path.join(imagePath,'image_edges.jpg'), edges_255)
    imageio.imwrite(os.path.join(imagePath,'image_seg.jpg'), segmap_uint8)
    

    查看这些链接以获取完整代码。它们包含 python 和 matlab 中的 mat 文件处理脚本。 BSDS500 data processing (convert groundtruth to jpg format), Convert bsds500 data set PS:即使链接不可用,主要答案在上面的代码中。

    【讨论】:

      最近更新 更多