画像分類を対象フォル​ダ内のすべての画像に​適用させたい

以下のコードでフォルダ内の画像一枚に対しては作成済のSVMを適用させ分類結果を示せるようにできたのですが、フォルダ内の画像を一括で分類させるための操作がわからないです。
教えていただけると幸いです。
function [] = Predict(img)
load classifier.mat;
figure;
img = imread("A");
imshow(img);
img=rgb2gray(img);
glcm_feature = getGLCMFeatures(img);
lvl = graythresh(img);
img = im2bw(img, lvl);
% imshow(img);
% figure
img=imresize(img,[256 256]);
[hog_4x4, ~] = extractHOGFeatures(img,'CellSize',[4 4]);
testFeature = [hog_4x4 glcm_feature];
predictedLabel = predict(classifier, testFeature);
str = ['分類結果' predictedLabel];
dim = [0.25 0.0004 0.2 0.2];
annotation('textbox', dim, 'string', str, 'fontsize', 20, 'color', 'g','edgecolor', 'none');
end

Answers (1)

Shunichi Kusano
Shunichi Kusano on 13 Dec 2021

0 votes

imagedatastore使うのが良いかと思います。フォルダ内の指定拡張子の画像をストア化しておいて、readで一枚ずつ読み込めます。詳しくは下のドキュメントをご覧ください。

4 Comments

YT
YT on 13 Dec 2021
回答ありがとうございます!
加えての質問なのですが、イメージデータセット作成後、分類結果を0.1としているのですがそれをグラフで表示することは可能でしょうか?縦軸に0と1、横軸に各画像の番号をいれたいのですが、可能でしょうか?
可能です。
読み取り自体はwhile文で回すことになると思いますが、
preds = [];
while hasData(ds) % ds:データストアのオブジェクト
img = read(ds); % 一枚ずつ読み取り
preds = [preds Predict(img)]; % predsアレイに結果をくっつけていく
end
plot(x,preds) % x:画像の番号(あらかじめ作っておく想定)
このような感じでできるかと思います。上のサンプルコードに肉付けして作成いただければと思います。
YT
YT on 14 Dec 2021
詳しくご説明下さりありがとうございます
イメージデータセットを用いたいと思ったのですが、フォルダーや画像自体を指定しても、エラーでファイルまたはフォルダーが見つかりませんと表示されてしまいます。画像の形式はJPGでマットラボonlineからマットラボdriveを使用しています。
matlab online でも大丈夫なはずですが、コードはどのように書いていますでしょうか。
例えば私のケースではdrive上のフォルダにBloodSmearImagesというフォルダがあり、その中に各クラスの画像を保存した3つのフォルダを配置しています。BloodSmearImages/ooo,BloodSmearImages/xxx,BloodSmearImages/+++という感じの3フォルダです。このケースでは
ds = imageDatastore('BloodSmearImages','IncludeSubfolders',true);
で各フォルダ内の画像を網羅したデータストアができます。

Sign in to comment.

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Products

Asked:

YT
on 13 Dec 2021

Commented:

on 14 Dec 2021

Community Treasure Hunt

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

Start Hunting!