opencv提供了很多Mat的操作,其中涉及到两个重要的类:MatOp和MatExpr

C++: MatExpr abs(const Mat& m)
C++: void absdiff(InputArray src1, InputArray src2, OutputArray dst)
C = abs(A-B) is equivalent to absdiff(A, B, C)
C = abs(A) is equivalent to absdiff(A, Scalar::all(), C)
C++: void add(InputArray src1, InputArray src2, OutputArray dst, InputArray mask=noArray(), int dtype=-)
C++: void addWeighted(InputArray src1, double alpha, InputArray src2, double beta, double gamma, Out-
putArray dst, int dtype=-)
C++: void bitwise_and(InputArray src1, InputArray src2, OutputArray dst, InputArray mask=noArray())
C++: void bitwise_not(InputArray src, OutputArray dst, InputArray mask=noArray())
C++: void bitwise_or(InputArray src1, InputArray src2, OutputArray dst, InputArray mask=noArray())
C++: void bitwise_xor(InputArray src1, InputArray src2, OutputArray dst, InputArray mask=noArray())
C++: void calcCovarMatrix(const Mat* samples, int nsamples, Mat& covar, Mat& mean, int flags, int
ctype=CV_64F)
C++: void cartToPolar(InputArray x, InputArray y, OutputArray magnitude, OutputArray angle, bool an-
gleInDegrees=false)
C++: void magnitude(InputArray x, InputArray y, OutputArray magnitude)
C++: bool checkRange(InputArray a, bool quiet=true, Point* pos=, double minVal=-DBL_MAX, double
maxVal=DBL_MAX )
C++: void compare(InputArray src1, InputArray src2, OutputArray dst, int cmpop)
C++: void completeSymm(InputOutputArray mtx, bool lowerToUpper=false)
C++: void convertScaleAbs(InputArray src, OutputArray dst, double alpha=, double beta=)
C++: int countNonZero(InputArray src)
C++: Mat cvarrToMat(const CvArr* arr, bool copyData=false, bool allowND=true, int coiMode= )
C++: void dct(InputArray src, OutputArray dst, int flags=)
C++: void idct(InputArray src, OutputArray dst, int flags=)
C++: void dft(InputArray src, OutputArray dst, int flags=, int nonzeroRows=)
C++: void idft(InputArray src, OutputArray dst, int flags=, int nonzeroRows=)
C++: void divide(InputArray src1, InputArray src2, OutputArray dst, double scale=, int dtype=-)
C++: double determinant(InputArray mtx)
C++: bool eigen(InputArray src, OutputArray eigenvalues, int lowindex=-, int highindex=-)
C++: void exp(InputArray src, OutputArray dst)
C++: void extractImageCOI(const CvArr* arr, OutputArray coiimg, int coi=- )
C++: void insertImageCOI(InputArray coiimg, CvArr* arr, int coi=- )
C++: void flip(InputArray src, OutputArray dst, int flipCode)
C++: void gemm(InputArray src1, InputArray src2, double alpha, InputArray src3, double gamma, OutputArray dst, int flags= )
C++: ConvertData getConvertElem(int fromType, int toType)
C++: int getOptimalDFTSize(int vecsize)
C++: void inRange(InputArray src, InputArray lowerb, InputArray upperb, OutputArray dst)
C++: double invert(InputArray src, OutputArray dst, int flags=DECOMP_LU)
C++: void log(InputArray src, OutputArray dst)
C++: void LUT(InputArray src, InputArray lut, OutputArray dst, int interpolation= )
C++: double Mahalanobis(InputArray v1, InputArray v2, InputArray icovar)
C++: void max(InputArray src1, InputArray src2, OutputArray dst)

MatOp负责MatExpr的运算操作


