矩阵和图像的操作
(1)cvInRange函数
其结构
void cvInRange(//提取图像中在阈值中间的部分 const CvArr* src,//目标图像 const CvArr* lower,//阈值下限 const CvArr* upper,//阈值上限 CvArr* dst//结果图像 );
#include <cv.h> #include <highgui.h> #include <stdio.h> #include <iostream> using namespace std; int main() { IplImage *src1,*src2,*dst11,*dst12,*dst13,*dst21,*dst22,*dst23; src1=cvLoadImage("5.jpg"); src2=cvLoadImage("7.jpg"); dst11 = cvCreateImage(cvSize(src1->width, src1->height), IPL_DEPTH_8U, 1); dst12 = cvCreateImage(cvSize(src1->width, src1->height), IPL_DEPTH_8U, 1); dst13 = cvCreateImage(cvSize(src1->width, src1->height), IPL_DEPTH_8U, 1); dst21 = cvCreateImage(cvSize(src2->width, src1->height), IPL_DEPTH_8U, 1); dst22 = cvCreateImage(cvSize(src2->width, src1->height), IPL_DEPTH_8U, 1); dst23 = cvCreateImage(cvSize(src2->width, src1->height), IPL_DEPTH_8U, 1); cvSplit(src1, dst11, dst12, dst13, 0); cvSplit(src2, dst21, dst22, dst23, 0); cvInRange(dst12,dst21,dst23,dst23); cvShowImage( "原图", src1); cvShowImage("过滤图",src2); cvShowImage( "结果图", dst23); cvWaitKey(); return 0; }
(2)cvInRangeS函数
其结构
void cvInRangeS(//提取图像中在阈值中间的部分 const CvArr* src,//目标图像 CvScalar lower,//阈值下限 CvScalar upper,//阈值上限 CvArr* dst//结果图像 );
#include <cv.h> #include <highgui.h> #include <stdio.h> #include <iostream> using namespace std; int main() { IplImage *src1,*src2,*dst11,*dst12,*dst13,*dst21,*dst22,*dst23; src1=cvLoadImage("5.jpg"); dst11 = cvCreateImage(cvSize(src1->width, src1->height), IPL_DEPTH_8U, 1); dst12 = cvCreateImage(cvSize(src1->width, src1->height), IPL_DEPTH_8U, 1); dst13 = cvCreateImage(cvSize(src1->width, src1->height), IPL_DEPTH_8U, 1); CvScalar cs1; cs1.val[0] = 0; cs1.val[1] = 0; cs1.val[2] = 0; cs1.val[3] = 0; CvScalar cs2; cs2.val[0] = 80; cs2.val[1] = 0; cs2.val[2] = 0; cs2.val[3] = 0; cvSplit(src1, dst11, dst12, dst13, 0); cvInRangeS(dst11,cs1,cs2,dst13); cvShowImage( "原图", src1); cvShowImage( "变单通过程图", dst11); cvShowImage( "结果图", dst13); cvWaitKey(); return 0; }
(3)cvInvert函数
其结构
double cvInvert(//矩阵取逆 const CvArr* src,//目标矩阵 CvArr* dst,//结果矩阵 int method = CV_LU//逆运算方法 );
方法的参数值 | 含义 |
CV_LU | 高斯消去法 |
CV_SVD | 奇异值分解 |
CV_SVD_SYM | 对称矩阵的SVD |
#include <cv.h> #include <highgui.h> #include <stdio.h> #include <iostream> using namespace std; int main() { double a[3][3] = { {1,0,0}, {0,2,0}, {0,0,3} }; CvMat va = cvMat(3,3, CV_64FC1,a); cout<<"目标矩阵:"<<endl; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) printf("%f\t",cvmGet(&va,i,j)); cout << endl; } cvInvert(&va,&va); cout << "其逆矩阵为:"; cout<<endl; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) printf("%f\t",cvmGet(&va,i,j)); cout << endl; } getchar(); return 0; }
(4)cvMahalonobis函数
其结构
CvSize cvMahalonobis(//计算马氏距离 const CvArr* vec1,//样本向量 const CvArr* vec2,//平均值 CvArr* mat//协方差的逆 );
ps:关于什么是马氏向量,我也研究了半天,找了一些资料算是弄明白了个大概。
关于马氏距离,定义的话自己百度百科就行。
关于理解和解释请参照博客:点击打开链接 通俗易懂
关于下面的代码实例的数据来源:点击打开链接
实例代码
#include <cv.h> #include <highgui.h> #include <stdio.h> #include <iostream> using namespace std; int main() { double a1[4] = {3,4,5,6}; double a2[4] = {2,2,8,4}; double b[4] = {2.5, 3, 6.5, 5}; double c[4][4] = { {0.25,0.50,-0.75,0.50}, {0.50,1.00,-1.50,1.00}, {-0.75,-1.50,2.25,-1.50}, {0.50,1.00,-1.50,1.00} }; CvMat va1 = cvMat(1,4, CV_64FC1,a1); CvMat va2 = cvMat(1,4, CV_64FC1,a2); CvMat vb = cvMat(1,4, CV_64FC1,b); CvMat vc = cvMat(4,4, CV_64FC1,c); cvInvert(&vc,&vc,CV_SVD); //协方差取逆,这个函数前面讲过 double r1 = cvMahalanobis(&va1,&vb,&vc); double r2 = cvMahalanobis(&va2,&vb,&vc); cout << "样本1的马氏距离:"<<endl; cout<<r1<<endl; cout << "样本2的马氏距离:"<<endl; cout<<r2<<endl; getchar(); return 0; }
to be continued
《学习opencv》笔记——矩阵和图像操作——cvInRange,cvInRangeS,cvInvert and cvMahalonobis,布布扣,bubuko.com
《学习opencv》笔记——矩阵和图像操作——cvInRange,cvInRangeS,cvInvert and cvMahalonobis
原文:http://blog.csdn.net/zhurui_idea/article/details/28630589