Clear Filters
Clear Filters

Creating mask Datastore for Mask R-CNN

15 views (last 30 days)
I am working with 126 (size 512x512) grayscale images. Each image has atleast 10-100 object instances. I labeled polygon masks and bounding boxes with the use of the image labeler app. I exported the ground truth and separated the images and bbox in their own datastore. Now I am looking to create the imagedatastore for the masks by following the instructions on:
Create Datastore that Reads Data: Create an imageDatastore and specify a custom read function that returns mask data as a binary matrix
I went on to follow the steps of converting the polygons labels to maskstacks. The mask stacks I got were as expexted the mask (h,w,numobjects). For example one of my maststacks was a 512x512x39 logical .
How do I format these mask stacks into an image datastore to train the mask RCNN.

Accepted Answer

T.Nikhil kumar
T.Nikhil kumar on 12 Feb 2024
Hi Bryce,
According to the documentation you’re following, the next step for you is to create an ‘imageDatastore’ that reads the mask data as a binary matrix.
I would suggest you to try this possible solution for the same.
1. After converting polygon labels into binary masks, you can save each mask stack in a .mat file with a suitable name (For eg: maskStack1.mat for the first image). You can put all these individual mask stack files in a ‘MaskStackFolder’ folder.
2. Now, define a custom read function that should take a file path as input and return the binary mask stack in the expected format. Refer to the following code snippet:
function mask = customReadFcn(filename)
% Load the data from the .mat file
loadedData = load(filename);
% Assuming the variable containing the mask stack is named 'maskStack'
mask = loadedData.maskStack;
% Ensure you are using correct variable name as per your naming
end
3. You can now create an ‘imageDatastore’ with this folder and custom read function.
maskFolder = "C:\MATLAB\MaskStackFolder"; % Replace with the actual path to your mask stack folder
% Create an imageDatastore for the mask stacks
masksDatastore = imageDatastore(maskFolder, 'ReadFcn', @customReadFcn);
Ensure that the order of the mask stacks in the ‘imageDatastore’ corresponds to the actual order of the images in the previously create image datastore for images.
I hope this answer is able to help you!
  1 Comment
Bryce
Bryce on 14 Feb 2024
Edited: Bryce on 14 Feb 2024
Hey T.Nikhil kumar,
Thanks for your answer. This problem has been a road block in my work so I am thankful for your answer. Unfortunately I tried the option and got this error.
Error using imageDatastore
Input folders or files contain non-standard file extensions.
Use FileExtensions Name-Value pair to include the non-standard file extensions.
I tried using the input argumet for imagedatastore ("FileExtenstions",".mat") to attempt to resolve the error and it worked so far. Hopefully I don't run into any other problems :). I am still curious why they did not put this in the instuctions/website

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!