フィルターのクリア

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

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

サインインしてコメントする。

採用された回答

Kojiro Saito
Kojiro Saito 2023 年 10 月 11 日
編集済み: Kojiro Saito 2023 年 10 月 11 日
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 件のコメント
大誠
大誠 2023 年 10 月 12 日
2023bを入れて、アノテーションを行ってみました。 欲しかったデータに落とし込むことができました。 教えていただきありがとうございます。

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!