Qt图像处理技术九:得到QImage图像的灰度直方图

效果

原理

得到灰度化值,将灰度化的值带入0-255内,增加,得到可视化图形

源码

cpp 复制代码
// 绘制直方图
QImage drawHistogram(const QImage &image)
{
    QVector<int> histogram(256, 0);

    // 计算图像的灰度直方图
    for (int y = 0; y < image.height(); ++y) {
        for (int x = 0; x < image.width(); ++x) {
            QColor color(image.pixel(x, y));
            int intensity = qGray(color.rgb());
            histogram[intensity]++;
        }
    }

    int maxValue = *std::max_element(histogram.constBegin(), histogram.constEnd());

    QImage histogramImage(256, maxValue, QImage::Format_RGB32);
    histogramImage.fill(Qt::white);

    QPainter painter(&histogramImage);
    painter.setPen(Qt::black);

    for (int i = 0; i < histogram.size(); ++i) {
        int height = histogram[i] * histogramImage.height() / maxValue;
        painter.drawLine(i, histogramImage.height(), i, histogramImage.height() - height);
    }

    return histogramImage;
}
相关推荐
AI科技星19 小时前
《全域数学/数术工坊》体系总览
c语言·开发语言·汇编·electron·概率论
范什么特西19 小时前
Maven中dependencies和dependencyManagement区别
java·开发语言·maven
techdashen20 小时前
Rust 项目进展月报:2026 年 1 月
开发语言·后端·rust
AI行业学习20 小时前
CC‑Switch v3.16.1 免费下载(Windows+macOS+Linux)、使用方法【2026.6.11】
linux·开发语言·windows·python·macos·前端框架·html
攻城狮Soar20 小时前
STL源码解析之deque
开发语言·c++
LDR00620 小时前
宠物电器供电革新:USB-C PD标准化,重塑30-65W设备体验
c语言·开发语言·宠物
张忠琳20 小时前
【Go 1.26.4】Golang Interface 接口深度解析
开发语言·golang
伊灵eLing20 小时前
GoLang 语言高级(1)
开发语言·后端·golang
zzz_236820 小时前
【Java基础】泛型的门道:伪泛型的真相
java·开发语言
小鱼仙官20 小时前
Windows Qt调用Vs库实现UDP双口接收数据
开发语言·qt