Main Content

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

retrieveImages

イメージ セットでの類似イメージの検索

説明

imageIDs = retrieveImages(queryImage,imageIndex) は、クエリ イメージと視覚的に似ている imageIndex 内のイメージに対応するイメージ識別子 imageIDs を返します。imageIDs は、類似度が最も高いものから最も低いものへとランク付けされた順序で返されます。

[imageIDs,scores] = retrieveImages(queryImage,imageIndex) は、イメージ取得結果のランク付けに使用される類似スコアをオプションで返します。scores 出力には、0 ~ 1 の対応するスコアが含まれます。

[imageIDs,scores,imageWords] = retrieveImages(queryImage,imageIndex) は、類似イメージの検索に使用される queryImage のビジュアル ワードをオプションで返します。

[imageIDs,___] = retrieveImages(queryImage,imageIndex,Name,Value) は、前述の構文のいずれかを使用し、Name,Value ペアの引数を 1 つ以上指定したオプションを追加で使用します。

すべて折りたたむ

ブック カバーのイメージ セットを作成します。

dataDir = fullfile(toolboxdir('vision'),'visiondata','bookCovers');
bookCovers = imageDatastore(dataDir);

データ セットを表示します。

thumbnailGallery = [];
for i = 1:length(bookCovers.Files)
    I = readimage(bookCovers,i);
    thumbnail = imresize(I,[300 300]);
    thumbnailGallery = cat(4,thumbnailGallery,thumbnail);
end

figure
montage(thumbnailGallery);

Figure contains an axes object. The axes object contains an object of type image.

イメージ セットをインデックス付けします。この手順には数分かかる場合があります。

imageIndex = indexImages(bookCovers);
Creating an inverted image index using Bag-Of-Features.
-------------------------------------------------------

Creating Bag-Of-Features.
-------------------------

* Selecting feature point locations using the Detector method.
* Extracting SURF features from the selected feature point locations.
** detectSURFFeatures is used to detect key points for feature extraction.

* Extracting features from 58 images...done. Extracted 29216 features.

* Keeping 80 percent of the strongest features from each category.

* Balancing the number of features across all image categories to improve clustering.
** Image category 1 has the least number of strongest features: 23373.
** Using the strongest 23373 features from each of the other image categories.

* Creating a 20000 word visual vocabulary.
* Number of levels: 1
* Branching factor: 20000
* Number of clustering steps: 1

* [Step 1/1] Clustering vocabulary level 1.
* Number of features          : 23373
* Number of clusters          : 20000
* Initializing cluster centers...100.00%.
* Clustering...completed 10/100 iterations (~0.96 seconds/iteration)...converged in 10 iterations.

* Finished creating Bag-Of-Features


Encoding images using Bag-Of-Features.
--------------------------------------

* Encoding 58 images...done.
Finished creating the image index.

クエリ イメージを選択および表示します。

queryDir = fullfile(dataDir,'queries',filesep);
queryImage = imread([queryDir 'query3.jpg']);

imageIDs = retrieveImages(queryImage,imageIndex);

クエリ イメージとその最適一致を左右に並べて表示します。

bestMatch = imageIDs(1);
bestImage = imread(imageIndex.ImageLocation{bestMatch});

figure
imshowpair(queryImage,bestImage,'montage')

Figure contains an axes object. The axes object contains an object of type image.

クエリ イメージの関心領域 (ROI) を使用してオブジェクトのイメージ セットを検索します。

検索する一連のイメージを定義します。

imageFiles = ...
  {'elephant.jpg', 'cameraman.tif', ...
  'peppers.png',  'saturn.png',...
  'pears.png',    'stapleRemover.jpg', ...
  'football.jpg', 'mandi.tif',...
  'kids.tif',     'liftingbody.png', ...
  'office_5.jpg', 'gantrycrane.png',...
  'moon.tif',     'circuit.tif', ...
  'tape.png',     'coins.png'};

imds = imageDatastore(imageFiles);

検索インデックスを作成します。

 imageIndex = indexImages(imds);
Creating an inverted image index using Bag-Of-Features.
-------------------------------------------------------

Creating Bag-Of-Features.
-------------------------

* Selecting feature point locations using the Detector method.
* Extracting SURF features from the selected feature point locations.
** detectSURFFeatures is used to detect key points for feature extraction.

* Extracting features from 16 images...done. Extracted 3680 features.

* Keeping 80 percent of the strongest features from each category.

* Balancing the number of features across all image categories to improve clustering.
** Image category 1 has the least number of strongest features: 2944.
** Using the strongest 2944 features from each of the other image categories.

* Creating a 2944 word visual vocabulary.
* Number of levels: 1
* Branching factor: 2944
* Number of clustering steps: 1

* [Step 1/1] Clustering vocabulary level 1.
* Number of features          : 2944
* Number of clusters          : 2944
* Initializing cluster centers...100.00%.
* Clustering...completed 1/100 iterations (~0.06 seconds/iteration)...converged in 1 iterations.

