AI正在绞尽脑汁想思路ING···
BiのAI摘要
HunYuan-Lite

opencv对图像的算数运算,感觉都大同小异,分为以下几种:加减乘除和位运算。

函数 功能 应用场景
cv2.bitwise_and() 按位与操作 掩码操作、图像分割
cv2.bitwise_or() 按位或操作 图像叠加
cv2.bitwise_not() 按位取反操作 图像反色
cv2.bitwise_xor() 按位异或操作 图像差异检测

因为感觉都差不多,所以只把加法运算代码搬过来

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// 加法
#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中