class CV_EXPORTS MatOp
{
public:
MatOp();
virtual ~MatOp(); virtual bool elementWise(const MatExpr& expr) const;
virtual void assign(const MatExpr& expr, Mat& m, int type=-) const = ;
virtual void roi(const MatExpr& expr, const Range& rowRange,
const Range& colRange, MatExpr& res) const;
virtual void diag(const MatExpr& expr, int d, MatExpr& res) const;
virtual void augAssignAdd(const MatExpr& expr, Mat& m) const;
virtual void augAssignSubtract(const MatExpr& expr, Mat& m) const;
virtual void augAssignMultiply(const MatExpr& expr, Mat& m) const;
virtual void augAssignDivide(const MatExpr& expr, Mat& m) const;
virtual void augAssignAnd(const MatExpr& expr, Mat& m) const;
virtual void augAssignOr(const MatExpr& expr, Mat& m) const;
virtual void augAssignXor(const MatExpr& expr, Mat& m) const; virtual void add(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res) const;
virtual void add(const MatExpr& expr1, const Scalar& s, MatExpr& res) const; virtual void subtract(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res) const;
virtual void subtract(const Scalar& s, const MatExpr& expr, MatExpr& res) const; virtual void multiply(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res, double scale=) const;
virtual void multiply(const MatExpr& expr1, double s, MatExpr& res) const; virtual void divide(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res, double scale=) const;
virtual void divide(double s, const MatExpr& expr, MatExpr& res) const; virtual void abs(const MatExpr& expr, MatExpr& res) const; virtual void transpose(const MatExpr& expr, MatExpr& res) const;
virtual void matmul(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res) const;
virtual void invert(const MatExpr& expr, int method, MatExpr& res) const; virtual Size size(const MatExpr& expr) const;
virtual int type(const MatExpr& expr) const;
};

//modules/core/src/matop.cpp  定义了很多不同的操作,每种操作会重载部分函数
class MatOp_Identity : public MatOp
{
public:
MatOp_Identity() {}
virtual ~MatOp_Identity() {} bool elementWise(const MatExpr& /*expr*/) const { return true; }
void assign(const MatExpr& expr, Mat& m, int type=-) const; static void makeExpr(MatExpr& res, const Mat& m);
}; static MatOp_Identity g_MatOp_Identity; //一种默认的MatOp
static inline bool isIdentity(const MatExpr& e) { return e.op == &g_MatOp_Identity; } class MatOp_T : public MatOp
{
public:
MatOp_T() {}
virtual ~MatOp_T() {} bool elementWise(const MatExpr& /*expr*/) const { return false; }
void assign(const MatExpr& expr, Mat& m, int type=-) const; void multiply(const MatExpr& e1, double s, MatExpr& res) const;
void transpose(const MatExpr& expr, MatExpr& res) const; static void makeExpr(MatExpr& res, const Mat& a, double alpha=);
}; static MatOp_T g_MatOp_T;
static inline bool isT(const MatExpr& e) { return e.op == &g_MatOp_T; } //其他函数的实现举例:
void MatOp::add(const MatExpr& e1, const MatExpr& e2, MatExpr& res) const
{
if( this == e2.op )
{
double alpha = , beta = ;
Scalar s;
Mat m1, m2;
if( isAddEx(e1) && (!e1.b.data || e1.beta == ) )
{
m1 = e1.a;
alpha = e1.alpha;
s = e1.s;
}
else
e1.op->assign(e1, m1); if( isAddEx(e2) && (!e2.b.data || e2.beta == ) )
{
m2 = e2.a;
beta = e2.alpha;
s += e2.s;
}
else
e2.op->assign(e2, m2);
MatOp_AddEx::makeExpr(res, m1, m2, alpha, beta, s);
}
else
e2.op->add(e1, e2, res);
} void MatOp::add(const MatExpr& expr1, const Scalar& s, MatExpr& res) const
{
Mat m1;
expr1.op->assign(expr1, m1);
MatOp_AddEx::makeExpr(res, m1, Mat(), , , s);
} void MatOp::subtract(const MatExpr& e1, const MatExpr& e2, MatExpr& res) const
{
if( this == e2.op )
{
double alpha = , beta = -;
Scalar s;
Mat m1, m2;
if( isAddEx(e1) && (!e1.b.data || e1.beta == ) )
{
m1 = e1.a;
alpha = e1.alpha;
s = e1.s;
}
else
e1.op->assign(e1, m1); if( isAddEx(e2) && (!e2.b.data || e2.beta == ) )
{
m2 = e2.a;
beta = -e2.alpha;
s -= e2.s;
}
else
e2.op->assign(e2, m2);
MatOp_AddEx::makeExpr(res, m1, m2, alpha, beta, s);
}
else
e2.op->subtract(e1, e2, res);
} void MatOp::subtract(const Scalar& s, const MatExpr& expr, MatExpr& res) const
{
Mat m;
expr.op->assign(expr, m);
MatOp_AddEx::makeExpr(res, m, Mat(), -, , s);
}


