LoginSignup
5
4

More than 5 years have passed since last update.

[Node.js] OpenCVで物体の特徴点を抽出、マッチングさせてみた

Last updated at Posted at 2018-05-29

はじめに

大学の研究でOpenCVを使う機会があり、調べたところnpm moduleが出ていたので遊んでみた。
今回は、二枚の画像から特徴点を抽出し、マッチングさせていきます。

OpenCVとは

 OpenCV(正式名称: Open Source Computer Vision Library)は、オープンソースのコンピューター・ビジョン・ライブラリです。コンピューターで画像や動画を処理するのに必要な、さまざま機能が実装されており、BSDライセンスで配布されていることから学術用途だけでなく商用目的でも利用できます。加えて、マルチプラットフォーム対応されているため、幅広い場面で利用されていることが特徴です。

環境構築

1.モジュールをインストールする。
 npm install --save opencv4nodejs
これで開発環境は整いました!

使用例

sample.js
const cv = require('opencv4nodejs');

const matchFeatures = ({ img1, img2, detector, matchFunc }) => {

  const keyPoints1 = detector.detect(img1);
  const keyPoints2 = detector.detect(img2);

  const descriptors1 = detector.compute(img1, keyPoints1);
  const descriptors2 = detector.compute(img2, keyPoints2);

  const matches = matchFunc(descriptors1, descriptors2);

  const bestN = 40;
  const bestMatches = matches.sort(
    (match1, match2) => match1.distance - match2.distance
  ).slice(0, bestN);

  return cv.drawMatches(
    img1,
    img2,
    keyPoints1,
    keyPoints2,
    bestMatches
  );
};

const img1 = cv.imread('画像のパス');
const img2 = cv.imread('画像のパス');

//SIFTで特徴点を抽出
if (cv.xmodules.xfeatures2d) {
  const siftMatchesImg = matchFeatures({
    img1,
    img2,
    detector: new cv.SIFTDetector({ nFeatures: 2000 }),
    matchFunc: cv.matchFlannBased
  });
  cv.imshowWait('SIFT matches', siftMatchesImg);
} else {
  console.log('skipping SIFT matches');
}

//ORBで特徴点を抽出
const orbMatchesImg = matchFeatures({
  img1,
  img2,
  detector: new cv.ORBDetector(),
  matchFunc: cv.matchBruteForceHamming
});
cv.imshowWait('ORB matches', orbMatchesImg);

一応Githubのリポジトリを貼っておきます。
https://github.com/toshi1127/node_openCV.git

実行結果

SIFTによる、キーポイント検出とマッチング

match.png

ORBによる、キーポイント検出とマッチング

match.png

5
4
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
5
4