zoukankan      html  css  js  c++  java
  • OpenCV基本图像容器Mat的几种创建方法

    參考文章:http://www.cnblogs.com/tornadomeet/archive/2012/07/19/2599376.html

    实验说明: (引用)

      本文主要讲一些opencv 2.0版本号后出现的Mat矩形类,主要是參考opencv自带doc目录下的tutiol教材。通过这次实验认为用Mat的话以下几点须要特别注意(在代码中能够体现出来):

    1. 利用create函数又一次改变Mat数据在内存中的布局。

     

    2. 注意多通道数据在Mat中事实上也是占一个元素的位置的。 
    3. 学会多维Mat的创建方法。
    4. 当Mat矩阵比較小时。学会直接赋值的方法,即用Mat_。
    5. 掌握Mat矩阵内容输出到终端时的几种常见格式。

    6. 注意假设vector是单独一维的话须要转换成Mat才干输出。多维的能够直接输出,比如vector里面存放的是点的话。

    #include <iostream>
    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    
    using namespace std;
    using namespace cv;
    
    int demo_Mat()
    {
        int next_num = 1;
    	char tmp;
    	while(next_num<7)
    	{
    		cout<<"-------- "<<next_num<<" --------"<<endl;
    		waitKey(0);
    		
    		switch (next_num)
            {
               case 1:
                {
    				Mat M( 2, 2, CV_8UC3, Scalar(0,255,0) );//事实上是2*6的矩阵,由于每一个元素有3个通道。

    cout<<"create by using the constructor..."<<endl; cout<<"M = "<<M<<endl; M.create( 4, 4, CV_8UC(2));//括号中面的2表示2通道 cout<< "create by using create function..."<<endl; cout<<"M = "<<M<<endl; break; }//当case语句里面变量定义时,须要用括号括起来,否则会报错 case 2: { int sz[3] = {2, 2, 2}; Mat L( 3, sz, CV_8UC(1), Scalar::all(0) ); cout<< "create multidimensional matrix..."<<endl; // cout<<"L = "<<L<<endl;此处不能打印出来,由于那仅仅适应二维数组 break; } case 3: { Mat E = Mat::eye(4, 4, CV_64F); Mat O = Mat::ones(2, 3, CV_32F); Mat Z = Mat::zeros(3, 3, CV_8UC1); cout<< "using matlab stytle..."<<endl; cout<<"E = "<<E<<endl; cout<<"O = "<<O<<endl; cout<<"Z = "<<Z<<endl; break; } case 4: { Mat C =(Mat_<double>(3,3)<<0,-1,0,-1,5,-1,0,-1,0);//直接赋初始值的方法 Mat row_clone = C.row(1).clone(); cout<< "create 3*3 double-precision identity matrix..."<<endl; cout<<"C = "<<C<<endl; cout<<"row_clone = "<<row_clone<<endl; break; } case 5: { Mat R = Mat( 3, 2, CV_8UC3 ); randu( R, Scalar::all(0), Scalar::all(255) ); cout<< "fill a matrix with rand numbers..."<<endl; cout<<"R (default) = "<<R<<endl; cout<< "demonstrate the output formating options..."<<endl; cout<<"R (python) = "<<format(R, "python")<<endl; cout<<"R (numpy) = "<<format(R, "numpy")<<endl;//numpy是一个用python实现的科学计算包 cout<<"R (csv) = "<<format(R, "csv")<<endl;//csv,逗号分隔符 cout<<"R (c) = " <<format(R, "C")<<endl; break; } case 6: { cout<< "the point format output..."<<endl; Point2f P1(5, 1); cout<<"Point (2D) = "<<P1<<endl; Point3f P2(4, 5, 6); cout<<"Point (3D) = "<<P2<<endl; vector<float>v; v.push_back( (float)CV_PI); v.push_back( 2 );//push_back为在其尾部增加一个数据 v.push_back( 3.01f ); cout<<"vector of float: = "<<Mat(v)<<endl;//vector数据是没法单独输出的,因此能够借助mat输出 int N = 20; vector<Point2f>vPoints(N);//vector能够用变量定义其长度。比数组好用 for( size_t E = 0; E < vPoints.size(); ++E ) //size_t事实上就是一个unsigned int类型 vPoints[E] = Point2f((float)(E*5), (float)(E%7)); cout<<"vPoints[] = "<<vPoints<<endl;//可是vector点确实能够直接输出的。由于这时候的vector本身就是 //一个多维(至少2维)Mat了 break; } default: break; } next_num++; } return 0; }


    执行结果:


      


  • 相关阅读:
    文件
    模块
    异常
    抽象
    条件,循环
    centos6.4 64位下安装nfs文件共享系统
    YUM安装删除软件
    apache常用的两种工作模式 prefork和worker
    shell不打印
    APACHE安装配置说明
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/7112143.html
Copyright © 2011-2022 走看看