Opencv cvAbs函数


/*
#define cvAbs( src, dst ) cvAbsDiffS( (src), (dst), cvScalarAll(0))
cvAbsDiffS( const CvArr* srcarr1, CvArr* dstarr, CvScalar scalar )
{
    cv::Mat src1 = cv::cvarrToMat(srcarr1), dst = cv::cvarrToMat(dstarr);
    CV_Assert( src1.size() == dst.size() && src1.type() == dst.type() );

    cv::absdiff( src1, scalar, dst );
}

CV_EXPORTS void absdiff(const Mat& a, const Mat& b, Mat& c);
CV_EXPORTS void absdiff(const Mat& a, const Scalar& s, Mat& c);


/****************************************************************************************\
*                                      absdiff                                           *
\****************************************************************************************/
/*
template<typename T> struct OpAbsDiff
{
    typedef T type1;
    typedef T type2;
    typedef T rtype;
    T operator()(T a, T b) { return (T)std::abs(a - b); }
};

template<> inline short OpAbsDiff<short>::operator ()(short a, short b)
{ return saturate_cast<short>(std::abs(a - b)); }

template<typename T, typename WT=T> struct OpAbsDiffS
{
    typedef T type1;
    typedef WT type2;
    typedef T rtype;
    T operator()(T a, WT b) { return saturate_cast<T>(std::abs(a - b)); }
};

void absdiff( const Mat& src1, const Mat& src2, Mat& dst )
{
    static BinaryFunc tab[] =
    {
        binaryOpC1_<OpAbsDiff<uchar>,VAbsDiff8u>, 0,
        binaryOpC1_<OpAbsDiff<ushort>,VAbsDiff16u>,
        binaryOpC1_<OpAbsDiff<short>,VAbsDiff16s>,
        binaryOpC1_<OpAbsDiff<int>,NoVec>,
        binaryOpC1_<OpAbsDiff<float>,VAbsDiff32f>,
        binaryOpC1_<OpAbsDiff<double>,NoVec>, 0
    };

    dst.create(src1.size(), src1.type());
    BinaryFunc func = tab[src1.depth()];
    CV_Assert(src1.size() == src2.size() && src1.type() == src2.type() && func != 0);
    func( src1, src2, dst );
}


void absdiff( const Mat& src1, const Scalar& s, Mat& dst )
{
    static BinarySFuncCn tab[] =
    {
        binarySOpCn_<OpAbsDiffS<uchar, int> >, 0,
        binarySOpCn_<OpAbsDiffS<ushort, int> >,
        binarySOpCn_<OpAbsDiffS<short, int> >,
        binarySOpCn_<OpAbsDiffS<int> >,
        binarySOpCn_<OpAbsDiffS<float> >,
        binarySOpCn_<OpAbsDiffS<double> >, 0
    };

    dst.create(src1.size(), src1.type());
    BinarySFuncCn func = tab[src1.depth()];
    CV_Assert(src1.channels() <= 4 && func != 0);
    func( src1, dst, s );
}
*/


#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
#include <cxcore.h>
#include <iostream>
using namespace std;
int main( int argc, char** argv )
{


	CvMat *mat;
	mat=cvCreateMat(3,4,CV_32FC1);
	
	float value = 0.0;
	int i = 0, j = 0;
	cout<<"初始化原始数组"<<endl;
	for ( i = 0; i < 3; i ++ ){
		for( j = 0; j < 4; j ++ ){
			
			value -= 3.0;
			CV_MAT_ELEM( *mat, float, i, j) = value;		
		}
	}
	cout<<"赋值后"<<endl;
	for ( i = 0; i < 3; i ++ ){
		for( j = 0; j < 4; j ++ ){
			
			cout<<" "<<CV_MAT_ELEM( *mat, float, i, j);	
		}
		cout<<endl;
	}

	CvMat *matDes;
	matDes=cvCreateMat(3,4,CV_32FC1);
	cout<<"目标矩阵"<<endl;
	for ( i = 0; i < 3; i ++ ){
		for( j = 0; j < 4; j ++ ){
			
			cout<<" "<<CV_MAT_ELEM( *matDes, float, i, j);		
		}
		
		cout<<endl;
	}
	//
	 cvAbsDiffS(mat,matDes,cvScalarAll(0));

   	 cvAbs( mat, matDes );
	cout<<"数组的绝对值"<<endl;
	for ( i = 0; i < 3; i ++ ){
		for( j = 0; j < 4; j ++ ){
			
			cout<<" "<<CV_MAT_ELEM( *matDes, float, i, j);		
		}
		
		cout<<endl;
	}
	cvReleaseMat( &mat );
	cvReleaseMat( &matDes );
	system("pause");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值