1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

OpecCV C++ Mat 最小値・最大値・座標の取得

Posted at

はじめに

cv::minMaxLoc 関数を使った最小値・最大値・座標の取得方法をメモした。

cv::minMaxLocの定義

cv::minMaxLoc関数を使って最小値・最大値・座標を取得することができる。
ただし、関数はシングルチャネル配列のみ適用可能。

マルチチャネル配列で適用したい場合、
cv::reshape() を用いて配列をシングルチャンネルに再編成するか
cv::extractImageCOI() や cv::mixChannels()、cv::split() などの関数を
用いて特定のチャンネルを抜き出す必要あり。

void minMaxLoc(
    const InputArray& src,
    double* minVal,
    double* maxVal = 0,
    Point* minLoc = 0,
    Point* maxLoc = 0,
    const InputArray& mask= noArray()
    )

※ n 次元の場合、src は cv::SparseMat 型。

  • 第 1 引数:src
    シングルチャンネルの入力配列。

  • 第 2 引数:minVal
    最小値が入る変数へのポインタ。不要ならば NULL ( 0 ) を指定。

  • 第 3 引数:maxVal
    最大値が入る変数へのポインタ。不要ならば NULL ( 0 ) を指定。

  • 第 4 引数:minLoc
    ( 2 次元の場合 ) 最小値の位置が入る変数へのポインタ。
    不要ならば NULL ( 0 ) を指定。

  • 第 5 引数:maxLoc
    ( 2 次元の場合 ) 最大値の位置が入る変数へのポインタ。
    不要ならば NULL ( 0 ) を指定。

  • 第 4 引数:minIdx int 型。
    ( n 次元の場合 ) 最小値の位置が入る変数へのポインタ。
    不要ならば NULL ( 0 ) を指定。必要なら src.dims 要素の配列を指する。
    各次元の最小要素の座標が順次格納される。
    mat.dims:次元数

  • 第 5 引数:maxIdx int 型。
    ( n 次元の場合 ) 最大値の位置が入る変数へのポインタ。
    不要ならば NULL ( 0 ) を指定。

  • 第 6 引数:mask
    部分配列を選択する為に利用されるマスク。

実行例

 double mMin, mMax;
    cv::Point minP, maxP;
    cv::Mat mBar = (cv::Mat_<double>(3, 4) << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
   
    cv::minMaxLoc(mBar, &mMin, &mMax, &minP, &maxP);
    std::cout << mBar << std::endl;
    std::cout << "min: " << mMin << ", point " << minP << std::endl;
    std::cout << "max: " << mMax << ", point " << maxP << std::endl;
    //pointは[x,y]で出力される

出力結果
[1, 2, 3, 4;
5, 6, 7, 8;
9, 10, 11, 12]
min: 1, point [0, 0]
max: 12, point [3, 2]

参考

1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?