[OpenCV] Samples 02: Mat - 图像矩阵

前言


一、简介

Ref:IplImage, CvMat, Mat 的关系

Mat是opencv2.0推出的处理图像的新的数据结构,现在越来越有趋势取代之前的cvMat和lplImage

相比之下Mat最大的好处就是能够更加方便的进行内存管理,不再需要程序员手动管理内存的释放。

opencv2.3中提到Mat是一个多维的密集数据数组,可以用来处理向量和矩阵、图像、直方图等等常见的多维数据。 

 

/* implement */

 

 

 

基本操作


操作Mat元素时:I.at<double>(1,1) = CV_PI;

本博客原内容

/*
 *
 * cvout_sample just demonstrates the serial out capabilities of cv::Mat
 *  That is, cv::Mat M(...); cout << M;  Now works.
 *
 */

#include "opencv2/core/core.hpp"
#include <iostream>

using namespace std;
using namespace cv;

static void help()
{
    cout
    << "\n------------------------------------------------------------------\n"
    << " This program shows the serial out capabilities of cv::Mat\n"
    << "That is, cv::Mat M(...); cout << M;  Now works.\n"
    << "Output can be formated to OpenCV, matlab, python, numpy, csv and \n"
    << "C styles Usage:\n"
    << "./cvout_sample\n"
    << "------------------------------------------------------------------\n\n"
    << endl;
}


int main(int argc, char** argv)
{
    cv::CommandLineParser parser(argc, argv, "{help h||}");
    if (parser.has("help"))
    {
        help();
        return 0;
    }

    /**************************************************************************/
    // Jeff --> Define Diagnal Mat.
    Mat I = Mat::eye(4, 4, CV_64F);
    I.at<double>(1,1) = CV_PI;
    cout << "I = \n" << I << ";" << endl << endl;

    /*------------------------------------------------------------------------*/
    Mat r = Mat(10, 3, CV_8UC3);
    randu(r, Scalar::all(0), Scalar::all(255));

    // Jeff --> Matrix Format transform.
    cout << "r (default) = \n" << r << ";" << endl << endl;
    cout << "r (matlab)  = \n" << format(r, Formatter::FMT_MATLAB) << ";" << endl << endl;
    cout << "r (python)  = \n" << format(r, Formatter::FMT_PYTHON) << ";" << endl << endl;
    cout << "r (numpy)   = \n" << format(r, Formatter::FMT_NUMPY)  << ";" << endl << endl;
    cout << "r (csv)     = \n" << format(r, Formatter::FMT_CSV)    << ";" << endl << endl;
    cout << "r (c)       = \n" << format(r, Formatter::FMT_C)      << ";" << endl << endl;

    /**************************************************************************/
    Point2f p(5, 1);
    cout << "p = " << p << ";" << endl;

    /*------------------------------------------------------------------------*/
    Point3f p3f(2, 6, 7);
    cout << "p3f = " << p3f << ";" << endl;

    /**************************************************************************/
    // Jeff --> vector.
    vector<float> v;
    v.push_back(1.1);
    v.push_back(2.2);
    v.push_back(3.3);

    cout << "shortvec = " << Mat(v) << endl;

    /*------------------------------------------------------------------------*/
    vector<Point2f> points(20);
    for (size_t i = 0; i < points.size(); ++i)
        points[i] = Point2f((float)(i * 5), (float)(i % 7));

    cout << "points = " << points << ";" << endl;
    return 0;
}

 


Result
: Matrix Format for不同的工具。

r (default) = 
[ 91,   2,  79, 179,  52, 205, 236,   8, 181;
 239,  26, 248, 207, 218,  45, 183, 158, 101;
 102,  18, 118,  68, 210, 139, 198, 207, 211;
 181, 162, 197, 191, 196,  40,   7, 243, 230;
  45,   6,  48, 173, 242, 125, 175,  90,  63;
  90,  22, 112, 221, 167, 224, 113, 208, 123;
 214,  35, 229,   6, 143, 138,  98,  81, 118;
 187, 167, 140, 218, 178,  23,  43, 133, 154;
 150,  76, 101,   8,  38, 238,  84,  47,   7;
 117, 246, 163, 237,  69, 129,  60, 101,  41];

