opecv 图片中心放大 只保留指定大小 C++

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

焰川

微信 2936729162

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值