//core/include/opencv2/core/mat.hpp
class CV_EXPORTS MatExpr
{
public:
MatExpr();
explicit MatExpr(const Mat& m); MatExpr(const MatOp* _op, int _flags, const Mat& _a = Mat(), const Mat& _b = Mat(),
const Mat& _c = Mat(), double _alpha = , double _beta = , const Scalar& _s = Scalar()); operator Mat() const;
template<typename _Tp> operator Mat_<_Tp>() const; Size size() const;
int type() const; MatExpr row(int y) const;
MatExpr col(int x) const;
MatExpr diag(int d = ) const;
MatExpr operator()( const Range& rowRange, const Range& colRange ) const;
MatExpr operator()( const Rect& roi ) const; MatExpr t() const;
MatExpr inv(int method = DECOMP_LU) const;
MatExpr mul(const MatExpr& e, double scale=) const;
MatExpr mul(const Mat& m, double scale=) const; Mat cross(const Mat& m) const;
double dot(const Mat& m) const; const MatOp* op;
int flags; Mat a, b, c;
double alpha, beta;
Scalar s;
}; CV_EXPORTS MatExpr operator + (const Mat& a, const Mat& b);
CV_EXPORTS MatExpr operator + (const Mat& a, const Scalar& s);
CV_EXPORTS MatExpr operator + (const Scalar& s, const Mat& a);
CV_EXPORTS MatExpr operator + (const MatExpr& e, const Mat& m);
CV_EXPORTS MatExpr operator + (const Mat& m, const MatExpr& e);
CV_EXPORTS MatExpr operator + (const MatExpr& e, const Scalar& s);
CV_EXPORTS MatExpr operator + (const Scalar& s, const MatExpr& e);
CV_EXPORTS MatExpr operator + (const MatExpr& e1, const MatExpr& e2); CV_EXPORTS MatExpr operator - (const Mat& a, const Mat& b);
CV_EXPORTS MatExpr operator - (const Mat& a, const Scalar& s);
CV_EXPORTS MatExpr operator - (const Scalar& s, const Mat& a);
CV_EXPORTS MatExpr operator - (const MatExpr& e, const Mat& m);
CV_EXPORTS MatExpr operator - (const Mat& m, const MatExpr& e);
CV_EXPORTS MatExpr operator - (const MatExpr& e, const Scalar& s);
CV_EXPORTS MatExpr operator - (const Scalar& s, const MatExpr& e);
CV_EXPORTS MatExpr operator - (const MatExpr& e1, const MatExpr& e2); CV_EXPORTS MatExpr operator - (const Mat& m);
CV_EXPORTS MatExpr operator - (const MatExpr& e); CV_EXPORTS MatExpr operator * (const Mat& a, const Mat& b);
CV_EXPORTS MatExpr operator * (const Mat& a, double s);
CV_EXPORTS MatExpr operator * (double s, const Mat& a);
CV_EXPORTS MatExpr operator * (const MatExpr& e, const Mat& m);
CV_EXPORTS MatExpr operator * (const Mat& m, const MatExpr& e);
CV_EXPORTS MatExpr operator * (const MatExpr& e, double s);
CV_EXPORTS MatExpr operator * (double s, const MatExpr& e);
CV_EXPORTS MatExpr operator * (const MatExpr& e1, const MatExpr& e2); CV_EXPORTS MatExpr operator / (const Mat& a, const Mat& b);
CV_EXPORTS MatExpr operator / (const Mat& a, double s);
CV_EXPORTS MatExpr operator / (double s, const Mat& a);
CV_EXPORTS MatExpr operator / (const MatExpr& e, const Mat& m);
CV_EXPORTS MatExpr operator / (const Mat& m, const MatExpr& e);
CV_EXPORTS MatExpr operator / (const MatExpr& e, double s);
CV_EXPORTS MatExpr operator / (double s, const MatExpr& e);
CV_EXPORTS MatExpr operator / (const MatExpr& e1, const MatExpr& e2); CV_EXPORTS MatExpr operator < (const Mat& a, const Mat& b);
CV_EXPORTS MatExpr operator < (const Mat& a, double s);
CV_EXPORTS MatExpr operator < (double s, const Mat& a); CV_EXPORTS MatExpr operator <= (const Mat& a, const Mat& b);
CV_EXPORTS MatExpr operator <= (const Mat& a, double s);
CV_EXPORTS MatExpr operator <= (double s, const Mat& a); CV_EXPORTS MatExpr operator == (const Mat& a, const Mat& b);
CV_EXPORTS MatExpr operator == (const Mat& a, double s);
CV_EXPORTS MatExpr operator == (double s, const Mat& a); CV_EXPORTS MatExpr operator != (const Mat& a, const Mat& b);
CV_EXPORTS MatExpr operator != (const Mat& a, double s);
CV_EXPORTS MatExpr operator != (double s, const Mat& a); CV_EXPORTS MatExpr operator >= (const Mat& a, const Mat& b);
CV_EXPORTS MatExpr operator >= (const Mat& a, double s);
CV_EXPORTS MatExpr operator >= (double s, const Mat& a); CV_EXPORTS MatExpr operator > (const Mat& a, const Mat& b);
CV_EXPORTS MatExpr operator > (const Mat& a, double s);
CV_EXPORTS MatExpr operator > (double s, const Mat& a); CV_EXPORTS MatExpr operator & (const Mat& a, const Mat& b);
CV_EXPORTS MatExpr operator & (const Mat& a, const Scalar& s);
CV_EXPORTS MatExpr operator & (const Scalar& s, const Mat& a); CV_EXPORTS MatExpr operator | (const Mat& a, const Mat& b);
CV_EXPORTS MatExpr operator | (const Mat& a, const Scalar& s);
CV_EXPORTS MatExpr operator | (const Scalar& s, const Mat& a); CV_EXPORTS MatExpr operator ^ (const Mat& a, const Mat& b);
CV_EXPORTS MatExpr operator ^ (const Mat& a, const Scalar& s);
CV_EXPORTS MatExpr operator ^ (const Scalar& s, const Mat& a); CV_EXPORTS MatExpr operator ~(const Mat& m); CV_EXPORTS MatExpr min(const Mat& a, const Mat& b);
CV_EXPORTS MatExpr min(const Mat& a, double s);
CV_EXPORTS MatExpr min(double s, const Mat& a); CV_EXPORTS MatExpr max(const Mat& a, const Mat& b);
CV_EXPORTS MatExpr max(const Mat& a, double s);
CV_EXPORTS MatExpr max(double s, const Mat& a); CV_EXPORTS MatExpr abs(const Mat& m);
CV_EXPORTS MatExpr abs(const MatExpr& e); } // cv

