11
15

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 5 years have passed since last update.

OpenCVのMatの中身をVisual Studioのデバッガで綺麗に表示

Last updated at Posted at 2015-04-27

Visual Studio 2013で、OpenCVのプログラムをデバッグするときの小ネタ。覚書き。

課題

3x3 の単位行列を出力する OpenCV のプログラム(実質2行)を書いてみる。

opencv_eye.cpp
#include <iostream>
#include <opencv/core.hpp>

int main(void)
{
    cv::Mat eye = cv::Mat::eye(3,3,CV_32FC1);
    std::cout << eye << std::endl;
    return 0;
}

実行するとこんな感じ。

output.png

Visual Studio 2013 の デバッガー から、変数 eye の中身を見たい。
しかし、ローカル変数のウィンドウでは、こんな風に 0 '\0' と表示されてしまう。

local_variable.png

デバッガーはOpenCVの型情報を解釈できないので、仕方なし。

メモリウィンドウで覗いても良いのですが、カラム数など毎回設定するのは面倒。
どうしたら、cv::Mat の中身を 3x3 の行列として、簡単に見られるのか?

ヒント:ウォッチ式でキャストを使う。

解1

cv::Mat.data を 3 要素の1次元 float 配列にキャストし、それを3行分表示する。

ウォッチ式: (float (*)[3]) eye.data, 3

watch1.png

『 , n 』で修飾すると n 個分のデータを表示してくれる ナイス機能を利用。

解2

cv::Mat.data を 3x3 要素の2次元 float 配列にキャストして表示する。

ウォッチ式: (float (*)[3][3]) eye.data

watch2.png

こちらの方が素直ですかね!

型は float [3][3] * という風にポインタ扱いになる模様。
同じ手は、3次元配列にも通用します。

まとめ

ウォッチ式でキャストを工夫すると、Continuous な cv::Mat の中身を簡単に確認することができる。

おわり。

11
15
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
11
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?