* Finished creating Bag-Of-Features


Encoding images using Bag-Of-Features.
--------------------------------------

* Encoding 16 images...done.
Finished creating the image index.

クエリ イメージと ROI を指定します。この ROI は検索対象のオブジェクトであるゾウの外郭を表示します。

queryImage = imread('clutteredDesk.jpg');
queryROI = [130 175 330 365];

figure
imshow(queryImage)
rectangle('Position',queryROI,'EdgeColor','yellow')

Figure contains an axes object. The axes object contains 2 objects of type image, rectangle.

また、関数 imrect を使用して対話形式で ROI を選択することもできます。たとえば、queryROI = getPosition(imrect) です。

オブジェクトが含まれているイメージを検索します。

imageIDs = retrieveImages(queryImage,imageIndex,'ROI',queryROI)
imageIDs = 13x1 uint32 column vector

    1
   11
    6
   12
   13
    3
    2
    8
   14
   10
      ⋮

最適一致を表示します。

bestMatch = imageIDs(1);

figure
imshow(imageIndex.ImageLocation{bestMatch})

Figure contains an axes object. The axes object contains an object of type image.

ビジュアル ワードの位置を使用して最適な検索結果を検証します。幾何学的な情報に基づいて検索結果のランクを付け直すには、上位 N 件の検索結果に対してこの手順を繰り返します。

イメージの位置を指定します。

dataDir = fullfile(toolboxdir('vision'),'visiondata','bookCovers');
bookCovers = imageDatastore(dataDir);

イメージ セットをインデックス付けします。この処理には数分かかることがあります。

imageIndex = indexImages(bookCovers);
Creating an inverted image index using Bag-Of-Features.
-------------------------------------------------------

Creating Bag-Of-Features.
-------------------------

* Selecting feature point locations using the Detector method.
* Extracting SURF features from the selected feature point locations.
** detectSURFFeatures is used to detect key points for feature extraction.

* Extracting features from 58 images...done. Extracted 29216 features.

* Keeping 80 percent of the strongest features from each category.

* Balancing the number of features across all image categories to improve clustering.
** Image category 1 has the least number of strongest features: 23373.
** Using the strongest 23373 features from each of the other image categories.

* Creating a 20000 word visual vocabulary.
* Number of levels: 1
* Branching factor: 20000
* Number of clustering steps: 1

* [Step 1/1] Clustering vocabulary level 1.
* Number of features          : 23373
* Number of clusters          : 20000
* Initializing cluster centers...100.00%.
* Clustering...completed 10/100 iterations (~1.35 seconds/iteration)...converged in 10 iterations.

* Finished creating Bag-Of-Features


Encoding images using Bag-Of-Features.
--------------------------------------

* Encoding 58 images...done.
Finished creating the image index.

クエリ イメージを選択および表示します。

queryDir = fullfile(dataDir,'queries',filesep);
queryImage = imread([queryDir 'query3.jpg']);

figure
imshow(queryImage)

Figure contains an axes object. The axes object contains an object of type image.

最適一致を取得します。queryWords 出力にはクエリ イメージのビジュアル ワードの位置情報が含まれます。検索結果を検証するには、この情報を使用します。

[imageIDs, ~, queryWords] = retrieveImages(queryImage,imageIndex);

イメージ インデックスからビジュアル ワードを抽出し、クエリ イメージの最適一致を検索します。イメージ インデックスにはインデックス内のすべてのイメージのビジュアル ワード情報が含まれます。

bestMatch = imageIDs(1);
bestImage = imread(imageIndex.ImageLocation{bestMatch});
bestMatchWords = imageIndex.ImageWords(bestMatch);

ビジュアル ワードの割り当てに基づいて、一連の仮の一致を生成します。クエリ内の各ビジュアル ワードは、ビジュアル ワードの割り当てに使用されるハード量子化によって、複数の一致をもつ可能性があります。

queryWordsIndex     = queryWords.WordIndex;
bestMatchWordIndex  = bestMatchWords.WordIndex;

tentativeMatches = [];
for i = 1:numel(queryWords.WordIndex)
    
    idx = find(queryWordsIndex(i) == bestMatchWordIndex);
    
    matches = [repmat(i, numel(idx), 1) idx];
    
    tentativeMatches = [tentativeMatches; matches];
    
end

仮の一致に対する点の位置を表示します。不適切な一致が数多くあります。

points1 = queryWords.Location(tentativeMatches(:,1),:);
points2 = bestMatchWords.Location(tentativeMatches(:,2),:);

figure
showMatchedFeatures(queryImage,bestImage,points1,points2,'montage')

Figure contains an axes object. The axes object contains 4 objects of type image, line. One or more of the lines displays its values using only markers

