How to work with .mat file from BSDS500 dataset?

Viewed 416

I want to create the image from the ground truth found in the .mat files from BSDS500 dataset. I want to compare the image from ground truth file with the one generated by my program. I read the content and the output of one of the file is:

 [[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')])]]

I do not know how to use this in my code in order to check if my segmentation program perform well.

1 Answers

Let's load the matfile:

import io
file = io.loadmat(matfilePath)

file is a dictionnary, and its key 'groundTruth' has the relevant information. In the following snippet, edges has the contours of the image and segmap is the segmentation map. Edge arrays are normalized so you will have to multiply them by 255 to get your images.

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)

Check out these links for the complete code. They contain mat file processing scripts in python and matlab. BSDS500 data processing (convert groundtruth to jpg format), Convert bsds500 data set PS: even if the links are unavailable, the main answer is in the code above.

Related