このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。
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);
イメージ セットをインデックス付けします。この手順には数分かかる場合があります。
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 5/100 iterations (~1.51 seconds/iteration)...converged in 5 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')
クエリ イメージの関心領域 (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.03 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')
また、関数 imrect
を使用して対話形式で ROI を選択することもできます。たとえば、queryROI = getPosition(imrect)
です。
オブジェクトが含まれているイメージを検索します。
imageIDs = retrieveImages(queryImage,imageIndex,'ROI',queryROI)
imageIDs = 12×1 uint32 column vector
1
11
6
12
2
3
8
5
14
13
10
16
⋮
最適一致を表示します。
bestMatch = imageIDs(1); figure imshow(imageIndex.ImageLocation{bestMatch})
ビジュアル ワードの位置を使用して最適な検索結果を検証します。幾何学的な情報に基づいて検索結果のランクを付け直すには、上位 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 5/100 iterations (~0.85 seconds/iteration)...converged in 5 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)
最適一致を取得します。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')
関数 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')
推定された変換を適用します。
outputView = imref2d(size(bestImage)); Ir = imwarp(queryImage, tform, 'OutputView', outputView); figure imshowpair(Ir,bestImage,'montage')
関数 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: {6×1 cell} ImageWords: [6×1 vision.internal.visualWords] WordFrequency: [0.1667 0.1667 0.1667 0.3333 0.1667 0.1667 0.1667 0.5000 0.3333 0.1667 0.3333 0.1667 0.1667 0.1667 0.1667 0.1667 0.1667 0.1667 0.1667 0.3333 0.1667 0.1667 0.1667 0.1667 0.1667 0.1667 0.1667 0.1667 0.1667 0.1667 … ] (1×1366 double) BagOfFeatures: [1×1 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 が、最も類似する一致イメージから最も類似しない一致イメージまで、ランク付けされた順序で返されます。
ビジュアル ワード割り当てを格納するオブジェクト。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 で導入
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)