#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
// 读取图像
Mat image = imread("your_image_path.jpg");
if (image.empty())
{
cout << "无法读取图像" << endl;
return -1;
}
// 获取图像的原始宽度和高度
int originalWidth = image.cols;
int originalHeight = image.rows;
// 定义放大比例(这里示例为放大2倍,你可以根据需求修改)
double scale = 2.0;
// 计算放大后的宽度和高度
int newWidth = static_cast<int>(originalWidth * scale);
int newHeight = static_cast<int>(originalHeight * scale);
// 计算中心坐标
int centerX = originalWidth / 2;
int centerY = originalHeight / 2;
// 计算裁剪区域的左上角坐标(以放大后的图像为基准)
int cropX = (newWidth - originalWidth) / 2;
int cropY = (newHeight - originalHeight) / 2;
// 定义输出图像的指定大小(这里示例为和原始图像一样大,你可以按需调整)
int targetWidth = originalWidth;
int targetHeight = originalHeight;
// 进行图像放大
Mat enlargedImage;
resize(image, enlargedImage, Size(newWidth, newHeight), 0, 0, INTER_LINEAR);
// 裁剪出指定大小的区域(以中心区域为准)
Mat croppedImage = enlargedImage(Rect(cropX, cropY, targetWidth, targetHeight));
// 显示原始图像和处理后的图像(可选)
namedWindow("Original Image", WINDOW_NORMAL);
namedWindow("Processed Image", WINDOW_NORMAL);
imshow("Original Image", image);
imshow("Processed Image", croppedImage);
waitKey(0);
return 0;
}
03-08
7404

02-04
3256
