This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
---
|
---
|
||||||
title: opencv基础操作
|
title: opencv应用-基础操作
|
||||||
categories:
|
categories:
|
||||||
- 技术
|
- 技术
|
||||||
series: opencv
|
series: opencv
|
||||||
|
|||||||
70
source/_posts/2026/2026.3/opencv-2.md
Normal file
70
source/_posts/2026/2026.3/opencv-2.md
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
---
|
||||||
|
title: opencv应用-算术运算
|
||||||
|
categories:
|
||||||
|
- 技术
|
||||||
|
series: opencv
|
||||||
|
tags:
|
||||||
|
- opencv
|
||||||
|
abbrlink: b559997d
|
||||||
|
summary: >-
|
||||||
|
这篇文章介绍了如何使用OpenCV进行图像的加法运算。首先,通过包含OpenCV和iostream头文件,并使用std命名空间,开始编写代码。主函数中,读取两张图片,检查是否成功加载,并确保它们的尺寸相同。如果尺寸不同,将第二张图片调整为与第一张相同的大小。然后,调用add函数对两幅图片进行加法运算,并将结果存储在result矩阵中。最后,显示原始图片和加法运算的结果,并等待按键关闭窗口。代码已上传至自建的git仓库。
|
||||||
|
date: 2026-03-17 13:13:08
|
||||||
|
---
|
||||||
|
|
||||||
|
opencv对图像的算数运算,感觉都大同小异,分为以下几种:加减乘除和位运算。
|
||||||
|
|
||||||
|
| 函数 | 功能 | 应用场景 |
|
||||||
|
|-------------------|--------|------------|
|
||||||
|
| cv2.bitwise_and() | 按位与操作 | 掩码操作、图像分割 |
|
||||||
|
| cv2.bitwise_or() | 按位或操作 | 图像叠加 |
|
||||||
|
| cv2.bitwise_not() | 按位取反操作 | 图像反色 |
|
||||||
|
| cv2.bitwise_xor() | 按位异或操作 | 图像差异检测 |
|
||||||
|
|
||||||
|
因为感觉都差不多,所以只把加法运算代码搬过来
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
// 加法
|
||||||
|
#include <opencv2/opencv.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace cv;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
string path1 = "../img/2.jpg";
|
||||||
|
string path2 = "../img/3.jpg";
|
||||||
|
|
||||||
|
Mat img1 = imread(path1);
|
||||||
|
Mat img2 = imread(path2);
|
||||||
|
|
||||||
|
if (img1.empty() || img2.empty())
|
||||||
|
{
|
||||||
|
cout << "无法读取图片,请检查路径!" << endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (img1.size() != img2.size())
|
||||||
|
{
|
||||||
|
cout << "尺寸不一致,正在自动调整 img2 的尺寸..." << endl;
|
||||||
|
// 将 img2 缩放到与 img1 相同的尺寸
|
||||||
|
resize(img2, img2, img1.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
Mat result;
|
||||||
|
add(img1, img2, result);
|
||||||
|
|
||||||
|
imshow("Original Image 1", img1);
|
||||||
|
imshow("Original Image 2", img2);
|
||||||
|
imshow("Add Result", result);
|
||||||
|
|
||||||
|
// 等待按键后关闭窗口
|
||||||
|
waitKey(0);
|
||||||
|
destroyAllWindows();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
另外,把代码上传到了自建的git中
|
||||||
|
{% link opencv-learning,opencv-learning,https://git.biss.click/biss/opencv-learning/src/branch/master/Arithmetic%20operations %}
|
||||||
Reference in New Issue
Block a user