首页 > 代码库 > 《学习opencv》笔记——矩阵和图像操作——cvAbs,cvAbsDiff and cvAbsDiffS

《学习opencv》笔记——矩阵和图像操作——cvAbs,cvAbsDiff and cvAbsDiffS

矩阵和图像的操作


(1)cvAbs。cvAbsdiff。cvAbsDiffS

它们的结构为:

void cvAbs(  //取src中元素的绝对值。写到dst中
  const CvArr* src,
  const dst
);

void cvAbsDiff( //src1减去src2的差的绝对值存入dst
  const CvArr* src1,
  const CvArr* src2,
  const dst
);

void cvAbsDiffs( //src中每一个元素减去value存入dst中
  const CvArr* src,
  CvScalar value,
  const dst
_;


使用实例代码:


#include <cv.h>
#include <highgui.h>
#include <cxcore.h>
#include <iostream>
using namespace std;
int main( int argc, char** argv ){
	CvMat *mat;
	mat=cvCreateMat(4,4,CV_32FC1);

	float value = http://www.mamicode.com/0.0;"初始化原始数组"<<endl;
	for ( i = 0; i < 4; i ++ ){
		for( j = 0; j < 4; j ++ ){

			value -= 1.0;
			CV_MAT_ELEM( *mat, float, i, j) = value;		
		}
	}
	for ( i = 0; i < 4; i ++ ){
		for( j = 0; j < 4; j ++ ){

			cout<<"\t"<<CV_MAT_ELEM( *mat, float, i, j);		
		}
		cout<<endl;
	}
	CvMat *matDes;

	matDes=cvCreateMat(4,4,CV_32FC1); 

	cout<<"目标矩阵"<<endl;
	for ( i = 0; i < 4; i ++ ){
		for( j = 0; j < 4; j ++ ){
			cvmSet( matDes, i, j,0);
			cout<<"\t"<<CV_MAT_ELEM( *matDes, float, i, j);		
		}

		cout<<endl;
	}

	cvAbs( mat, matDes );
	cout<<"数组的绝对值"<<endl;
	for ( i = 0; i < 4; i ++ ){
		for( j = 0; j < 4; j ++ ){

			cout<<"\t"<<CV_MAT_ELEM( *matDes, float, i, j);		
		}

		cout<<endl;
	}

	CvMat *matDiff1 = cvCreateMat(4,4,CV_32FC1);

	cvAbsDiff( mat,matDes , matDiff1);
	cout<<"两个差的绝对值"<<endl;
	for ( i = 0; i < 4; i ++ ){
		for( j = 0; j < 4; j ++ ){

			cout<<"\t"<<CV_MAT_ELEM( *matDiff1, float, i, j);		
		}

		cout<<endl;
	}

	CvScalar cs;
	cs.val[0] = 2.0;
	cs.val[1] = 1.0;

	cvAbsDiffS( mat, matDiff1,cs);
	cout<<"矩阵减去标准值的绝对值"<<endl;
	for ( i = 0; i < 4; i ++ ){
		for( j = 0; j < 4; j ++ ){

			cout<<"\t"<<CV_MAT_ELEM( *matDiff1, float, i, j);		
		}

		cout<<endl;
	}
	cvReleaseMat( &mat );
	cvReleaseMat( &matDes );
	getchar();
	return 0;
}


输出结果:


技术分享

   to be continued

《学习opencv》笔记——矩阵和图像操作——cvAbs,cvAbsDiff and cvAbsDiffS