diff --git a/Arithmetic operations/bitwise.cpp b/Arithmetic operations/bitwise.cpp new file mode 100644 index 0000000..2edac83 --- /dev/null +++ b/Arithmetic operations/bitwise.cpp @@ -0,0 +1,44 @@ +#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; +// bitwise_and() 按位与操作 掩码操作、图像分割 +// bitwise_or() 按位或操作 图像叠加 +// bitwise_not() 按位取反操作 图像反色 +// bitwise_xor() 按位异或操作 图像差异检测 + bitwise_and(img1, img2, result); + + imshow("Original Image 1", img1); + imshow("Original Image 2", img2); + imshow("Multiply Result", result); + + // 等待按键后关闭窗口 + waitKey(0); + destroyAllWindows(); + + return 0; +} \ No newline at end of file diff --git a/Arithmetic operations/multiply.cpp b/Arithmetic operations/multiply.cpp new file mode 100644 index 0000000..58e2422 --- /dev/null +++ b/Arithmetic operations/multiply.cpp @@ -0,0 +1,40 @@ +#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; + multiply(img1, img2, result); + + imshow("Original Image 1", img1); + imshow("Original Image 2", img2); + imshow("Multiply Result", result); + + // 等待按键后关闭窗口 + waitKey(0); + destroyAllWindows(); + + return 0; +} \ No newline at end of file diff --git a/Arithmetic operations/plus.cpp b/Arithmetic operations/plus.cpp index cf93cd5..0297b54 100644 --- a/Arithmetic operations/plus.cpp +++ b/Arithmetic operations/plus.cpp @@ -1,6 +1,6 @@ // 加法 -# include -# include +#include +#include using namespace std; using namespace cv; @@ -13,18 +13,20 @@ int main() Mat img1 = imread(path1); Mat img2 = imread(path2); - if (img1.empty() || img2.empty()) { + if (img1.empty() || img2.empty()) + { cout << "无法读取图片,请检查路径!" << endl; return -1; } - if (img1.size() != img2.size()) { + if (img1.size() != img2.size()) + { cout << "尺寸不一致,正在自动调整 img2 的尺寸..." << endl; // 将 img2 缩放到与 img1 相同的尺寸 resize(img2, img2, img1.size()); } - - Mat result ; + + Mat result; add(img1, img2, result); imshow("Original Image 1", img1); diff --git a/Arithmetic operations/subtract.cpp b/Arithmetic operations/subtract.cpp index 91dc64e..a937abd 100644 --- a/Arithmetic operations/subtract.cpp +++ b/Arithmetic operations/subtract.cpp @@ -1,6 +1,6 @@ // 减法,与加法基本一样,只把add改成了subtract. -# include -# include +#include +#include using namespace std; using namespace cv; @@ -13,18 +13,20 @@ int main() Mat img1 = imread(path1); Mat img2 = imread(path2); - if (img1.empty() || img2.empty()) { + if (img1.empty() || img2.empty()) + { cout << "无法读取图片,请检查路径!" << endl; return -1; } - if (img1.size() != img2.size()) { + if (img1.size() != img2.size()) + { cout << "尺寸不一致,正在自动调整 img2 的尺寸..." << endl; // 将 img2 缩放到与 img1 相同的尺寸 resize(img2, img2, img1.size()); } - - Mat result ; + + Mat result; subtract(img1, img2, result); imshow("Original Image 1", img1);