Qt图像处理技术十:得到QImage图像的高斯模糊

效果图

参数为5

参数为20

原理

高斯模糊使用正态分布来分配周围像素的权重。具体来说,距离中心点越近的像素对最终结果的影响越大,权重也越高;随着距离的增加,权重逐渐减小。

这种权重分配方式确保了图像在模糊处理时,边缘信息得到相对较好的保留。

构建高斯核, 实现归一化,然后分别对水平方向模糊和垂直方向模糊

源码

cpp 复制代码
// 高斯模糊函数
QImage applyGaussianBlur(const QImage &oldimage, int radius)
{
    QImage image(oldimage);
    if (image.isNull() || radius <= 0)
        return QImage();

    QImage resultImage = image;
    const int size = radius * 2 + 1;
    const int sigma = radius / 2;
    const double sigmaSq = sigma * sigma;
    QVector<double> kernel(size);

    // 构建高斯核
    double sum = 0.0;
    for (int i = -radius; i <= radius; ++i)
    {
        double value = exp(-(i * i) / (2 * sigmaSq)) / (sqrt(2 * M_PI) * sigma);
        kernel[i + radius] = value;
        sum += value;
    }

    // 归一化
    for (int i = 0; i < size; ++i)
    {
        kernel[i] /= sum;
    }

    // 水平方向模糊
    for (int y = 0; y < image.height(); ++y)
    {
        for (int x = radius; x < image.width() - radius; ++x)
        {
            double red = 0, green = 0, blue = 0;
            for (int i = -radius; i <= radius; ++i)
            {
                QRgb pixel = image.pixel(x + i, y);
                red += qRed(pixel) * kernel[i + radius];
                green += qGreen(pixel) * kernel[i + radius];
                blue += qBlue(pixel) * kernel[i + radius];
            }
            resultImage.setPixel(x, y, qRgb(red, green, blue));
        }
    }

    // 垂直方向模糊
    for (int x = 0; x < image.width(); ++x)
    {
        for (int y = radius; y < image.height() - radius; ++y)
        {
            double red = 0, green = 0, blue = 0;
            for (int i = -radius; i <= radius; ++i)
            {
                QRgb pixel = resultImage.pixel(x, y + i);
                red += qRed(pixel) * kernel[i + radius];
                green += qGreen(pixel) * kernel[i + radius];
                blue += qBlue(pixel) * kernel[i + radius];
            }
            image.setPixel(x, y, qRgb(red, green, blue));
        }
    }
    return image;
}
相关推荐
blasit1 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
CoovallyAIHub4 天前
仿生学突破:SILD模型如何让无人机在电力线迷宫中发现“隐形威胁”
深度学习·算法·计算机视觉
CoovallyAIHub4 天前
从春晚机器人到零样本革命:YOLO26-Pose姿态估计实战指南
深度学习·算法·计算机视觉
CoovallyAIHub4 天前
Le-DETR:省80%预训练数据,这个实时检测Transformer刷新SOTA|Georgia Tech & 北交大
深度学习·算法·计算机视觉
CoovallyAIHub4 天前
强化学习凭什么比监督学习更聪明?RL的“聪明”并非来自算法,而是因为它学会了“挑食”
深度学习·算法·计算机视觉
CoovallyAIHub4 天前
YOLO-IOD深度解析:打破实时增量目标检测的三重知识冲突
深度学习·算法·计算机视觉
youcans_6 天前
【AI辅助编程】ROP 图像预处理
图像处理·人工智能·ai编程·辅助编程
范特西.i6 天前
QT聊天项目(8)
开发语言·qt
这张生成的图像能检测吗6 天前
(论文速读)XLNet:语言理解的广义自回归预训练
人工智能·计算机视觉·nlp·注意力机制
十铭忘6 天前
自主认知-行动1——架构
人工智能·计算机视觉