opencv中cvarrcvmatiplimagecvmat和cvinputarray的相关总
结
1.CvArr* :
[cpp] view plain copy print?typedef void CvArr; 可以认为CvArr*是一个万能指针,例如某个函数参数是CvArr*,该函数内部会强制转换回该函数需要的数据类型,所以在调用该函数时,传入的参数类型就必须与该函数要求的类型一致,否则就会出错。 2.cv::Mat:
我们可以认为cv::Mat类型把向量、矩阵、图像等都统一了操作。cv::Mat有更强大的矩阵运算能力,支持常见的矩阵运算。对于图像数据的运算,将IplImage和CvMat类型转换成cv::Mat类型可大大提高运算效率(后面会将cv::Mat与IplImage和CvMat类型之间的转换)。 2.1 cv::Mat的一些操作
[cpp] view plain copy print?cv::Mat mat = imread(const string* filename); //读取图像 imshow(const string Window's name, mat); //显示图像 imwrite(const string&filename, mat); //将mat图像保存到固定路径中 3.IplImage:
现在OpenCV的很多处理图像的函数中都使用IplImage* 这
个数据类型,下面是它的一些重要的操作: [cpp] view plain copy print?IplImage* img =
cvLoadImage(PathName); //从路径中加载图像到img cvShowImage(\ //显示图像img cvWaitKey(); //按任意键退出窗口
4.CvMat:
因为CvMat是矩阵结构,无法像IplImage和Mat一样直接读取图像数据,而是要先创建Mat类的空矩阵
(cvCreateMat());再利用宏CV_MAT_ELEM()存放数据,或者提取数据。
注意:这个宏只针对单通道有用,多通道就会报错。 对于多通道的CvMat矩阵来说,要用cvSetND()对矩阵初始化,用cvGetND()来提取数据。
[cpp] view plain copy print?cvSetND(CvArr* arr, int idx0, int idy0, CvScalar value); cvSet2D(mat, 0, 0,
CvScalar(2,3,0,0)); //矩阵mat的(0,0)点的像素值为一通道2,二通道3.
5.IplImage转cv::Mat类型、CvMat转cv::Mat类型: 在VC++调用OpenCV处理图像时,经常用到的一些OpenCV函数的形参类型可能是cv::Mat、也可能是IplImage或者
CvMat,那么在图像数据传递的过程中就需要在IplImage和CvMat与cv::Mat之间进行转换。 5.1.1 IplImage转cv::Mat类型:
[cpp] view plain copy print?Mat MatImage(IplImage* img); //直接利用Mat定义一个Mat类矩阵,强制将括号内的IplImage*类型图像转换成Mat类 5.1.2 CvMat转cv::Mat类型:
[cpp] view plain copy print?Mat MatImage = Mat(CvMat* img, bool = true); //当bool=true时,会将img中的数据直接深拷贝到MatImage中,而bool=false时只创建与img相同的矩阵头 5.1.3 cv::Mat转CvMat类型:
[cpp] view plain copy print?CvMat CvMatImage =
CvMat(cv::Mat img); //直接将cv::Mat类型图像矩阵转换成CvMat类型 5.1.4 cv::Mat转IplImage类型:
[cpp] view plain copy print?IplImage IPLimage = MatImage; 5.2 IplImage类型图像拷贝到另一个IplImage类型图像中: [cpp] view plain copy print?IplImage* img1 =
cvLoadImage(pathName, 2); //img1指针指向路径中的图像,参数=2时加载原始的灰度图 IplImage* img2 = cvCloneImage(&img1); //将img1指针指向的图像引用,并复制到IplImage* img2,img2也是指向该图像的指针类型 6.OpenCV中的一些常用的函数及其参数类型
的总结:
[cpp] view plain copy print?GaussianBlur(cv::InputArray src, cv::outputArray dst, cv::Size, 0,0); //高斯滤波
通常导入的图像为cv::Mat类型,输出的高斯滤波后的图像也是cv::Mat类型图像,cv::Size通常用Size(3,3)。 [cpp] view plain copy print?HoughCircles(cv::InputArray image, cv::OutputArray circles, int method, double dp, double minDist, double param1, double param2,int minRadius, int maxRadius); [cpp] view plain copy print?//通常输入图像为cv::Mat类型,输出的circles定义为vector<Vec3f> circles,用于存放找到的圆的圆心x,y坐标和圆的半径; [cpp] view plain copy print?//dp =1为最小分辨率,minDist=1为允许找到的圆的圆心之间相隔的最小距离,param1和param2根据具体图像灰度来确定最优值。 [cpp] view plain copy print?CvMat* img =
cvCreateMatHeader(int rows, int cols,CV_8UC1);//CvMat* cvCreateMatHeader(); [cpp] view plain copy print?CvRect rect = cvRect(int x, int y, int width, int height); //定义矩形框的尺寸 cvGetSubRect(IplImage* img,CvMat* submat, CvRect rect); //将原始IplImage*类型图像按照rect尺寸截出矩形框出来,并传给CvMat* submat输出。
[cpp] view plain copy print?GradValue_level.at<float>(0,i) = (float)CV_MAT_ELEM( *averageValue_level, float,0, i+1 ) -(float)CV_MAT_ELEM( *averageValue_level, float,0, i ); [cpp] view plain copy print?//对于CvMat*类型矩阵通过CV_MAT_ELEM宏提取像素值或修改像素值,对于cv::Mat类图像矩阵通过MatImage.at<type>(x,y)进行像素提取或修改像素值
[cpp] view plain copy print?rectangle(cv::Mat &img, Rect(Point, Size(int width,int height)),cv::Scalar &color, int thickness =1,int lineType = 8, int shift = 0); [cpp] view plain copy print?//在输入图像cv::Mat img上画出矩形框 [cpp] view plain copy print?circle(cv::Mat &img, cv::Point center, int Radius,const cv::Scalar &color, int thickness=1,int lineType =8, int shift=0); [cpp] view plain copy print?//在图像cv::Mat img上画出圆心为Point center,半径为Radius的圆。