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

显示如下:

相关推荐
jndingxin1 小时前
OpenCV计算摄影学(15)无缝克隆(Seamless Cloning)调整图像颜色的函数colorChange()
人工智能·opencv·计算机视觉
程序员Linc1 小时前
用OpenCV写个视频播放器可还行?(C++版)
c++·opencv·音视频·opencv 4.11
kimi-2221 小时前
plt和cv2有不同的图像表示方式和颜色通道顺序
人工智能·opencv·计算机视觉
byxdaz5 小时前
CUDA编程之OpenCV与CUDA结合使用
人工智能·opencv·计算机视觉
YueiL8 小时前
OpenCV 颜色空间:原理与操作指南
python·opencv
WenGyyyL9 小时前
使用OpenCV和MediaPipe库——增强现实特效(在手腕添加虚拟手表)
人工智能·opencv·计算机视觉·ar·cv·mediapipe
挣扎与觉醒中的技术人1 天前
OpenCV视频解码全流程详解
人工智能·深度学习·opencv·计算机视觉·ffmpeg·音视频
WenGyyyL1 天前
使用OpenCV和MediaPipe库——驼背检测(姿态监控)
人工智能·python·opencv·算法·计算机视觉·numpy
jndingxin2 天前
OpenCV计算摄影学(16)调整图像光照效果函数illuminationChange()
人工智能·opencv·计算机视觉
花花鱼2 天前
opencv 阈值threshold 二值化,反二值化,截断,阈值取零,阈值反取零 python版实现
python·opencv·计算机视觉