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());
- 参数一:src,输入图像矩阵;
- 参数二:dst,输出图像矩阵,大小和类型与输入相同;
- 参数三:mask,8位单通道掩码。
示例
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);
运行效果
与运算
公式
src1
和src2
是两个大小相同的图像矩阵
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
src1
是图像矩阵,src2
是 Scalar
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
src1
是 Scalar ,src2
是图像矩阵
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());
- 参数一:src1,第一个输入的图像矩阵或者 Scalar;
- 参数二:src2,第二个输入的图像矩阵或者 Scalar;
- 参数三:dst,输出图像矩阵,大小和类型与输入相同;
- 参数四:mask,8位单通道掩码。
示例
Mat bitwiseAnd;
Scalar scalar = Scalar(255,255);
bitwise_and(left, scalar, bitwiseAnd);
imshow("bitwise_and", bitwiseAnd);
运行效果
或运算
公式
src1
和src2
是两个大小相同的图像矩阵
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
src1
是图像矩阵,src2
是 Scalar
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
src1
是 Scalar ,src2
是图像矩阵
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());
- 参数一:src1,第一个输入的图像矩阵或者 Scalar;
- 参数二:src2,第二个输入的图像矩阵或者 Scalar
- 参数三:dst,输出图像矩阵,大小和类型与输入相同;
- 参数四:mask,8位单通道掩码。
示例
Mat bitwiseOr;
Scalar scalarOr = Scalar(255);
bitwise_or(left, scalarOr, bitwiseOr);
imshow("bitwise_or", bitwiseOr);
运行效果
异或运算
公式
src1
和src2
是两个大小相同的图像矩阵
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
src1
是图像矩阵,src2
是 Scalar
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
src1
是 Scalar ,src2
是图像矩阵
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());
- 参数一:src1,第一个输入的图像矩阵或者 Scalar;
- 参数二:src2,第二个输入的图像矩阵或者 Scalar
- 参数三:dst,输出图像矩阵,大小和类型与输入相同;
- 参数四:mask,8位单通道掩码。
示例
Mat bitwiseXor;
Scalar scalarXor = Scalar(255, 255);
bitwise_xor(left, scalarXor, bitwiseXor);
imshow("bitwise_xor", bitwiseXor);
运行效果
算术运算
图像矩阵的 add
、subtract
、multiply
、divide
四则运算是针对矩阵内对应元素的操作,并非矩阵的加减乘除。
公式
加法
- 输入为两个大小和通道数相同的图像矩阵
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
src1
为图像矩阵,src2
为 Scalar
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
src1
为 Scalar,src2
为 图像矩阵
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
src1
为图像矩阵,src2
为 Scalar
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
src1
为 Scalar,src2
为图像矩阵
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
- SubRS 情况下标量和数组的反向差异
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);
- 参数一:src1,第一个输入的图像矩阵或者 Scalar;
- 参数二:src2,第二个输入的图像矩阵或者 Scalar
- 参数三:dst,输出图像矩阵,大小和类型与输入相同;
- 参数四:mask,8位单通道掩码;
- 参数五:scale,缩放倍数;
- 参数六:dtype,输出矩阵的图像深度。
示例
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