diff --git a/basic/splitmerge.cpp b/basic/splitmerge.cpp new file mode 100644 index 0000000..e718447 --- /dev/null +++ b/basic/splitmerge.cpp @@ -0,0 +1,39 @@ +// 图像通道分离与合并 +#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(); + // 定义向量数组接收通道 + vector channels; + // 拆分 + split(result,channels); + + Mat b = channels[0]; + Mat g = channels[1]; + Mat r = channels[2]; + + imshow("Blue Channel (Grayscale)", channels[0]); + imshow("Green Channel (Grayscale)", channels[1]); + imshow("Red Channel (Grayscale)", channels[2]); + + // merge + Mat merged_img; + vector channels_to_merge; + channels_to_merge.push_back(b); + channels_to_merge.push_back(g); + channels_to_merge.push_back(r); + + merge(channels_to_merge, merged_img); + imshow("merged",merged_img); + + waitKey(0); + + return(0); +} \ No newline at end of file diff --git a/basic/zoom.cpp b/basic/zoom.cpp new file mode 100644 index 0000000..db0d5fd --- /dev/null +++ b/basic/zoom.cpp @@ -0,0 +1,40 @@ +#include +#include + +int main() { + // 读取图像 + cv::Mat img = cv::imread("../img/1.png"); + if (img.empty()) { + std::cout << "无法读取图像" << std::endl; + return -1; + } + + // 1. 缩放 + cv::Mat resized_img; + cv::resize(img, resized_img, cv::Size(200, 200)); + cv::imshow("resized_img", resized_img); + + // 2. 旋转 + cv::Mat rotated_img, M_rot; + cv::Point2f center(img.cols / 2.0, img.rows / 2.0); + M_rot = cv::getRotationMatrix2D(center, 45, 1.0); + cv::warpAffine(img, rotated_img, M_rot, img.size()); + cv::imshow("rotated_img", rotated_img); + + // 3. 平移 + cv::Mat translated_img; + cv::Mat M_trans = (cv::Mat_(2, 3) << 1, 0, 100, 0, 1, 50); + cv::warpAffine(img, translated_img, M_trans, img.size()); + cv::imshow("translated_img", translated_img); + + // 4. 翻转 + cv::Mat flipped_img; + cv::flip(img, flipped_img, 1); + cv::imshow("flipped", flipped_img); + + // 显示结果 + cv::imshow("Original", img); + cv::waitKey(0); + + return 0; +} \ No newline at end of file