cell配列に格納されているクラス名によって色を変えて画像を表示するにはどうすればよいですか?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Natsuo OKADA
am 20 Okt. 2023
Kommentiert: Natsuo OKADA
am 23 Okt. 2023
predicted_imageは、読み込んだ画像の各ピクセルごとに機械学習で判定したクラス名が格納されてる512×512 cell配列です。
クラス名ごとに色を変えて表示したいのですが、どのように行えばよいでしょうか?
例えば、犬として判定されたピクセルを赤色、猫として判定されたピクセルを青色として凡例とともに表示したいです。
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1516131/image.png)
% 予測結果を画像データに変換する。
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を使用
% クラスごとに色を変えて画像として表示
0 Kommentare
Akzeptierte Antwort
Hiroshi Iwamura
am 20 Okt. 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)
%% using a-array only
names = unique(a)
cnum = size(names, 1) % category size
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
b = cellfun(@(x) d(x), a, UniformOutput=false) % swapping categories with values
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 Kommentare
Akira Agata
am 23 Okt. 2023
+1
% make test cell array
mapping = {'neko', 'inu'; 'saru', 'kiji'};
predImg = repelem(mapping, 10, 10);
% カテゴリカル配列に変換
predCat = categorical(predImg);
% label2rgbでRGB画像に変換
I = label2rgb(predCat);
% 確認
imshow(I)
Weitere Antworten (1)
Dyuman Joshi
am 20 Okt. 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
Siehe auch
Kategorien
Mehr zu 多角形 finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!