Unique RGB colors for segmentation
8 views (last 30 days)
Show older comments
Hi , I have an image. This image was generated by a segmentation artifact. Each geometric region has a color.
I need to use this as the ground truth, so each geometric region should have one label. The way I can accomplish this is by considering each unique color as a distinct label. But some os the masks have some transparency.
When I load this image, there is a alpha variable, which I guess it has to do with transparency. I am not an image processing person but I don't know how to use this variable. Do I need this varibale to get each unique color? ANd how should I construct a disctionary so that I can check what class a particular pixel is from ?
6 Comments
Image Analyst
on 7 May 2024
How are you going to use it as the ground truth? If you're going to use the ground truth to train a new model of your own, then why? Why not just use SAM itself since you are agreeing that SAM produces 100% accurate segmentation?
Answers (1)
aditi bagora
on 7 May 2024
Hello,
I understand that the images includes four channels, namely RGBA, where the 'A' stands for the alpha channel that indicates transparency.
The segmentation images you have provided all have a uniform alpha value of 255, indicating that they are fully opaque. This means the transparency level across the entire image is consistent.
Therefore, you can proceed by constructing a dictionary where each unique RGB triplet is associated with a specific label, without the need to consider variations in the alpha channel.
Please refer to the following code to create a map of the values:
% Load the image and its alpha channel
[imageData, ~, alphaChannel] = imread('mask_31b.png');
% Check if the image is in RGB format, if not convert it
if size(imageData, 3) ~= 3
error('Image must be RGB');
end
% Preallocate a map object for color to label mapping
colorToLabelMap = containers.Map('KeyType', 'char', 'ValueType', 'double');
currentLabel = 1;
% Iterate over each pixel in the imageDataRGB
[rows, cols, ~] = size(imageData);
for row = 1:rows
for col = 1:cols
rgb = squeeze(imageData(row, col, :))';
% Convert RGB values to a string key (or any other unique identifier)
key = mat2str(rgb);
% Check if this color is already in the map
if ~isKey(colorToLabelMap, key)
% Assign a new label to this unique color
colorToLabelMap(key) = currentLabel;
currentLabel = currentLabel + 1;
end
end
end
% At this point, colorToLabelMap contains a mapping from each unique color to a label
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!