Find index of unique pixel values and assign a color to the image. Original image is a grayscale.

1 回表示 (過去 30 日間)
I have a grayscale image in which objects are represented as:
Object Pixel Value
Background 0
Class 1 1
Class 2 2
Class 3 3
Class 4 4
Class 5 5
There are no other pixel values except the 6 mentioned above. Background is essentially present, other objects may or may not be present.
The requirement is to assign specific colors to the objects
Object Pixel Value Color
Background 0 Black
Class 1 1 Red
Class 2 2 Green
Class 3 3 Blue
Class 4 4 White
Class 5 5 Pink
For an example, i am attaching an image with Background, Class 4 and Class 5

採用された回答

Walter Roberson
Walter Roberson 2022 年 7 月 29 日
cmap = [
0 0 0 %black
1 0 0 %red
0 1 0 %green
0 0 1 %blue
1 1 1 %white
1 0 1 %fuschia
];
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1081490/case_452_14.png';
grayimg = imread(filename);
imshow(grayimg, [])
rgbimg = ind2rgb(grayimg, cmap);
imshow(rgbimg)
  4 件のコメント
Rishi Raj
Rishi Raj 2022 年 7 月 29 日
ok.. png has to be saved with a different format.. the following code worked..
path=strcat('/Volumes/ESD-USB/_RISHI/TTV_png/Test/mask_visible/',name, '.png');
imwrite(grayImage,cmap, path);
Walter Roberson
Walter Roberson 2022 年 7 月 29 日
The problem you had with writing the rgbimage is that 'Quality' is not a valid option for png files.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by