Filter löschen
Filter löschen

画像を教師なしで分類する方法

1 Ansicht (letzte 30 Tage)
cho hunseki
cho hunseki am 2 Feb. 2024
Bearbeitet: Atsushi Ueno am 3 Feb. 2024
jpgのn個の画像データを教師なしで2つのグループに分類するために、
以下のcodeを準備しました。しかし、エラーが出て対応に困っております。
ご教授頂ければ幸いです。
仮にn個の「〇」と「✖」の画像を2つに分類する場合としました。
【code】
imageFiles = dir('*.jpg'); %
numImages = length(imageFiles);
images = cell(1, numImages);
for i = 1:numImages
currentFilename = fullfile(imageFiles(i).folder, imageFiles(i).name);
images{i} = imread(currentFilename);
end
numPixels = size(images{1}, 1) * size(images{1}, 2);
featureMatrix = zeros(numImages, numPixels, 3); %
for i = 1:numImages
featureMatrix(i, :, :) = reshape(images{i}, numPixels, 3);
end
% k-means
numClusters = 2; %
[idx, centroids] = kmeans(reshape(featureMatrix, [], 3), numClusters);
%
cluster1Indices = find(idx == 1);
cluster2Indices = find(idx == 2);
%
figure;
for i = 1:length(cluster1Indices)
subplot(2, length(cluster1Indices), i);
imshow(images{cluster1Indices(i)});
title(['Cluster 1 - Image ', num2str(i)]);
end
for i = 1:length(cluster2Indices)
subplot(2, length(cluster2Indices), length(cluster1Indices) + i);
imshow(images{cluster2Indices(i)});
title(['Cluster 2 - Image ', num2str(i)]);
end
【エラーメッセージ】
インデックスが配列要素数を超えています。インデックスは 6 を超えてはなりません。
エラー: untitled (34)
imshow(images{cluster1Indices(i)});
宜しくお願い致します。
  2 Kommentare
Atsushi Ueno
Atsushi Ueno am 3 Feb. 2024
  • cluster1Indices + cluster2Indices = (1枚目の画像の画素数) です
  • numImages = (画像の枚数、ここでは6枚) です
画像が6枚しかないのに、cluster1Indices枚目の画像を表示しようとしているので上記エラーが出ています。
Atsushi Ueno
Atsushi Ueno am 3 Feb. 2024
Bearbeitet: Atsushi Ueno am 3 Feb. 2024
k = 2 クラスターで全画像の画素値を2分しても「分類結果がどの画像に紐付くか」の情報は持っていません。

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu イメージを使用した深層学習 finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!