2012-02-07 62 views
2

我想将iplimage转换为cv :: mat(不是CvMat)。有了这个代码的价值主题溢出...iplimage to mat 32位转换错误

IplImage mhi32f = cvCreateImage(cvSize(draw_rect.width,draw_rect.height), IPL_DEPTH_32F, 1); 
cv::Mat mhi32_mat(mhi32f); 
mhi32_mat.convertTo(mhi32_mat,CV_32FC1); 

有什么建议吗?

回答

2

为解释here,你就必须做到这一点

Mat imgMat(iplimg); //Construct an Mat image "img" out of an IplImage 
+0

与垫imgMat(iplimg,真);它是一个副本!谢谢! – Phil 2012-02-08 20:28:38

2

首先,IplImage mhi32f = ...应该是IplImage* mhi32f = ...,但我会认为这是您的错误。

你的例子很好,除了你不需要convertTo调用。如果要将IplImage数据复制到Mat对象,只需将true作为第二个参数传递给构造函数,如here所示。

此处给出了一个类型是已CV_32FC1一个例子:

#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/imgproc/imgproc.hpp> 
#include <iostream> 

using namespace std; 
using namespace cv; 

int main(int argc, char** argv) 
{ 
    IplImage* mhi32f = cvCreateImage(cvSize(320, 240), IPL_DEPTH_32F, 1); 
    cv::Mat mhi32_mat(mhi32f); 

    assert(mhi32_mat.type() == CV_32FC1); 

    cout << "Already a CV_32FC1 matrix..." << endl; 

    return 0; 
} 

希望有所帮助。