From 4be5a75cf85356d528dc58e83c33e022ebb76a01 Mon Sep 17 00:00:00 2001 From: biss Date: Mon, 16 Mar 2026 21:00:45 +0800 Subject: [PATCH] add & subtract --- Arithmetic operations/plus.cpp | 39 ++++++++++++++++++++++++++++++ Arithmetic operations/subtract.cpp | 39 ++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 Arithmetic operations/plus.cpp create mode 100644 Arithmetic operations/subtract.cpp diff --git a/Arithmetic operations/plus.cpp b/Arithmetic operations/plus.cpp new file mode 100644 index 0000000..cf93cd5 --- /dev/null +++ b/Arithmetic operations/plus.cpp @@ -0,0 +1,39 @@ +// 加法 +# include +# include + +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; +} \ No newline at end of file diff --git a/Arithmetic operations/subtract.cpp b/Arithmetic operations/subtract.cpp new file mode 100644 index 0000000..91dc64e --- /dev/null +++ b/Arithmetic operations/subtract.cpp @@ -0,0 +1,39 @@ +// 减法,与加法基本一样,只把add改成了subtract. +# include +# include + +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 ; + subtract(img1, img2, result); + + imshow("Original Image 1", img1); + imshow("Original Image 2", img2); + imshow("Subtract Result", result); + + // 等待按键后关闭窗口 + waitKey(0); + destroyAllWindows(); + + return 0; +} \ No newline at end of file