Unique RGB colors for segmentation
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
DGM
on 4 May 2024
Why would a label image have alpha? By what process were they generated? Maybe that would tell us why.
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
on 6 May 2024
Edited: Image Analyst
on 7 May 2024
L
on 6 May 2024
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?
L
on 7 May 2024
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
1 Comment
Josh
on 7 May 2024
bravo <3
Categories
Find more on Image Segmentation in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!