cell配列に格納さ​れているクラス名によ​って色を変えて画像を​表示するにはどうすれ​ばよいですか?

3 views (last 30 days)
Natsuo OKADA
Natsuo OKADA on 20 Oct 2023
Commented: Natsuo OKADA on 23 Oct 2023
predicted_imageは、読み込んだ画像の各ピクセルごとに機械学習で判定したクラス名が格納されてる512×512 cell配列です。
クラス名ごとに色を変えて表示したいのですが、どのように行えばよいでしょうか?
例えば、犬として判定されたピクセルを赤色、猫として判定されたピクセルを青色として凡例とともに表示したいです。
% 予測結果を画像データに変換する。
predicted_image = reshape(yfit, size_array(1), size_array(2));
% セル配列内のユニークなクラス名を取得
unique_classes = unique(yfit);
% クラスの数を計算
num_classes = numel(unique_classes);
% クラスごとの色を生成
class_colors = jet(num_classes); % 例としてJet colormapを使用
% クラスごとに色を変えて画像として表示

Accepted Answer

Hiroshi Iwamura
Hiroshi Iwamura on 20 Oct 2023
It's an alternative response.
別のやり方でやってみました。
最初にテスト用の cell 配列を作っています。
オプションで、reorderFlag = true; にすると、頻度順に並びます。
その必要がない場合は、reorderFlag = false; とし、最後の行は
c.TickLabels = names; でもどっちでも大丈夫です。
% make test cell array
N = 4;
mapping = {'neko', 'inu', 'saru', 'kiji'};
randomData = randi([0, size(mapping,2)-1], N, N);
a = cellfun(@(x) mapping{x + 1}, num2cell(randomData), 'UniformOutput', false)
a = 4×4 cell array
{'inu' } {'inu' } {'kiji'} {'inu' } {'neko'} {'neko'} {'kiji'} {'neko'} {'inu' } {'inu' } {'neko'} {'neko'} {'saru'} {'neko'} {'saru'} {'saru'}
%% using a-array only
names = unique(a)
names = 4×1 cell array
{'inu' } {'kiji'} {'neko'} {'saru'}
cnum = size(names, 1) % category size
cnum = 4
vals = (1:cnum)'; % index numbers
reorderFlag = true;
if (reorderFlag) % reorder using histogram count if you want to
catHist = histogram(categorical(a),DisplayOrder='ascend');
d = dictionary(string(catHist.Categories),vals') % mapping values to each categry
else
d = dictionary(string(names),vals) % mapping values to each categry
end
d = dictionary (string --> double) with 4 entries: "kiji" --> 1 "saru" --> 2 "inu" --> 3 "neko" --> 4
b = cellfun(@(x) d(x), a, UniformOutput=false) % swapping categories with values
b = 4×4 cell array
{[3]} {[3]} {[1]} {[3]} {[4]} {[4]} {[1]} {[4]} {[3]} {[3]} {[4]} {[4]} {[2]} {[4]} {[2]} {[2]}
I = cell2mat(b);
I = imresize(I,128,"nearest"); % for easy to view
cmap = jet(cnum);
imshow(I,cmap)
colormap(cmap)
c = colorbar;
c.Ticks = ((1:cnum) + 0.5);
c.TickLabels = d.keys;
  3 Comments
Akira Agata
Akira Agata on 23 Oct 2023
+1
別のやり方として、クラス名の配列をカテゴリカル配列に変換した後、label2rgb を使うという方法もあります。
% make test cell array
mapping = {'neko', 'inu'; 'saru', 'kiji'};
predImg = repelem(mapping, 10, 10);
% カテゴリカル配列に変換
predCat = categorical(predImg);
% label2rgbでRGB画像に変換
I = label2rgb(predCat);
% 確認
imshow(I)
Natsuo OKADA
Natsuo OKADA on 23 Oct 2023
このようにもできるのですね!勉強になります。コメントしていただきありがとうございます。

Sign in to comment.

More Answers (1)

Dyuman Joshi
Dyuman Joshi on 20 Oct 2023
%colors
r = [1 0 0];
b = [0 0 1];
%Random data
in = {'inu', 'neko'};
idx = randi(2,512,512);
in = in(idx);
%% If the data inside cell array is charactar array, use strcmp()
%here I have taken the data to be character array
out=strcmp(in,'inu');
%% If the data inside cell array is categorical array, use ==
%out = in==categorical('inu');
%red for inu, blue for neko
%1x1x3 for colored image
img = out.*reshape(r,1,1,3)+(~out.*reshape(b,1,1,3));
image(img)
Adding legends corresponding to a image is not possible. The workaround is to plot NaN data and use them as legend -
hold on
scatter(nan,nan,[],r,'.','DisplayName','inu')
scatter(nan,nan,[],b,'.','DisplayName','neko')
hold off
legend
  1 Comment
Natsuo OKADA
Natsuo OKADA on 21 Oct 2023
Thank you for your valuable advice! I have found it helpful.

Sign in to comment.

Categories

Find more on 測定と特性抽出 in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!