update 20260317
All checks were successful
自动部署 / deploy (push) Successful in 3m43s

This commit is contained in:
2026-03-17 21:53:59 +08:00
Unverified
parent ebe1c292b1
commit ba922718ac
2 changed files with 71 additions and 1 deletions

View 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 %}