labelmeで作っ​たjsonファイルを​matlabを用いて​、jsonからpng​画像を出したい

36 visualizzazioni (ultimi 30 giorni)
大誠
大誠 il 10 Ott 2023
Commentato: Atsushi Ueno il 12 Ott 2023
機械学習のために、とある画像のマスク画像を作成したいと思い、labelmeというアプリを用いてアノテーションを付けました。
保存がjsonファイルとして保存され、どのように画像として出すのかがわからず、MATLABでもjsonファイルを読み込めると拝見したので、質問させていただきました。
jsonファイルが添付できなかったので、このような形で質問させていただきます。
よろしくお願いいたします
  3 Commenti
大誠
大誠 il 10 Ott 2023
Ueno様
読み込みも苦戦していたので、教えていただきとても助かりました。
ありがとうございます。
おっしゃる通りアノテーション画像を表示したいのですが、データ型がstructになっているので画像を出すことが現在できない状態です、、、
Atsushi Ueno
Atsushi Ueno il 12 Ott 2023
余談ですがこの”Labelme”はWikipediaに載る程有名なアノテーションツールの様で「MATLAB Toolbox for the LabelMe」も開発されたそうです。試していないので詳細は不明ですが、jsonファイル経由でデータを移動しなくてもMATLABからLabelmeを直接動かして、アノテーションデータを直に取得する事が出来るようです。情報まで。

Accedi per commentare.

Risposta accettata

Kojiro Saito
Kojiro Saito il 11 Ott 2023
Modificato: Kojiro Saito il 11 Ott 2023
JSONファイルの読み込みは、fileread (文字列として読み込む)、readlines (string配列として読み込む)、そしてR2023bからはreadstruct (構造体として読み込む)などが使えます。
JSONファイルを読み込んだ後、中のアノテーションをshape_typeに応じてimages.roi.Polygonimages.roi.Rectangle (どちらもImage Processing Toolboxの関数)などを使って描画し、createMask (Image Processing Toolbox)でマスク画像を生成できます。
websave('2011_000025.jpg', 'https://github.com/wkentaro/labelme/blob/main/examples/semantic_segmentation/data_annotated/2011_000025.jpg?raw=true');
websave('2011_000025.json','https://github.com/wkentaro/labelme/blob/main/examples/semantic_segmentation/data_annotated/2011_000025.json?raw=true');
% JSONファイルを文字列として読み込み
str = fileread("2011_000025.json");
% 構造体に変換
str = jsondecode(str);
img = imread("2011_000025.jpg");
imshow(img)
for n=1:length(str.shapes)
if str.shapes(n).shape_type == "polygon"
h = images.roi.Polygon(gca,'Position', vertcat(str.shapes(n).points), 'Label', str.shapes(n).label);
elseif str.shapes(n).shape_type == "rectangle"
h = images.roi.Rectangle(gca,'Position', [str.shapes(n).points(1, :) str.shapes(n).points(2, :)-str.shapes(n).points(1, :)], 'Label', str.shapes(n).label);
elseif str.shapes(n).shape_type == "circle"
h = images.roi.Circle(gca,'Center', str.shapes(n).points(1, :), 'Radius', norm(str.shapes(n).points(2, :)-str.shapes(n).points(1, :)), 'Label', str.shapes(n).label);
elseif str.shapes(n).shape_type == "line"
h = images.roi.Line(gca,'Position', vertcat(str.shapes(n).points), 'Label', str.shapes(n).label);
elseif str.shapes(n).shape_type == "point"
h = images.roi.Point(gca,'Position', vertcat(str.shapes(n).points), 'Label', str.shapes(n).label);
elseif str.shapes(n).shape_type == "linestrip"
h = images.roi.Polyline(gca,'Position', vertcat(str.shapes(n).points), 'Label', str.shapes(n).label);
end
% マスクの作成
if n == 1
mask = createMask(h);
else
% 重ね描き
mask = mask + createMask(h);
end
end
% マスク画像を画像ファイルとして出力
imwrite(mask, 'out.jpg')
imshow(mask)
  1 Commento
大誠
大誠 il 12 Ott 2023
2023bを入れて、アノテーションを行ってみました。 欲しかったデータに落とし込むことができました。 教えていただきありがとうございます。

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!