opencv (opencv3.X) IplImage相互转换mat

时间:2022-04-30 15:39:34

opencv3.x之前的版本,mat有构造函数 Mat(const IplImage* img, bool copyData=false);
IplImage转mat可以直接用由

extern  IplImage * plpliamge;//plpliamge已创建
cv::Mat matImage( IplImage, 0 ): //第二个参数表示不进行像素数据copy;

实现IplImage转为mat

但是在opencv3.x中,Mat(const IplImage* img, bool copyData=false);构造函数取消了,所以只能另辟蹊径啦!下面是在3.x中可行办法:

1. IplImage to cv::Mat example program

 #include "opencv2/highgui/highgui.hpp" 
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
int main(int argc,
char *argv[]
)
{
///Loading image to IplImage
IplImage *img=cvLoadImage(argv[1]);
cvShowImage("Ipl",img);
///converting IplImage to cv::Mat
Mat image=cvarrToMat(img);
imshow("Mat",image);
waitKey();
return 0;
}

2. cv::Mat to IplImage example program

 #include "opencv2/highgui/highgui.hpp" 
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
int main(int argc,
char *argv[]
)
{
///Reading Image to cv::Mat
Mat image =imread(argv[1],1);
///Converting Mat to IplImage
IplImage test = image;
///showing image from mat
imshow("Mat",image);
///showing image from IplImage
cvShowImage("Ipl",&test);
waitKey();
return 0;
}