OpenCV数据格式转换

在mfc c++ 以及opencv 编写程序当中,很多常用的类型转换,现在总结一下。(注意加相应的头文件,这里不罗嗦)

提纲:

1. Mat ---> Iplimage

2. Iplimage  --->  CvvImage

3. Mat  ---> vector<Point2f> or vector<Point3f>

4. vector<Point2f> or vector<Point3f>  --->  vector<vector<Point2f>> or vector<vector<Point3f>>

5. vector<vector<Point2f>> or vector<vector<Point3f>>  ---> Mat

6. vector<Point2f> or vector<Point3f>  --->  Mat

图像类

1. Mat ---> Iplimage :直接赋值 

Mat img;  
Iplimage myImg = img;  

2. Iplimage  --->  CvvImage :用“Copyof ”

CvvImage cImg;  
Iplimage myimg;  
cImg.Copyof(myimg, -1);  

数据类

3. Mat  ---> vector<Point2f> or vector<Point3f> :用“Mat_<Point2f>“ ,“Mat_<Point3f>”

Mat m;  
vector<Point3f> p;  
p = Mat_<Point3f>(m);  

4. vector<Point2f> or vector<Point3f>  --->  vector<vector<Point2f>> or vector<vector<Point3f>> :用“pushback”

vector<Point3f> p1,p2,p3;
vector<vector<Point3f>> pp;
pp.pushback(p1);
pp.pushback(p2);
pp.pushback(p3);

5. vector<vector<Point2f>> or vector<vector<Point3f>>  ---> Mat

复制代码
vector<vector<Point3f>> p;
Mat pm((int)p.size(), p[0].size(), CV_32FC3);

for( int i = 0; i < (int)p.size(); i++ )
{
    Mat r = pm.row(i).reshape(3, pm.cols);
    Mat pm1(p[i]);
    pm1.copyTo(r);
}
复制代码

6. vector<Point2f> or vector<Point3f>  --->  Mat :用“Mat(Point3f)"

vector<Point3f> p;
Mat m = Mat(p);
 
 
 
 
 
 
 
 
7. IplImage -> Mat:
  
  
IplImage* imgSrc= &IplImage(imgSrc1);
Mat *imgSrc1=&Mat(imgSrc,0);
IplImage pImg= IplImage(imgMat);//Mat转Ipl
Mat img(pImg,0);//0为不复制数据,共同记忆位置


8.mat转CvMat:
Mat a,
CvMat *b = cvCreateMat(..........);
CvMat temp = a;
cvCopy(&temp, b)
或者
Mat b;
CvMat a=b;
&a即为CvMat* 



    
    
IplImage* oldC0 = cvCreateImage(cvSize(320,240),16,1);
Mat newC = cvarrToMat(oldC0);
IplImage oldC1 = newC;
CvMat oldC2 = newC;
这是为了把经典的OpenCV图像导成矩阵,第一句创建一个320×240的图像;第二句话把IplImage转成Mat;第三句话把Mat转成IplImage;第四句把Mat转成CvMat。
Mat newC2 = cvarrToMat(oldC0).clone();
转换后克隆一下赋值。
都知道,2.0版本对之前的OpenCV数据结构进行了大幅度的修改。但对之前版本的兼容是一个很重要的事情。这节就主要讨论这个问题
首先来看一下2.0版本对之前版本的进行了哪些修改
1.采用了新的数据结构Mat作为图像的容器,取代了之前的CvMat和lplImage,这个改动不是太复杂,只需适应一下新东西,而且可以自由转换
[cpp]  view plain copy
  1. Mat I;  
  2. IplImage pI = I;  
  3. CvMat mI = I;  

对于指针的操作要相对复杂一些,而且还要注意内存的释放,我这里不推荐用老版本的数据结构,例如:
[cpp]  view plain copy
  1. Mat I;  
  2. IplImage* pI = &I.operator IplImage();  
  3. CvMat* mI = &I.operator CvMat();  

2.对library进行了重组,将原来的一个大库根据功能结构分成具体小库,这样包含头文件的时候只需要加入你需要的库,只是原来库的子集
3.使用了cv 这个namespace来防止和其他的library 结构冲突。所以在使用的时候也要预先加上cv::关键字,这也是新版本的函数,数据都省略了cv前缀的原因,一般放在include之后,格式为:
[cpp]  view plain copy
  1. using namespace cv; // The new C++ interface API is inside this namespace. Import it. 
 
 
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值