C++ OpenCV(二):Mat 运算
2021/9/17 22:05:02
本文主要是介绍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;
运行效果
这篇关于C++ OpenCV(二):Mat 运算的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-11国产医疗级心电ECG采集处理模块
- 2025-01-10Rakuten 乐天积分系统从 Cassandra 到 TiDB 的选型与实战
- 2025-01-09CMS内容管理系统是什么?如何选择适合你的平台?
- 2025-01-08CCPM如何缩短项目周期并降低风险?
- 2025-01-08Omnivore 替代品 Readeck 安装与使用教程
- 2025-01-07Cursor 收费太贵?3分钟教你接入超低价 DeepSeek-V3,代码质量逼近 Claude 3.5
- 2025-01-06PingCAP 连续两年入选 Gartner 云数据库管理系统魔力象限“荣誉提及”
- 2025-01-05Easysearch 可搜索快照功能,看这篇就够了
- 2025-01-04BOT+EPC模式在基础设施项目中的应用与优势
- 2025-01-03用LangChain构建会检索和搜索的智能聊天机器人指南