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;
}
相关推荐
似水明俊德2 小时前
02-C#.Net-反射-面试题
开发语言·面试·职场和发展·c#·.net
Thera7772 小时前
C++ 高性能时间轮定时器:从单例设计到 Linux timerfd 深度优化
linux·开发语言·c++
炘爚3 小时前
C语言(文件操作)
c语言·开发语言
阿蒙Amon3 小时前
C#常用类库-详解SerialPort
开发语言·c#
凸头4 小时前
CompletableFuture 与 Future 对比与实战示例
java·开发语言
wuqingshun3141594 小时前
线程安全需要保证几个基本特征
java·开发语言·jvm
Moksha2624 小时前
5G、VoNR基本概念
开发语言·5g·php
jzlhll1234 小时前
kotlin Flow first() last()总结
开发语言·前端·kotlin
W.D.小糊涂4 小时前
gpu服务器安装windows+ubuntu24.04双系统
c语言·开发语言·数据库
用头发抵命4 小时前
Vue 3 中优雅地集成 Video.js 播放器:从组件封装到功能定制
开发语言·javascript·ecmascript