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);

显示如下:

相关推荐
骄傲的心别枯萎7 小时前
RV1126 NO.57:ROCKX+RV1126人脸识别推流项目之读取人脸图片并把特征值保存到sqlite3数据库
数据库·opencv·计算机视觉·sqlite·音视频·rv1126
中年程序员一枚10 小时前
cv2.sqrBoxFilter 是 OpenCV 中用于计算像素邻域平方和的盒式滤波函数
人工智能·opencv·计算机视觉
棒棒的皮皮10 小时前
【OpenCV】Python图像处理之平滑处理
图像处理·python·opencv·计算机视觉
棒棒的皮皮11 小时前
【OpenCV】Python图像处理之重映射
图像处理·python·opencv·计算机视觉
中年程序员一枚11 小时前
cv2.blur 是 OpenCV 中实现均值滤波(归一化盒式滤波) 的核心函数
人工智能·opencv·均值算法
技术净胜1 天前
MATLAB进行图像分割从基础阈值到高级分割
opencv·计算机视觉·matlab
骄傲的心别枯萎1 天前
RV1126 NO.56:ROCKX+RV1126人脸识别推流项目之VI模块和VENC模块讲解
人工智能·opencv·计算机视觉·音视频·rv1126
骄傲的心别枯萎1 天前
RV1126 NO.55:ROCKX+RV1126人脸识别推流项目讲解
opencv·计算机视觉·音视频·rv1126
码界奇点1 天前
Python与OpenCV集成海康威视工业相机从基础配置到高级应用的全方位指南
python·数码相机·opencv·相机·python3.11
棒棒的皮皮1 天前
【OpenCV】Python图像处理几何变换之透视
图像处理·python·opencv·计算机视觉