Unique RGB colors for segmentation

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

Why would a label image have alpha? By what process were they generated? Maybe that would tell us why.
Josh
Josh on 4 May 2024
Edited: Josh on 6 May 2024
trying to better understand your question by breaking down your task into smaller pieces...
it sounds like step 1 is that you want to extract all colors from the image into some kind of list/database/dictionary?
...however, there's the added issue of some images/masks having a transparency value that also needs to be accounted for? perhaps because some images have the same original color with different transparency values and you don't want those to be categorized separately?
am i understanding the task in front of you?
L
L on 6 May 2024
Edited: Image Analyst on 7 May 2024
@DGM it was generated using the "Segment Anything Model" (SAM) from Meta
@Josh You got it
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?
Did you see the answer from @aditi bagora below?
Hi @Image Analyst. I am devising a new method for segmentation. I am using as a ground truth just to initially tune the parameters, then the fine tuning will be done by other methods. I saw the answer.. very good and I will accept it

Sign in to comment.

Answers (1)

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

Products

Asked:

L
L
on 4 May 2024

Commented:

L
L
on 7 May 2024

Community Treasure Hunt

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

Start Hunting!