Pages

Saturday, August 4, 2012

How to convert opencv IplImage to cv::Mat and cv::Mat to IplImage

Hey guys

     This is a tutorial for converting OpenCV image handlers 

*IplImage to cv::Mat
*cv::Mat to IplImage

IplImage to cv::Mat example program

Ipltomat.cpp

 ///Opencv 2.4.2   
 #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;  
 }  




Command for compiling opencv program in Ubuntu

g++ Ipltomat.cpp `pkg-config opencv --cflags --libs` -o Ipltomat


cv::Mat to IplImage example program

MattoIpl.cpp


 ///Opencv 2.4.2  
 #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;  
 }  

Compiling command


g++ MattoIpl.cpp `pkg-config opencv --cflags --libs` -o MattoIpl


Output






3 comments:

  1. Thanks for this article.
    I would be able to run the IplImage to Mat conversion but I am getting problem
    with mat to IplImage program

    I am running the following code in VS 2008 using OpenCV 2.4.6

    #include "stdafx.h"

    #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);
    Mat image = imread("C:\\Users\\JOSHI\\Desktop\\Images\\2.png",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;
    }


    while running above program it shows a message box with the message
    "
    An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred in test.exe

    Additional information: External component has thrown an exception.
    Break or Continue
    "
    at this line imshow("Mat",image);

    can you tell me what's the problem.

    ReplyDelete
  2. Hi PRAKASH, use the follow code to convert Mat to iplimage

    //convert Mat to IplImage
    IplImage* Mat2IplImage(Mat mat) {
    IplImage* image;
    image = cvCreateImage(cvSize(mat.cols, mat.rows), 8, 3);
    IplImage ipltemp = mat;
    cvCopy(&ipltemp, image);
    return image;
    }

    Good Luck

    ReplyDelete
  3. Hi PRAKASH, use the follow code to convert Mat to iplimage

    //convert Mat to IplImage
    IplImage* Mat2IplImage(Mat mat) {
    IplImage* image;
    image = cvCreateImage(cvSize(mat.cols, mat.rows), 8, 3);
    IplImage ipltemp = mat;
    cvCopy(&ipltemp, image);
    return image;
    }

    Good Luck

    ReplyDelete