How to unpack mat file frame in single image

Hi,

I have a matlab file where inside there are two matrices representing 60 frame videos.

The first matrix (“data_m”) is composed of 1200 x 120 x 60, the second matrix ("ground_truth) consists of k x 60 x 4, the two matrices are linked, that is the i-th frame (data_m) corresponds to the i-th column (ground_truth). I need to be able to unpack these matrices so that I can see them as 60 single images.
This is my dataloader:

 def __init__(self, snapshot_dir,transform):

        self.snapshot_dir = snapshot_dir
        self.transform = transform

 def __len__(self):

        return len([img for img in os.listdir(self.snapshot_dir) if img.endswith('.mat')]) * 60

 def __getitem__(self, idx):

        # image path
        snapshot = os.path.join(self.snapshot_dir, "snapshot_n_" + str(idx+1) + ".mat")

        matrix_TBD = scipy.io.loadmat(snapshot)
        data_matrix = matrix_TBD.get('data_matrix')
        ground_truth = matrix_TBD.get('ground_truth')
        frame_size = data_matrix.shape[2]

        for i in range(frame_size):

            img = data_matrix[:, :, i]
            target = ground_truth[:, i, :]

        sample = {'image': img,
                  'target': target}

        if self.transform:
            image, target = sample['image'], sample['target']
            return {'image': torch.tensor(image),
                    'target': torch.tensor(target)}

        return sample

I debug to see the result

dataset = TBD_Dataset(snapshot_dir=snapshot_dir, transform=True)

    # DEBUG: show all samples in GMM Dataset
    if debug_dataset:

        for i in range(len(dataset)):
            sample = dataset[i]
            print(i, ",",
                  "Image shape: ", sample['image'].shape, ",",
                  "target shape: ", sample['target'].shape)

I know this way I will only get the latest img and target but I can’t find a way to get the 60 images, can anyone help me?

1 Like

In your current code you are iterating the frames and use only the last one.
To select each frame you could load the data in the __init__ method (move the data_matrix = ... code there) and select the frame in __getitem__ by using the passed idx:

img = data_matrix[:, :, idx]

(same for the target).

2 Likes