関数 estimateGeometricTransform2D を使用して不適切なビジュアル ワードの割り当てを削除します。有効な幾何学的変換に適合する割り当ては保持します。

[tform,inlierIdx] = ...
    estimateGeometricTransform2D(points1,points2,'affine',...
        'MaxNumTrials',2000);
inlierPoints1 = points1(inlierIdx, :);
inlierPoints2 = points2(inlierIdx, :);

インライアのパーセント比で検索結果のランクを付け直します。これは、幾何学的な検証手順が上位 N 件の検索結果に適用される際に行います。インライアのパーセント比が高いこれらのイメージは、関連している可能性が高くなります。

percentageOfInliers = size(inlierPoints1,1)./size(points1,1);

figure
showMatchedFeatures(queryImage,bestImage,inlierPoints1,...
    inlierPoints2,'montage')

Figure contains an axes object. The axes object contains 4 objects of type image, line. One or more of the lines displays its values using only markers

推定された変換を適用します。

outputView = imref2d(size(bestImage));
Ir = imwarp(queryImage, tform, 'OutputView', outputView);

figure
imshowpair(Ir,bestImage,'montage')

Figure contains an axes object. The axes object contains an object of type image.

関数 evaluateImageRetrieval を使用して適切な検索パラメーターを選択できます。

イメージ セットを作成します。

setDir  = fullfile(toolboxdir('vision'),'visiondata','imageSets','cups');
imds = imageDatastore(setDir, 'IncludeSubfolders', true, 'LabelSource', 'foldernames');

イメージ セットをインデックス付けします。

 imageIndex = indexImages(imds,'Verbose',false);

イメージ検索パラメーターを調整します。

imageIndex.MatchThreshold = 0.2;
imageIndex.WordFrequencyRange = [0 1]
imageIndex = 
  invertedImageIndex with properties:

         ImageLocation: {6x1 cell}
            ImageWords: [6x1 vision.internal.visualWords]
         WordFrequency: [1x1366 double]
         BagOfFeatures: [1x1 bagOfFeatures]
               ImageID: [1 2 3 4 5 6]
        MatchThreshold: 0.2000
    WordFrequencyRange: [0 1]

queryImage = readimage(imds, 1);
indices = retrieveImages(queryImage,imageIndex);

入力引数

すべて折りたたむ

入力クエリ イメージ。M x N x 3 のトゥルーカラー イメージ、または M 行 N 列の 2 次元グレースケール イメージのいずれかに指定します。

データ型: single | double | int16 | uint8 | uint16 | logical

イメージ検索インデックス。invertedImageIndex オブジェクトとして指定します。関数 indexImages は、イメージの検索に使用されるデータを格納する invertedImageIndex オブジェクトを作成します。

名前と値の引数

オプションの引数のペアを Name1=Value1,...,NameN=ValueN として指定します。ここで、Name は引数名で、Value は対応する値です。名前と値の引数は他の引数の後に指定しなければなりませんが、ペアの順序は重要ではありません。

R2021a より前では、コンマを使用して名前と値をそれぞれ区切り、Name を引用符で囲みます。

例: 'NumResults'25'NumResults' プロパティを 25 に設定する

返される結果の最大数。NumResults と数値で構成されるコンマ区切りのペアとして指定します。できる限り多くの一致イメージが返されるようにするにはこの値を Inf に設定します。

クエリ イメージ探索領域。'ROI' と [x y width height] ベクトルで構成されるコンマ区切りのペアとして指定します。

イメージ検索結果のランク付けに使用する類似度メトリクス。'cosine' または 'L1' [3] として指定します。

出力引数

すべて折りたたむ

取得されたイメージのランク付けされたインデックス。M 行 1 列のベクトルとして返されます。イメージ ID が、最も類似する一致イメージから最も類似しない一致イメージまで、ランク付けされた順序で返されます。

類似度メトリクス。N 行 1 列のベクトルとして返されます。この出力には、imageIDs 出力で取得したイメージに一致するスコアが含まれます。スコアは、Metric プロパティを使用して計算され、範囲は 0 ~ 1 です。

ビジュアル ワード割り当てを格納するオブジェクト。visualWords オブジェクトとして返されます。このオブジェクトには、queryImage のビジュアル ワード割り当てと、そのイメージ内における位置が格納されます。

参照

[1] Sivic, J. and A. Zisserman. Video Google: A text retrieval approach to object matching in videos. ICCV (2003) pg 1470-1477.

[2] Philbin, J., O. Chum, M. Isard, J. Sivic, and A. Zisserman. Object retrieval with large vocabularies and fast spatial matching. CVPR (2007).

[3] Gálvez-López, Dorian, and Juan D. Tardos. Bags of binary words for fast place recognition in image sequences. IEEE Transactions on Robotics 28.5 (2012): 1188-1197.

拡張機能

C/C++ コード生成
MATLAB® Coder™ を使用して C および C++ コードを生成します。

バージョン履歴

R2015a で導入