살군의 보조기억 장치

Another memory device…

OpenCV Mat 클래스 분석#2 (예제코드)

with one comment

아마도 이 예제코드가 많이 도움이 될 것 같아서 바로 넣어둔다. 최초의 matrix 선언에서 type 을 바꿔가면서 실행해보면 각 함수, 멤버변수가 의미하는 바를 쉽게 확인할 수 있을 것이다. 아래글에서 언급한 내용을 확인하는데 도움이 될 것이다.


#include <stdio.h>
#include <iostream>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/flann/miniflann.hpp"
#include <direct.h>

using namespace cv; // all the new API is put into "cv" namespace. Export its content
using namespace std;
using namespace cv::flann;


string depth2str(int depth) {
  string r;

  switch ( depth ) {
    case CV_8U:  r = "8U"; break;
    case CV_8S:  r = "8S"; break;
    case CV_16U: r = "16U"; break;
    case CV_16S: r = "16S"; break;
    case CV_32S: r = "32S"; break;
    case CV_32F: r = "32F"; break;
    case CV_64F: r = "64F"; break;
    default:     r = "User"; break;
  }

  return r;
}

string type2str(int type) {
  string r;

  uchar depth = type & CV_MAT_DEPTH_MASK;
  uchar chans = 1 + (type >> CV_CN_SHIFT);

  r = depth2str(depth);
  r += "C";
  r += (chans+'0');

  return r;
}

int main( int argc, char** argv )
{
    const int sz[] = {4,5,6};
    Mat M = Mat(3, sz, CV_16UC1);
    cv::randu(M, Scalar::all(0), Scalar::all(255));
    
    cout << "Mat::elemSize() = " << M.elemSize() << " Byte: same as sizeof(depth) * channels" << endl;
    cout << "Mat::elemSize1() = " << M.elemSize1() << " Byte: same as sizeof(depth)" << endl;
    cout << "Mat::depth() = " << depth2str(M.depth()) << endl;
    cout << "Mat::channels() = " << M.channels() << endl;
    cout << "Mat::type() = " << type2str(M.type()) << " : including both depth and channels, defined type number=" << M.type() << endl;
    cout << "Mat::dimension = " << M.dims << " --> rows = " << M.rows << ", cols = " << M.cols << endl;

    cout << endl << "\t\t|  Size[-1] = " << M.size[-1] << endl;
    for (int i=0; i<M.dims; i++) {
        cout << "Step[" << i << "] = " << M.step[i] << "\t|  Size[" << i << "] = " << M.size[i] << endl;
    }

    cout << endl;
    cout << "Mat::step1() = " << M.step1() << ": normalized step. matrix step divided by Mat::elemSize1()" << endl;
    cout << endl;

    return 0;
}

위 코드의 결과값은…

Mat::elemSize() = 2 Byte: same as sizeof(depth) * channels
Mat::elemSize1() = 2 Byte: same as sizeof(depth)
Mat::depth() = 16U
Mat::channels() = 1
Mat::type() = 16UC1 : including both depth and channels, defined type number=2
Mat::dimension = 3 –> rows = -1, cols = -1

                      | Size[-1] = 3
Step[0] = 60   | Size[0] = 4
Step[1] = 12   | Size[1] = 5
Step[2] = 2     | Size[2] = 6

Mat::step1() = 30: normalized step. matrix step divided by Mat::elemSize1()

참고
Advertisements

Written by gomiski

2014/03/13 at 5:23 am

One Response

Subscribe to comments with RSS.

  1. […] 가 되므로 유효한 표현식이 되는 것이다. 이 내용을 확인하고 싶다면 여기 참조. […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: