【转】Cvmat与IplImage的相互转换

seyvlei 原文地址

1、IplImage转Cvmat

   IplImage* src = cvLoadImage("rice.bmp",0);
   CvMat* mat=cvCreateMat(src->height,src->width,CV_32SC1);
   cvConvert(src,mat);

或者:

Cvmat matObj;
* mat=cvGetmat(src,&matObj);

2、Cvmat转IplImage

IplImage* pImg = cvCreateImage(cvGetSize(mat),8,1);
cvGetImage(matI,pImg);
cvSaveImage("rice1.bmp",pImg);

from:http://apps.hi.baidu.com/share/detail/19289229

使用函数cvGetMat和cvGetImage,首先要创建矩阵头,和图像头,数据共享。

例:

IplImage,CvMat圖形矩陣空間

#include <cv.h>
#include <highgui.h>
#include <stdio.h>

int main()
{
     IplImage *Image1;
     IplImage *Image2;
     CvMat *Matrix1;

     CvSize Size1;
     Image1=cvLoadImage("footpath.jpg",1);

     Size1=cvGetSize(Image1);

     Image2=cvCreateImageHeader(Size1,IPL_DEPTH_8U,3);
     Matrix1=cvCreateMatHeader(Size1.height,Size1.width,CV_8UC3);

     cvGetMat(Image1,Matrix1);

     cvFlip(Matrix1,Matrix1,1);

     cvGetImage(Matrix1,Image2);

     cvNamedWindow("footpath (Flip)",1);
     cvShowImage("footpath (Flip)",Image2);
     cvWaitKey(0);

     cvReleaseImage(&Image1);
     cvReleaseImageHeader(&Image2);
     cvReleaseMatHeader(&Matrix1);
     cvDestroyWindow("footpath (Flip)");
}

 

posted @ 2016-03-09 10:31  wenglabs  阅读(1214)  评论(0编辑  收藏  举报