编程语言
首页 > 编程语言> > C++ OpenCV(二):Mat 运算

C++ OpenCV(二):Mat 运算

作者:互联网

逻辑运算

非运算

公式

dst ( I ) = ¬ src ( I ) \texttt{dst} (I) = \neg \texttt{src} (I) dst(I)=¬src(I)

API

CV_EXPORTS_W void bitwise_not(InputArray src, OutputArray dst,
                              InputArray mask = noArray());

示例

Mat mask = Mat::zeros(512, 512, CV_8UC1);
Rect rect = Rect(100, 100, 200, 200);
mask(rect) = Scalar(1);
Mat bitwiseNot;
bitwise_not(left, bitwiseNot, mask);
imshow("bitwise_not", bitwiseNot);

运行效果

bitwise_not

与运算

公式

dst ( I ) = src1 ( I ) ∧ src2 ( I ) if   mask ( I ) ≠ 0 \texttt{dst} (I) = \texttt{src1} (I) \wedge \texttt{src2} (I) \quad \texttt{if mask} (I) \ne0 dst(I)=src1(I)∧src2(I)if mask(I)​=0

dst ( I ) = src1 ( I ) ∧ src2 if   mask ( I ) ≠ 0 \texttt{dst} (I) = \texttt{src1} (I) \wedge \texttt{src2} \quad \texttt{if mask} (I) \ne0 dst(I)=src1(I)∧src2if mask(I)​=0

dst ( I ) = src1 ∧ src2 ( I ) if   mask ( I ) ≠ 0 \texttt{dst} (I) = \texttt{src1} \wedge \texttt{src2} (I) \quad \texttt{if mask} (I) \ne0 dst(I)=src1∧src2(I)if mask(I)​=0

API

CV_EXPORTS_W void bitwise_and(InputArray src1, InputArray src2,
                              OutputArray dst, InputArray mask = noArray());

示例

Mat bitwiseAnd;
Scalar scalar = Scalar(255,255);
bitwise_and(left, scalar, bitwiseAnd);
imshow("bitwise_and", bitwiseAnd);

运行效果

bitwise_and

或运算

公式

dst ( I ) = src1 ( I ) ∨ src2 ( I ) if   mask ( I ) ≠ 0 \texttt{dst} (I) = \texttt{src1} (I) \vee \texttt{src2} (I) \quad \texttt{if mask} (I) \ne0 dst(I)=src1(I)∨src2(I)if mask(I)​=0

dst ( I ) = src1 ( I ) ∨ src2 if   mask ( I ) ≠ 0 \texttt{dst} (I) = \texttt{src1} (I) \vee \texttt{src2} \quad \texttt{if mask} (I) \ne0 dst(I)=src1(I)∨src2if mask(I)​=0

dst ( I ) = src1 ∨ src2 ( I ) if   mask ( I ) ≠ 0 \texttt{dst} (I) = \texttt{src1} \vee \texttt{src2} (I) \quad \texttt{if mask} (I) \ne0 dst(I)=src1∨src2(I)if mask(I)​=0

API

CV_EXPORTS_W void bitwise_or(InputArray src1, InputArray src2,
                              OutputArray dst, InputArray mask = noArray());

示例

Mat bitwiseOr;
Scalar scalarOr = Scalar(255);
bitwise_or(left, scalarOr, bitwiseOr);
imshow("bitwise_or", bitwiseOr);

运行效果

bitwise_or

异或运算

公式

dst ( I ) = src1 ( I ) ⊕ src2 ( I ) if   mask ( I ) ≠ 0 \texttt{dst} (I) = \texttt{src1} (I) \oplus \texttt{src2} (I) \quad \texttt{if mask} (I) \ne0 dst(I)=src1(I)⊕src2(I)if mask(I)​=0

dst ( I ) = src1 ( I ) ⊕ src2 if   mask ( I ) ≠ 0 \texttt{dst} (I) = \texttt{src1} (I) \oplus \texttt{src2} \quad \texttt{if mask} (I) \ne0 dst(I)=src1(I)⊕src2if mask(I)​=0

dst ( I ) = src1 ⊕ src2 ( I ) if   mask ( I ) ≠ 0 \texttt{dst} (I) = \texttt{src1} \oplus \texttt{src2} (I) \quad \texttt{if mask} (I) \ne0 dst(I)=src1⊕src2(I)if mask(I)​=0

API

CV_EXPORTS_W void bitwise_xor(InputArray src1, InputArray src2,
                              OutputArray dst, InputArray mask = noArray());

示例

Mat bitwiseXor;
Scalar scalarXor = Scalar(255, 255);
bitwise_xor(left, scalarXor, bitwiseXor);
imshow("bitwise_xor", bitwiseXor);

运行效果

bitwise_xor

算术运算

图像矩阵的 addsubtractmultiplydivide 四则运算是针对矩阵内对应元素的操作,并非矩阵的加减乘除。

公式

加法

