OPenCV 图片局部放大

cpp 复制代码
  m_image = cv::imread("C:/Code/JPG/1.jpg");
  if (m_image.empty()) return;
  cv::imshow("原始图像", m_image);
cpp 复制代码
    // TODO: 在此添加控件通知处理程序代码
    int width = m_image.cols;
    int height = m_image.rows;
    // 确定要放大的区域(这里是图像中心部分)
    int roiWidth = width / 3;
    int roiHeight = height / 3;
    int roiX = (width - roiWidth) / 2;
    int roiY = (height - roiHeight) / 2;
    cv::Rect roi(roiX, roiY, roiWidth, roiHeight);
    cv::Mat roiImg = m_image(roi);

    double scale = 2.0;
    int enlargedWidth = static_cast<int>(roiWidth * scale);
    int enlargedHeight = static_cast<int>(roiHeight * scale);
    cv::Mat enlargedRoiImg(enlargedHeight, enlargedWidth, m_image.type());

    for (int y = 0; y < enlargedHeight; ++y)
    {
        for (int x = 0; x < enlargedWidth; ++x)
        {
            double origX = static_cast<double>(x) / scale;
            double origY = static_cast<double>(y) / scale;
            int x1 = static_cast<int>(origX);
            int y1 = static_cast<int>(origY);
            int x2 = (x1 == roiWidth - 1) ? x1 : x1 + 1;
            int y2 = (y1 == roiHeight - 1) ? y1 : y1 + 1;
            double dx = origX - x1;
            double dy = origY - y1;
            cv::Vec3b p1 = roiImg.at<cv::Vec3b>(y1, x1);
            cv::Vec3b p2 = roiImg.at<cv::Vec3b>(y1, x2);
            cv::Vec3b p3 = roiImg.at<cv::Vec3b>(y2, x1);
            cv::Vec3b p4 = roiImg.at<cv::Vec3b>(y2, x2);
            cv::Vec3b newPixel;
            newPixel[2] = static_cast<uchar>((1 - dx) * (1 - dy) * p1[2] + dx * (1 - dy) * p2[2] + (1 - dx) * dy * p3[2] + dx * dy * p4[2]);
            newPixel[1] = static_cast<uchar>((1 - dx) * (1 - dy) * p1[1] + dx * (1 - dy) * p2[1] + (1 - dx) * dy * p3[1] + dx * dy * p4[1]);
            newPixel[0] = static_cast<uchar>((1 - dx) * (1 - dy) * p1[0] + dx * (1 - dy) * p2[0] + (1 - dx) * dy * p3[0] + dx * dy * p4[0]);
            enlargedRoiImg.at<cv::Vec3b>(y, x) = newPixel;
        }
    }


    CRect rect;
    mStaticShowRegion.GetClientRect(&rect);
    ClientToScreen(rect);

    cv::Rect enlargedRoiRect(rect.left, rect.top, width, height);
    cv::imshow("放大2倍", enlargedRoiImg);
    cv::waitKey(0);

显示如下:

相关推荐
星期天要睡觉9 小时前
计算机视觉(opencv)——仿射变换(Affine Transformation)
人工智能·opencv·计算机视觉
abcd_zjq20 小时前
【2025最新】【win10】vs2026+qt6.9+opencv(cmake编译opencv_contrib拓展模
人工智能·qt·opencv·计算机视觉·visual studio
abcd_zjq21 小时前
VS2026+QT6.9+opencv图像增强(多帧平均降噪)(CLAHE对比度增强)(边缘增强)(图像超分辨率)
c++·图像处理·qt·opencv·visual studio
Blossom.11821 小时前
用一颗MCU跑通7B大模型:RISC-V+SRAM极致量化实战
人工智能·python·单片机·嵌入式硬件·opencv·机器学习·risc-v
mit6.82421 小时前
[GazeTracking] 摄像头交互与显示 | OpenCV
人工智能·opencv·交互
星期天要睡觉1 天前
计算机视觉(opencv)——基于 OpenCV DNN 的实时人脸检测 + 年龄与性别识别
opencv·计算机视觉·dnn
AndrewHZ2 天前
【图像处理基石】暗光增强算法入门:从原理到实战(Python+OpenCV)
图像处理·python·opencv·算法·计算机视觉·cv·暗光增强
~光~~2 天前
【环境配置 安装 】RK3588+Ubuntu20.04+cmake3.22+opencv4.54
opencv·ubuntu·rk3588
txwtech2 天前
第6篇 OpenCV RotatedRect如何判断矩形的角度
人工智能·opencv·计算机视觉
过往入尘土3 天前
走进 OpenCV 人脸识别的世界
人工智能·python·深度学习·opencv