2
1

More than 3 years have passed since last update.

2.1.4 グラフィックス (マーカー描画・テキスト描画・図形描画)

Posted at

目次へのリンク

MATLABによる画像処理・コンピュータービジョン入門目次

概要

MATLABで画像にマーカーや注釈、図形、テキストを描画・挿入する方法を紹介します。

Computer Vision Toolbox™の各種描画関数を活用します。

対応ファイル

初期化

Code
clear; clc; close all; imtool close all

画像を読み込む

Code
I = imread('visionteam1.jpg');
figure; imshow(I);

figure_0_png.jpg

人物検出のアルゴリズムを実行

可視化に使用するために人物検出器を使ってバウンディングボックスの座標とスコアを計算します。

Code
detector = peopleDetectorACF;
[bboxes,scores] = detect(detector,I)
Output
bboxes = 5x4    
   180   119    98   238
   589   130   126   308
   260    95   149   364
    51   115   182   443
   439    52   215   523

scores = 5x1    
   39.6764
   25.4916
   74.0602
   59.2155
   47.6619

検出したバウンディングボックスを描画

はじめに検出したバウンディングボックスを描画します。

Code
Iboxes = insertShape(I,'rectangle',bboxes);
imshow(Iboxes)

figure_1_png.jpg

バウンディングボックスの枠の太さや色を変えてみます。

Code
Iboxes = insertShape(I,'rectangle',bboxes,"LineWidth",5,"Color","red");
imshow(Iboxes)

figure_2_png.jpg

検出したバウンディングボックスを描画(キャプションを追加)

次に各検出結果のスコアをキャプションとしてバウンディングボックスを可視化します。

Code
Iout = insertObjectAnnotation(I,'rectangle',bboxes,scores);
figure
imshow(Iout)

figure_3_png.jpg

高いスコアと低いスコアで異なる色になるようにします。

Code
% カラーマップを生成
cmap = jet;

% スコアを0-255に正規化
scoreNorm = im2uint8(mat2gray(scores));

% カラーマップに割り当て
colors = im2uint8(reshape(ind2rgb(scoreNorm,cmap),[],3));

% バウンディングボックスを挿入した画像生成
Iout = insertObjectAnnotation(I,'rectangle',bboxes,scores,...
    "LineWidth",3,"Color",colors);
figure
imshow(Iout)

figure_4_png.jpg

テキスト挿入

テキストを挿入します。日本語で挿入することも可能です。

Code
listTrueTypeFonts % フォントのリスト確認
Output
ans = 591x1 cell    
'Agency FB'            
'Agency FB Bold'       
'Algerian'             
'Arial'                
'Arial Black'          
'Arial Bold'           
'Arial Bold Italic'    
'Arial Italic'         
'Arial Narrow'         
'Arial Narrow Bold'    

Code
Iout2 = insertText(Iout,[1,1],'人物検出結果の可視化',...
    'Font','MS UI Gothic','FontSize',25,...
    'BoxColor','blue','TextColor','white');
figure, imshow(Iout2);

figure_5_png.jpg

マーカー挿入

マーカーを描画・挿入します。

例として検出された人物上でORB特徴を抽出し、その特徴点を描画します。

Code
points = detectORBFeatures(rgb2gray(I),'ROI',bboxes(3,:));

% スコアを0-255に正規化
metricNorm = im2uint8(mat2gray(points.Metric));

% カラーマップに割り当て
colors = im2uint8(reshape(ind2rgb(metricNorm,parula),[],3));

% 描画
Iout3 = insertMarker(Iout2,points.Location,"plus","Color",colors);
figure, imshow(Iout3);

figure_6_png.jpg

図形挿入

任意の図形を挿入することもできます。

Code
% 特徴点の重複を取り除く
loc = unique(double(points.Location),'rows');
% 三角形分割
DT = delaunayTriangulation(loc(:,1),loc(:,2));
%凸包を計算
C = convexHull(DT);
% [x1,y1,x2,y2,...]というベクトルに変換
polyPos = reshape([DT.Points(C,1),DT.Points(C,2)]',1,[]);
% ポリゴンとして挿入
Iout4 = insertShape(Iout3,'Polygon',polyPos,'LineWidth',5);

% 顔検出
faceDetector = vision.CascadeObjectDetector();
bboxesFace = faceDetector(rgb2gray(I));
% 円を描画するために[x,y,r]のベクトルに変換
posCir = [bboxesFace(:,1:2)+bboxesFace(:,3:4)/2 bboxesFace(:,3)/2];
Iout5 = insertShape(Iout4,'Circle',posCir,'LineWidth',5);

figure, imshow(Iout5);

figure_7_png.jpg

まとめ

MATLABで画像にマーカーや注釈、図形、テキストを描画・挿入する方法を紹介しました。

画像処理、コンピュータービジョン、ディープラーニングなどの処理結果の可視化にお役立てください。

参考

謝辞

本記事は @eigs さんのlivescript2markdownを使わせていただいてます。

目次へのリンク

MATLABによる画像処理・コンピュータービジョン入門目次

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1