필터 지우기
필터 지우기

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!