42 lines
1.1 KiB
C++
42 lines
1.1 KiB
C++
#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;
|
|
} |