OpenCV 4.13.0
Open Source Computer Vision
読み込み中...
検索中...
見つかりません
🤖 AIによる機械翻訳(非公式) — これは OpenCV 4.13.0 公式リファレンス(英語)を AI (Claude) で自動翻訳したものです。訳に誤りを含む場合があります。正確な情報は 公式英語版(原文) を参照してください。
クラスタリング

詳細説明

列挙型

enum  cv::KmeansFlags {
  cv::KMEANS_RANDOM_CENTERS = 0 ,
  cv::KMEANS_PP_CENTERS = 2 ,
  cv::KMEANS_USE_INITIAL_LABELS = 1
}
 k-meansフラグ 続き...
 

関数

double cv::kmeans (InputArray data, int K, InputOutputArray bestLabels, TermCriteria criteria, int attempts, int flags, OutputArray centers=noArray())
 クラスタの中心を見つけ、入力サンプルをクラスタの周りにグループ化する。
 
template<typename _Tp , class _EqPredicate >
int cv::partition (const std::vector< _Tp > &vec, std::vector< int > &labels, _EqPredicate predicate=_EqPredicate())
 要素の集合を同値クラスに分割する。
 

列挙型詳解

◆ KmeansFlags

#include <opencv2/core.hpp>

k-meansフラグ

列挙値
KMEANS_RANDOM_CENTERS 
Python: cv.KMEANS_RANDOM_CENTERS

各試行でランダムな初期中心を選択する。

KMEANS_PP_CENTERS 
Python: cv.KMEANS_PP_CENTERS

ArthurとVassilvitskiiによるkmeans++中心初期化を使用する [Arthur2007]。

KMEANS_USE_INITIAL_LABELS 
Python: cv.KMEANS_USE_INITIAL_LABELS

最初の(場合によっては唯一の)試行では、初期中心から計算する代わりにユーザが指定したラベルを使用する。2回目以降の試行では、ランダムまたは半ランダムな中心を使用する。正確な手法を指定するにはKMEANS_*_CENTERSフラグのいずれかを使用する。

関数詳解

◆ kmeans()

double cv::kmeans ( InputArray data,
int K,
InputOutputArray bestLabels,
TermCriteria criteria,
int attempts,
int flags,
OutputArray centers = noArray() )
Python:
cv.kmeans(data, K, bestLabels, criteria, attempts, flags[, centers]) -> retval, bestLabels, centers

#include <opencv2/core.hpp>

クラスタの中心を求め、入力サンプルをクラスタの周囲にグループ化する。

関数kmeansは、cluster_count個のクラスタの中心を求め、入力サンプルをそれらのクラスタの周りにグループ化するk-meansアルゴリズムを実装する。出力として、\(\texttt{bestLabels}_i\) には、samples行列の \(i^{th}\) 行に格納されたサンプルの0始まりのクラスタインデックスが格納される。

覚え書き
  • (Python) k-meansクラスタリングの例は opencv_source_code/samples/python/kmeans.py にある
引数
dataクラスタリング対象のデータ。浮動小数点座標を持つN次元点の配列が必要である。この配列の例としては次のものがある:
  • Mat points(count, 2, CV_32F);
  • Mat points(count, 1, CV_32FC2);
  • Mat points(1, count, CV_32FC2);
  • std::vector<cv::Point2f> points(sampleCount);
K集合を分割するクラスタ数。
bestLabels各サンプルのクラスタインデックスを格納する入出力用の整数配列。
criteriaアルゴリズムの終了条件、すなわち最大反復回数および/または希望する精度。精度は criteria.epsilon として指定する。ある反復で各クラスタ中心の移動量が criteria.epsilon 未満になった時点で、アルゴリズムは停止する。
attempts異なる初期ラベリングを用いてアルゴリズムを実行する回数を指定するフラグ。アルゴリズムは最良のコンパクトさをもたらすラベルを返す(最後の関数引数を参照)。
flagscv::KmeansFlags の値を取り得るフラグ
centersクラスタ中心を出力する行列。各行が1つのクラスタ中心に対応する。
戻り値
The function returns the compactness measure that is computed as

\[\sum _i \| \texttt{samples} _i - \texttt{centers} _{ \texttt{labels} _i} \| ^2\]

after every attempt. The best (minimum) value is chosen and the corresponding labels and the compactness value are returned by the function. Basically, you can use only the core of the function, set the number of attempts to 1, initialize labels each time using a custom algorithm, pass them with the ( flags = KMEANS_USE_INITIAL_LABELS ) flag, and then choose the best (most-compact) clustering.
この関数の呼び出しグラフ:

◆ partition()

template<typename _Tp , class _EqPredicate >
int cv::partition ( const std::vector< _Tp > & vec,
std::vector< int > & labels,
_EqPredicate predicate = _EqPredicate() )

#include <opencv2/core/operations.hpp>

要素集合を同値類に分割する。

汎用関数partitionは、http://en.wikipedia.org/wiki/Disjoint-set_data_structure に記載されているように、\(N\) 個の要素からなる集合を1つ以上の同値類に分割する \(O(N^2)\) のアルゴリズムを実装する。この関数は同値類の数を返す。

引数
vecベクトルとして格納された要素の集合。
labelsラベルの出力ベクトル。vecと同じ数の要素を含む。各ラベルlabels[i]は vec[i] の0始まりのクラスタインデックスである。
predicate同値述語(2引数のブール関数へのポインタ、またはメソッド bool operator()(const _Tp& a, const _Tp& b) を持つクラスのインスタンス)。この述語は要素が確実に同じクラスに属するときtrueを返し、同じクラスに属するか否か不明な場合はfalseを返す。