dst ( I ) = saturate ( src1 ( I ) + src2 ( I ) ) if   mask ( I ) ≠ 0 \texttt{dst}(I) = \texttt{saturate} ( \texttt{src1}(I) + \texttt{src2}(I)) \quad \texttt{if mask}(I) \ne0 \\ dst(I)=saturate(src1(I)+src2(I))if mask(I)​=0

dst ( I ) = saturate ( src1 ( I ) + src2 ) if   mask ( I ) ≠ 0 \texttt{dst}(I) = \texttt{saturate} ( \texttt{src1}(I) + \texttt{src2} ) \quad \texttt{if mask}(I) \ne0 \\ dst(I)=saturate(src1(I)+src2)if mask(I)​=0

dst ( I ) = saturate ( src1 + src2 ( I ) ) if   mask ( I ) ≠ 0 \texttt{dst}(I) = \texttt{saturate} ( \texttt{src1} + \texttt{src2}(I) ) \quad \texttt{if mask}(I) \ne0 \\ dst(I)=saturate(src1+src2(I))if mask(I)​=0

减法

dst ( I ) = saturate ( src1 ( I ) − src2 ( I ) ) if   mask ( I ) ≠ 0 \texttt{dst}(I) = \texttt{saturate} ( \texttt{src1}(I) - \texttt{src2}(I)) \quad \texttt{if mask}(I) \ne0 \\ dst(I)=saturate(src1(I)−src2(I))if mask(I)​=0

dst ( I ) = saturate ( src1 ( I ) − src2 ) if   mask ( I ) ≠ 0 \texttt{dst}(I) = \texttt{saturate} ( \texttt{src1}(I) - \texttt{src2} ) \quad \texttt{if mask}(I) \ne0 \\ dst(I)=saturate(src1(I)−src2)if mask(I)​=0

dst ( I ) = saturate ( src1 − src2 ( I ) ) if   mask ( I ) ≠ 0 \texttt{dst}(I) = \texttt{saturate} ( \texttt{src1} - \texttt{src2}(I) ) \quad \texttt{if mask}(I) \ne0 \\ dst(I)=saturate(src1−src2(I))if mask(I)​=0

dst ( I ) = saturate ( src2 − src1 ( I ) ) if   mask ( I ) ≠ 0 \texttt{dst}(I) = \texttt{saturate} ( \texttt{src2} - \texttt{src1}(I) ) \quad \texttt{if mask}(I) \ne0 dst(I)=saturate(src2−src1(I))if mask(I)​=0

乘法

dst ( I ) = saturate ( scale ⋅ src1 ( I ) ⋅ src2 ( I ) ) \texttt{dst} (I)= \texttt{saturate} ( \texttt{scale} \cdot \texttt{src1} (I) \cdot \texttt{src2} (I)) dst(I)=saturate(scale⋅src1(I)⋅src2(I))

除法

dst(I)   =   saturate(src1(I)*scale/src2(I)) \texttt{dst(I) = saturate(src1(I)*scale/src2(I))} \\ dst(I) = saturate(src1(I)*scale/src2(I))

dst(I)   =   saturate(scale/src2(I)) \texttt{dst(I) = saturate(scale/src2(I))} dst(I) = saturate(scale/src2(I))

API

CV_EXPORTS_W void add(InputArray src1, InputArray src2, OutputArray dst,                      InputArray mask = noArray(), int dtype = -1);
CV_EXPORTS_W void subtract(InputArray src1, InputArray src2, OutputArray dst,                           InputArray mask = noArray(), int dtype = -1);
CV_EXPORTS_W void multiply(InputArray src1, InputArray src2,                           OutputArray dst, double scale = 1, int dtype = -1);
CV_EXPORTS_W void divide(InputArray src1, InputArray src2, OutputArray dst,                         double scale = 1, int dtype = -1);

示例

int8_t b[6] = {8, 9, 10, 11, 12, 13};int8_t c[6] = {6, 7, 5, 4, 3, 9};Mat first = Mat(3, 2, CV_8UC1, b);Mat second = Mat(3, 2, CV_8UC1, c);Mat third = Mat(2, 3, CV_8UC1, c);// 算数运算Mat addResult;add(first, second, addResult);cout << "add" << endl << format(addResult, Formatter::FMT_C) << endl << endl;Mat subtractResult;subtract(first, second, subtractResult);cout << "subtract" << endl << format(subtractResult, Formatter::FMT_C) << endl << endl;Mat multiplyResult;multiply(first, first, multiplyResult);cout << "multiply" << endl << format(multiplyResult, Formatter::FMT_C) << endl << endl;Mat divideResult;divide(first, second, divideResult);cout << "divide" << endl << format(divideResult, Formatter::FMT_C) << endl << endl;

运行效果

算术加减乘除

标签:saturate,Mat,dst,texttt,mask,C++,src2,OpenCV,src1
来源: https://blog.csdn.net/poorkick/article/details/120357550