Compare commits

...

3 Commits

8 changed files with 95 additions and 15 deletions

View File

@@ -0,0 +1,39 @@
// 加法
# 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;
}

View File

@@ -0,0 +1,39 @@
// 减法与加法基本一样只把add改成了subtract.
# 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 ;
subtract(img1, img2, result);
imshow("Original Image 1", img1);
imshow("Original Image 2", img2);
imshow("Subtract Result", result);
// 等待按键后关闭窗口
waitKey(0);
destroyAllWindows();
return 0;
}

View File

@@ -10,7 +10,8 @@ int main()
string image_path = "../img/1.png"; string image_path = "../img/1.png";
Mat image = imread(image_path); Mat image = imread(image_path);
if (image.empty()) { if (image.empty())
{
cout << "错误:无法加载图像,请检查路径是否正确。" << endl; cout << "错误:无法加载图像,请检查路径是否正确。" << endl;
return -1; return -1;
} }
@@ -20,5 +21,4 @@ int main()
cout << "B: " << (int)pixel_value[0] << " " cout << "B: " << (int)pixel_value[0] << " "
<< "G: " << (int)pixel_value[1] << " " << "G: " << (int)pixel_value[1] << " "
<< "R: " << (int)pixel_value[2] << endl; << "R: " << (int)pixel_value[2] << endl;
} }

View File

@@ -11,7 +11,8 @@ int main()
Mat image = imread(image_path); Mat image = imread(image_path);
Mat result = image.clone(); Mat result = image.clone();
if (image.empty()) { if (image.empty())
{
cout << "错误:无法加载图像,请检查路径是否正确。" << endl; cout << "错误:无法加载图像,请检查路径是否正确。" << endl;
return -1; return -1;
} }
@@ -24,5 +25,4 @@ int main()
waitKey(0); waitKey(0);
return 0; return 0;
} }

View File

@@ -1,10 +1,12 @@
#include <opencv2/opencv.hpp> #include <opencv2/opencv.hpp>
#include <iostream> #include <iostream>
int main() { int main()
{
// 读取图像 // 读取图像
cv::Mat img = cv::imread("../img/1.png"); cv::Mat img = cv::imread("../img/1.png");
if (img.empty()) { if (img.empty())
{
std::cout << "无法读取图像" << std::endl; std::cout << "无法读取图像" << std::endl;
return -1; return -1;
} }

BIN
img/2.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 MiB

BIN
img/3.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 MiB