opencv (opencv3.X) IplImage相互轉換mat

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;  
 }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章