MatExpr::MatExpr(const Mat& m) : op(&g_MatOp_Identity), flags(), a(m), b(Mat()), c(Mat()), alpha(), beta(), s(Scalar())
{
  //默认使用的是g_MatOp_Identity
}
MatExpr MatExpr::diag(int d) const
{
MatExpr e;
op->diag(*this, d, e);
return e;
}
MatExpr MatExpr::t() const
{
MatExpr e;
op->transpose(*this, e);
return e;
}
MatExpr operator + (const Mat& a, const Mat& b)
{
MatExpr e;
MatOp_AddEx::makeExpr(e, a, b, , );
return e;
}
class MatOp_AddEx : public MatOp
{
public:
MatOp_AddEx() {}
virtual ~MatOp_AddEx() {} bool elementWise(const MatExpr& /*expr*/) const { return true; }
void assign(const MatExpr& expr, Mat& m, int type=-) const; void add(const MatExpr& e1, const Scalar& s, MatExpr& res) const;
void subtract(const Scalar& s, const MatExpr& expr, MatExpr& res) const;
void multiply(const MatExpr& e1, double s, MatExpr& res) const;
void divide(double s, const MatExpr& e, MatExpr& res) const; void transpose(const MatExpr& e1, MatExpr& res) const;
void abs(const MatExpr& expr, MatExpr& res) const; static void makeExpr(MatExpr& res, const Mat& a, const Mat& b, double alpha, double beta, const Scalar& s=Scalar());
}; static MatOp_AddEx g_MatOp_AddEx;
inline void MatOp_AddEx::makeExpr(MatExpr& res, const Mat& a, const Mat& b, double alpha, double beta, const Scalar& s)
{
    res = MatExpr(&g_MatOp_AddEx, 0, a, b, Mat(), alpha, beta, s);
}

 

