桌面端截长图/滚动截图:图像融合拼接关键算法

关键环节的实现逻辑

1. 滚动条滚动之前,为窗口拍照:

 2. 滚动条滚动之后,为窗口拍照:(滚动的是左侧代码区域)

3. 两个图像融合拼接成一个图像:(右侧区域不是关键区域,仅简单拼接) 

找到左侧关键区域子窗口句柄

POINT pt;
GetCursorPos(&pt);
HWND hwnd = WindowFromPoint(pt);

 找到这个句柄之后,确定左侧核心区域在原图中的位置,并得到两个子图像:QImage image1和QImage image2

找两个子图像的相似区域(OpenCV算法)

注意1:两个子图像重叠的部分并不一定是所有像素都一样,所以必须使用图像相似性查找算法。

注意2:我们明确image1的底部区域与image2的顶部区域相似,所以以下代码中,我们只拿image2的顶部区域在image1中搜索(找相似区域)

#include "opencv2\opencv.hpp"

QRect findMatchingRegion(const QImage& img1, const QImage& img2) {
    // 转换为 OpenCV Mat,确保格式为 RGB888
    cv::Mat mat1 = cv::Mat(img1.height(), img1.width(), CV_8UC3, (void*)img1.constBits(), img1.bytesPerLine()).clone();
    cv::Mat mat2 = cv::Mat(img2.height(), img2.width(), CV_8UC3, (void*)img2.constBits(), img2.bytesPerLine()).clone();

    // 结果图像大小:W - w + 1, H - h + 1
    int result_cols = mat1.cols - mat2.cols + 1;
    int result_rows = mat1.rows - mat2.rows + 1;
    cv::Mat result;
    result.create(result_rows, result_cols, CV_32FC1);

    // 进行模板匹配
    cv::matchTemplate(mat1, mat2, result, cv::TM_CCOEFF_NORMED);

    // 找到最大值的位置(最佳匹配)
    double minVal, maxVal;
    cv::Point minLoc, maxLoc;
    cv::minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc);

    // 匹配位置就是左上角坐标,宽高就是模板图像大小
    QRect matchedRegion(maxLoc.x, maxLoc.y, img2.width(), img2.height());
    return matchedRegion;
}

auto img1 = image1.convertToFormat(QImage::Format_RGB888);
auto img2 = image2.convertToFormat(QImage::Format_RGB888).copy(0, 0, image2.width(), 100);
auto r1 = findMatchingRegion(img1, img2);

这个算法返回的就是image1中与image2顶部100个像素区域相似的区域(在image1的底部)

找两个子图像的相似区域(自研算法)

OpenCV库太大了,得五六十兆,所以自己写了个算法

#include <QtConcurrent>
#include <QFuture>
#include <QMutex>

struct MatchResult {
    int y;
    double error;
};

MatchResult computeSSDAtY(int y, const QImage& gray1, const QImage& gray2) {
    const int width = gray1.width();
    const int blockHeight = gray2.height();

    double error = 0.0;
    const uchar* p1 = gray1.constBits() + y * gray1.bytesPerLine();
    const uchar* p2 = gray2.constBits();

    for (int row = 0; row < blockHeight; ++row) {
        const uchar* row1 = p1 + row * gray1.bytesPerLine();
        const uchar* row2 = p2 + row * gray2.bytesPerLine();

        for (int x = 0; x < width; ++x) {
            int diff = int(row1[x]) - int(row2[x]);
            error += diff * diff;
        }
    }

    return { y, error };
}

QRect findMostSimilarRegionParallel(const QImage& gray1, const QImage& gray2) {
    const int searchHeight = gray1.height() - gray2.height() + 1;

    // 构造搜索区间
    QList<int> yList;
    for (int y = 0; y < searchHeight; ++y)
        yList.append(y);

    // 并行计算每个 y 的误差
    auto results = QtConcurrent::blockingMapped(
        yList,
        [&](int y) { return computeSSDAtY(y, gray1, gray2); }
    );

    // 取最小误差的结果
    MatchResult best = *std::min_element(results.begin(), results.end(),
        [](const MatchResult& a, const MatchResult& b) {
            return a.error < b.error;
        });

    return QRect(0, best.y, gray2.width(), gray2.height());
}
auto img1 = img1.convertToFormat(QImage::Format_Grayscale8);
auto img2 = img2.convertToFormat(QImage::Format_Grayscale8);
auto r2 = findMostSimilarRegionParallel(img1, img2);

自己实现得算法用到了多线程(QtConcurrent库),效率也不错,跟OpenCV的算法相差不大。

总结

找到滚动区域的相似区之后(关键是相似区域的位置),拼接两个图像就不成问题了。

误区:不要试图去计算鼠标滚轮滚动一次窗口内容行进了多少个像素高度,这得不到好结果,因为每个窗口行进的高度是不一样的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

liulun

如果文章真帮到了你,谢谢您打赏

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

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

打赏作者

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

抵扣说明:

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

余额充值