r (matlab) = 
(:, :, 1) = 
 91, 179, 236;
239, 207, 183;
102,  68, 198;
181, 191,   7;
 45, 173, 175;
 90, 221, 113;
214,   6,  98;
187, 218,  43;
150,   8,  84;
117, 237,  60
(:, :, 2) = 
  2,  52,   8;
 26, 218, 158;
 18, 210, 207;
162, 196, 243;
  6, 242,  90;
 22, 167, 208;
 35, 143,  81;
167, 178, 133;
 76,  38,  47;
246,  69, 101
(:, :, 3) = 
 79, 205, 181;
248,  45, 101;
118, 139, 211;
197,  40, 230;
 48, 125,  63;
112, 224, 123;
229, 138, 118;
140,  23, 154;
101, 238,   7;
163, 129,  41;

r (python) = 
[[[ 91,   2,  79], [179,  52, 205], [236,   8, 181]],
 [[239,  26, 248], [207, 218,  45], [183, 158, 101]],
 [[102,  18, 118], [ 68, 210, 139], [198, 207, 211]],
 [[181, 162, 197], [191, 196,  40], [  7, 243, 230]],
 [[ 45,   6,  48], [173, 242, 125], [175,  90,  63]],
 [[ 90,  22, 112], [221, 167, 224], [113, 208, 123]],
 [[214,  35, 229], [  6, 143, 138], [ 98,  81, 118]],
 [[187, 167, 140], [218, 178,  23], [ 43, 133, 154]],
 [[150,  76, 101], [  8,  38, 238], [ 84,  47,   7]],
 [[117, 246, 163], [237,  69, 129], [ 60, 101,  41]]];

r (numpy) = 
array([[[ 91,   2,  79], [179,  52, 205], [236,   8, 181]],
       [[239,  26, 248], [207, 218,  45], [183, 158, 101]],
       [[102,  18, 118], [ 68, 210, 139], [198, 207, 211]],
       [[181, 162, 197], [191, 196,  40], [  7, 243, 230]],
       [[ 45,   6,  48], [173, 242, 125], [175,  90,  63]],
       [[ 90,  22, 112], [221, 167, 224], [113, 208, 123]],
       [[214,  35, 229], [  6, 143, 138], [ 98,  81, 118]],
       [[187, 167, 140], [218, 178,  23], [ 43, 133, 154]],
       [[150,  76, 101], [  8,  38, 238], [ 84,  47,   7]],
       [[117, 246, 163], [237,  69, 129], [ 60, 101,  41]]], dtype='uint8');

r (csv) = 
 91,   2,  79, 179,  52, 205, 236,   8, 181
239,  26, 248, 207, 218,  45, 183, 158, 101
102,  18, 118,  68, 210, 139, 198, 207, 211
181, 162, 197, 191, 196,  40,   7, 243, 230
 45,   6,  48, 173, 242, 125, 175,  90,  63
 90,  22, 112, 221, 167, 224, 113, 208, 123
214,  35, 229,   6, 143, 138,  98,  81, 118
187, 167, 140, 218, 178,  23,  43, 133, 154
150,  76, 101,   8,  38, 238,  84,  47,   7
117, 246, 163, 237,  69, 129,  60, 101,  41
;

r (c) = 
{ 91,   2,  79, 179,  52, 205, 236,   8, 181,
 239,  26, 248, 207, 218,  45, 183, 158, 101,
 102,  18, 118,  68, 210, 139, 198, 207, 211,
 181, 162, 197, 191, 196,  40,   7, 243, 230,
  45,   6,  48, 173, 242, 125, 175,  90,  63,
  90,  22, 112, 221, 167, 224, 113, 208, 123,
 214,  35, 229,   6, 143, 138,  98,  81, 118,
 187, 167, 140, 218, 178,  23,  43, 133, 154,
 150,  76, 101,   8,  38, 238,  84,  47,   7,
 117, 246, 163, 237,  69, 129,  60, 101,  41};

  

posted @ 2016-11-25 16:26  郝壹贰叁  阅读(426)  评论(0编辑  收藏  举报