opencv MatExpr MatOp的更多相关文章

  1. OpenCV MAT基本图像容器

    参考博客: OpenCv中cv::Mat和IplImage,CvMat之间的转换 Mat - 基本图像容器 Mat类型较CvMat和IplImage有更强的矩阵运算能力,支持常见的矩阵运算(参照Mat ...

  2. opencv 61篇

    (一)--安装配置.第一个程序 标签: imagebuildincludeinputpathcmd 2011-10-21 16:16 41132人阅读 评论(50) 收藏 举报  分类: OpenCV ...

  3. 【OpenCV入门教程之三】 图像的载入,显示和输出 一站式完全解析(转)

    本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/20537737 作者:毛星云(浅墨)  ...

  4. OpenCV源码阅读(3)---base.hpp

    base.h处于core模块中,是OpenCV的核心类.其作用是定义了OpenCV的基本错误类型,在程序运行出现错误是抛出错误,防止数据溢出.总而言之,其功能主要是考虑程序的健壮性. 头文件 #ifn ...

  5. 图像储存容器Mat[OpenCV 笔记11]

    IplImage 与 Mat IplImage是OpenCV1中的图像存储结构体,基于C接口创建.在退出之前必须release,否则就会造成内存泄露.在一些只能使用C语言的嵌入式系统中,不得不使用. ...

  6. OpenCV中Mat的详解

    每次碰到Mat都得反复查具体的用法,网上的基础讲解不多,难得看到一篇,赶快转来收藏~ 原文地址:http://www.opencvchina.com/thread-1039-1-1.html 目标 我 ...

  7. OPENCV第一篇

    了解过之前老版本OpenCV的童鞋们都应该清楚,对于OpenCV1.0时代的基于 C 语言接口而建的图像存储格式IplImage*,如果在退出前忘记release掉的话,就会照成内存泄露.而且用起来超 ...

  8. 相机标定 matlab opencv ROS三种方法标定步骤(2)

    二  ubuntu下Opencv的相机标定 一般直接用Opencv的源码就可以进行相机的标定,但是可能只是会实现结果,却不懂实现的过程,我也是模模糊糊的看了<计算机视觉中的多视图几何>以及 ...

  9. OpenCV中的结构体、类与Emgu.CV的对应表

    OpenCv中的 C 结构 OpenCV中的 C++ 封装 Emgu.CV中的 C# 封装 OpenCV 和 Emgu.CV 中的结构罗列 谢谢阅读,有误希望指正 原文地址 Basic Structu ...

随机推荐

  1. Legacy BIOS Boot 是如何启动或引导的

    现在Windows 8 64位操作系统全面采用UEFI引导启动的方式,与过去的Legacy启动有什么区别呢?今天就让我们一起来了解下. Legacy BIOS UEFI Boot 是如何启动或引导的 ...

  2. Codeforces Round #267 (Div. 2) B. Fedor and New Game

    After you had helped George and Alex to move in the dorm, they went to help their friend Fedor play ...

  3. 关于global和$GLOBALS[]的一道经典面试题

    在不执行程序的情况下,你觉得的输出结果是什么? <?php $var1 = 1; $var2 = 2; function test(){ global $var1,$var2; $var2 = ...

  4. 几个经典的TCP通信函数

    前言 在TCP通信中要使用到几个非常经典的函数( 点这里参考一个关于它们作用的形象比方 ),本文将对这几个函数进行一个简短的使用说明. socket函数 函数作用:创建一个网际字节流套接字 包含头文件 ...

  5. 基于chyh1990/caffe-compact在windows vs2013上编译caffe步骤

    1.      从https://github.com/chyh1990/caffe-compact下载caffe-compact代码: 2.      通过CMake(cmake-gui)生成vs2 ...

  6. [Phoenix] 一、快速入门

    Phoenix是一个开源的HBASE SQL层.Phoeinx可以用标准的JDBC API替代HBASE client API来创建表,插入和查询HBASE中的数据. Phoenix作为应用层和HBA ...

  7. java类的初始化过程

    1 先初始化父类的静态成员和静态块,然后初始化子类的静态成员和静态块,然后再初始化父类,然后再初始化子类. 2 先初始化父类 3 单个类初始化的顺序 先初始化成员变量和代码块,后调用构造函数 4 如果 ...

  8. 阿里Java开发手册学习 3 MYSQL规约

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  9. 分词系统简介:PHPAnalysis分词程序

    分词系统简介:PHPAnalysis分词程序使用居于unicode的词库,使用反向匹配模式分词,理论上兼容编码更广泛,并且对utf-8编码尤为方便. 由于PHPAnalysis是无组件的系统,因此速度 ...

  10. Windows程序设计(1)——Win32运行原理(二)

    创建进程 1 进程和线程 2 应用程序的启动过程 3 CreateProcess函数 4 实例 3 创建进程 3.1 进程和线程 进程通常被定义为一个存在运行的程序的实例.进程是一个正在运行的程序,它 ...