Creating a labelmatrix from a non-binary matrix for regionprops
1 view (last 30 days)
Show older comments
Bryan Cannon
on 31 Mar 2022
Answered: Githin George
on 3 Nov 2023
I have an RGB image that I've imported into MATLAB using imread(), and I have code that has converted each cells value from RGB values into a single value (e.g. going from [1;2;1] to [34]). I'm using this to record unique labels for each cell, which correspond to labels of object's I've segmented previously. A small section of the matrix would like this:
0 0 0 52 52 52 0 0 0
0 0 52 52 31 31 31 0 0
0 52 52 0 0 31 31 31 31
0 0 0 0 0 31 31 0 0
I'm currently trying to run regionprops on the matrix with single values, thinking it would automatically interpret the matrix as a labelmatrix. This doesn't seem to be the case though as the 'label' property is not present in the output. I have areas in my matrix where objects are touching each other, and thus converting the single matrix to binary and running regionprops on that will lead to unwanted merging of objects.
Is there a way to get regionprops to recongize my matrix as a labelmatrix as-is? Or at least convert my matrix into a labelmatrix?
0 Comments
Accepted Answer
Githin George
on 3 Nov 2023
Hello,
I understand you are having a matrix with unique values of labels for each point and would like to use this matrix as a ‘labelmatrix’ for running the “regionprops” function.
A ‘single’ valued labelmatrix is a supported argument for the “regionprops” function but the label property will be present in the output only if the input matrix L is a ‘categorical’ labelmatrix as described in the MATLAB documentation for “regionprops” function.
As a workaround, you could modify the following code snippet to add a custom label property to the “regionprops” output for each label or convert your matrix into a ‘categorical’ based on the label information you already have.
% let L be your labelMatrix
lVal = unique(L);
baseMask = zeros(size(L));
regProps = {};
for ii=2:numel(unique(lVal))
idx = find(L == lVal(ii));
MaskTemp = baseMask;
MaskTemp(idx) = 1;
% You could add logic for custom labeling Here
regProps{ii-1} = regionprops(MaskTemp,"basic");
%
end
I hope this helps.
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!