This commit is contained in:
2026-03-15 17:53:31 +08:00
parent 3aa643e052
commit 3ea1e48f4c
2 changed files with 79 additions and 0 deletions

39
basic/splitmerge.cpp Normal file
View File

@@ -0,0 +1,39 @@
// 图像通道分离与合并
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main()
{
string image_path = "../img/1.png";
Mat image = imread(image_path);
Mat result = image.clone();
// 定义向量数组接收通道
vector<Mat> 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<cv::Mat> 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);
}

40
basic/zoom.cpp Normal file
View File

@@ -0,0 +1,40 @@
#include <opencv2/opencv.hpp>
#include <iostream>
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_<float>(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;
}