From 3aa643e052a207845413202a743a374e0b25a234 Mon Sep 17 00:00:00 2001 From: biss Date: Sun, 15 Mar 2026 08:48:44 +0800 Subject: [PATCH] basic --- basic/readpixel.cpp | 24 ++++++++++++++++++++++++ basic/roi.cpp | 28 ++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 basic/readpixel.cpp create mode 100644 basic/roi.cpp diff --git a/basic/readpixel.cpp b/basic/readpixel.cpp new file mode 100644 index 0000000..0c6815d --- /dev/null +++ b/basic/readpixel.cpp @@ -0,0 +1,24 @@ +// 读取像素 +#include +#include + +using namespace std; +using namespace cv; + +int main() +{ + string image_path = "../img/1.png"; + Mat image = imread(image_path); + + if (image.empty()) { + cout << "错误:无法加载图像,请检查路径是否正确。" << endl; + return -1; + } + + Vec3b pixel_value = image.at(100, 150); + + cout << "B: " << (int)pixel_value[0] << " " + << "G: " << (int)pixel_value[1] << " " + << "R: " << (int)pixel_value[2] << endl; + +} \ No newline at end of file diff --git a/basic/roi.cpp b/basic/roi.cpp new file mode 100644 index 0000000..146681e --- /dev/null +++ b/basic/roi.cpp @@ -0,0 +1,28 @@ +// 修改像素 +#include +#include + +using namespace std; +using namespace cv; + +int main() +{ + string image_path = "../img/1.png"; + Mat image = imread(image_path); + Mat result = image.clone(); + + if (image.empty()) { + cout << "错误:无法加载图像,请检查路径是否正确。" << endl; + return -1; + } + + Rect roi_rect(0, 0, 100, 100); + Mat roi = result(roi_rect); + roi.setTo(Scalar(0, 255, 0)); + imshow("Original (Unchanged)", image); + imshow("Modified Copy", result); + waitKey(0); + + return 0; + +} \ No newline at end of file