《学习opencv》笔记——矩阵和图像处理——cvMinManLoc,cvMul,cvNot,cvNorm and cvNormalize


矩阵和图像的操作


(1)cvMinManLoc函数

其结构


void cvMinMaxLoc(//取出矩阵中最大最小值
	const CvArr* arr,//目标矩阵
	double* min_val,//最小值
	double* max_val,//最大值
	CvPoint* min_loc = NULL,//最小值位置
	CvPoint* max_loc = NULL,//最大值位置
	const CvArr* mask = NULL//矩阵“开关”
);

实例代码


#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <iostream>
using namespace std;


int main() 
{ 
	double a[3][3] = 
	{    
		{1,2,3},
		{4,5,6},
		{7,8,9}
	};

	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;
	}

	double min_Val,max_Val;

	cvMinMaxLoc(&va,&min_Val,&max_Val);

	cout << "最小值为:" << endl;
	cout << min_Val << endl;

	cout << "最大值为:" << endl;
	cout << max_Val << endl;

	getchar();
	return 0;

}

输出代码




(2)cvMul函数

其结构


void cvMul(//两个矩阵相应元素相乘
	const CvArr* src1,//矩阵1
	const CvArr* src2,//矩阵2
	CvArr* dst,//结果矩阵
	double scale = 1//因子系数
);


实例代码


#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <iostream>
using namespace std;


int main() 
{ 
	double a[3][3] = 
	{    
		{1,2,3},
		{4,5,6},
		{7,8,9}
	};

	CvMat va = cvMat(3,3, CV_64FC1,a);

	double b[3][3] = 
	{    
		{2,1,2},
		{1,2,1},
		{2,1,2}
	};

	CvMat vb = cvMat(3,3, CV_64FC1,b);

	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;
	}

	cout<<"因子矩阵:"<<endl;

	for(int i=0;i<3;i++)
	{
		for(int j=0;j<3;j++)
			printf("%f\t",cvmGet(&vb,i,j));
		cout << endl;
	}

	cvMul(&va,&vb,&va);

	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;

}

输出结果




(3)cvNot函数

其结构


void cvNot(//元素按位取反
	const CvArr* src,//目标矩阵
	CvArr* dst//结果矩阵
);

实例代码


#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <iostream>
using namespace std;

int main() 
{
	IplImage *src1,*src2;
	src1=cvLoadImage("5.jpg");
	src2=cvLoadImage("7.jpg");
	cvNot(src1,src2);
	cvShowImage( "原图", src1);
	cvShowImage( "结果图", src2);
	cvWaitKey();
	return 0;
}

输出结果




(4)cvNorm函数

其结构


double cvNorm(//计算各种范式
	const CvArr* arr1,//矩阵1
	const CvArr* arr2 = NULL,//矩阵2
	int norm_type = CV_L2,//选择范式标量
	const CvArr* mask = NULL//矩阵“开关”
);


ps:arr2=NULL时,对于不同的norm_type由cvNorm()计算范式的公式




 arr2非空。且norm_type不同值时函数cvNorm()计算范数的计算公式


  


实例代码


#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <iostream>
using namespace std;


int main() 
{ 
	double a[3][3] = 
	{    
		{1,2,3},
		{4,-12,6},
		{7,8,9}
	};

	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;
	}


	double dis = cvNorm(&va,NULL,CV_C,NULL);

	cout<<"结果:"<<endl;

	cout<< dis <<endl;
	

	getchar();
	return 0;

}

输出结果




(5)cvNormalize函数

其结构


void cvNormalize(
	const CvArr* src, //目标矩阵
	CvArr* dst,//结果矩阵
	double a = 1.0 // 归一化区间上限
	double b = 0.0 // 归一化区间下限
	int norm_type = CV_L2,//选择归一化标量
	const CvArr* mask //矩阵“开关”
);


ps:函数cvNormalize()的參数norm_type可能的值



实例代码


#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,2}
	};

	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;
	}


	cvNormalize(&va,&va,1,0,CV_C);

	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;
}

输出结果




to be continued

版权声明:本文博客原创文章,博客,未经同意,不得转载。

posted @ 2015-08-09 13:38  hrhguanli  阅读(796)  评论(0编辑  收藏  举报