Clear Filters
Clear Filters

Label array from workspace in Image Labeler

2 views (last 30 days)
Sam Polk
Sam Polk on 30 Apr 2024
Answered: recent works on 30 Apr 2024
I would like to label the entries of an array Z using MATLAB's imageLabeler. Specifically, Z is an MxN double matrix that I can visualize as an image by calling imagesc(Z). As an end goal, I would like an MxN array G such that G(i,j) = k indicates that the (i,j)-entry of Z is class k.
My main questions are as follows:
  1. How can I format Z to be suitable for loading into imageLabeler? I understand that imageLabeler can be applied to any image that is loaded using imread, but I'm not sure how to apply it to a double array.
  2. Once I've obtained my ground truth labels using imageLabeler, how can I convert the groundTruth object into an array G of the form described above?
Thank you very much for your help.

Answers (1)

recent works
recent works on 30 Apr 2024
To use MATLAB's Image Labeler with a matrix Z, you can convert it into a grayscale image and then load it into the Image Labeler tool. Afterwards, you can convert the labeled regions back into an array G with the corresponding class labels. Here's a step-by-step guide:
  1. Format Z for Image Labeler: Convert your Z matrix into a grayscale image suitable for Image Labeler. You can achieve this by scaling Z to the range [0, 255] and converting it to uint8 format.
Z_scaled = mat2gray(Z) * 255; % Scale Z to [0, 255]
Z_uint8 = uint8(Z_scaled); % Convert to uint8
Now, you can save this as an image file and load it into the Image Labeler tool. You can use imwrite to save it as a PNG, JPEG, or any other format supported by Image Labeler.
imwrite(Z_uint8, 'Z_image.png'); % Save as an image file
2. Labeling with Image Labeler: Open MATLAB's Image Labeler tool and load the image file Z_image.png you just created. You can then label the regions in the image using the tool.
3. Convert Ground Truth Labels to Array G: Once you've labeled the regions in Image Labeler, you can export the labeled data as a groundTruth object in MATLAB. Then, you can convert this object into an array G with the desired format.
% Export ground truth labels from Image Labeler
groundTruthData = groundTruth('Z_image.png', labels);
% Convert to array G
G = zeros(size(Z)); % Initialize G with zeros
for i = 1:numel(groundTruthData.LabelData)
labelData = groundTruthData.LabelData(i);
label = labelData{1}; % Extract label
region = labelData{2}; % Extract region coordinates
G(region) = label; % Assign label to corresponding region in G
end
In this code, labels should be a cell array containing the labels assigned to each region in Image Labeler.
Now, G will be an array where G(i,j) = k indicates that the (i,j) entry of Z is in class k, as you desired.

Community Treasure Hunt

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

Start Hunting!