把 CvMat, IplImage ,或者CvMatND 转化为 Mat.

C++: Mat cvarrToMat(const CvArr* arr, bool copyData=false, bool allowND=true, int coiMode=0 )

Parameters:


  • arr – input CvMatIplImage , or CvMatND.
  • copyData – when false (default value), no data is copied and only the new header is created, in this case, the original array should not be deallocated while the new matrix header is used; if the parameter is true, all the data is copied and you may deallocate the original array right after the conversion.
  • allowND – when true (default value), CvMatND is converted to 2-dimensional Mat, if it is possible (see the discussion below); if it is not possible, or when the parameter is false, the function will report an error.
  • coiMode –

    parameter specifying how the IplImage COI (when set) is handled.

    • If coiMode=0 and COI is set, the function reports an error.
    • If coiMode=1 , the function never reports an error. Instead, it returns the header to the whole original image and you will have to check and process COI manually. See extractImageCOI() .
CvMat* A = cvCreateMat(10, 10, CV_32F);
cvSetIdentity(A);
IplImage A1; cvGetImage(A, &A1);
Mat B = cvarrToMat(A);
Mat B1 = cvarrToMat(&A1);
IplImage C = B;
CvMat C1 = B1;
// now A, A1, B, B1, C and C1 are different headers
// for the same 10x10 floating-point array.
// note that you will need to use "&"
// to pass C & C1 to OpenCV functions, for example:
printf("%g\n", cvNorm(&C1, 0, CV_L2));
1、示例(IplImage -> Mat)
IplImage* transIplImage;  

Mat m = cvarrToMat(transIplImage,true); 

2、CvMat与IplImage相互转换
IplImage* transIplImage;  
CvMat* cvmat;   
cvGetMat(transIplImage,cvmat); 
cvGetImage(cvmat,transIplImage);

       
       

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
	IplImage*image=cvLoadImage("lenab.jpg");
	cvNamedWindow("原图1");
	cvShowImage("原图1",image);
	Mat srcImage(image);
	namedWindow("原图2");
	imshow("原图2",srcImage);
	waitKey();
	cvReleaseImage(&image);
	cvDestroyWindow("原图1");
	return 0;
}

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
	IplImage*image=cvLoadImage("E:\\图片\\lena.jpg");
	cvNamedWindow("原图1");
	cvShowImage("原图1",image);
	Mat srcImage=cvarrToMat(image);//IplImage转化为Mat
	namedWindow("原图2");
	imshow("原图2",srcImage);
	waitKey();
	cvReleaseImage(&image);
	cvDestroyWindow("原图1");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值