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

显示如下:

相关推荐
蹦蹦跳跳真可爱5893 小时前
Python----OpenCV(几何变换--图像平移、图像旋转、放射变换、图像缩放、透视变换)
开发语言·人工智能·python·opencv·计算机视觉
看到我,请让我去学习6 小时前
OpenCV 与深度学习:从图像分类到目标检测技术
深度学习·opencv·分类
jndingxin6 小时前
OpenCV 图像哈希类cv::img_hash::AverageHash
人工智能·opencv·哈希算法
Hoshino _Ai1 天前
OpenCV图像认知(三)
人工智能·opencv·计算机视觉
azoo1 天前
Canny边缘检测(cv2.Canny())
人工智能·opencv·计算机视觉
PyAIExplorer1 天前
图像梯度处理与边缘检测:OpenCV 实战指南
人工智能·opencv·计算机视觉
九章云极AladdinEdu1 天前
华为昇腾NPU与NVIDIA CUDA生态兼容层开发实录:手写算子自动转换工具链(AST级代码迁移方案)
人工智能·深度学习·opencv·机器学习·华为·数据挖掘·gpu算力
Blue桃之夭夭1 天前
基于OpenCV的实时人脸检测系统实现指南 ——Python+Haar级联分类器从环境搭建到完整部署
人工智能·python·opencv
presenttttt1 天前
用Python和OpenCV从零搭建一个完整的双目视觉系统(四)
开发语言·python·opencv·计算机视觉
jndingxin2 天前
OpenCV 人脸分析----人脸识别的一个经典类cv::face::EigenFaceRecognizer
人工智能·opencv·计算机视觉