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;
}
相关推荐
white-persist22 分钟前
Python实例方法与Python类的构造方法全解析
开发语言·前端·python·原型模式
千里马-horse23 分钟前
Async++ 源码分析8--partitioner.h
开发语言·c++·async++·partitioner
Lucis__1 小时前
再探类&对象——C++入门进阶
开发语言·c++
007php0071 小时前
某大厂跳动面试:计算机网络相关问题解析与总结
java·开发语言·学习·计算机网络·mysql·面试·职场和发展
lsx2024062 小时前
HTML 字符集
开发语言
很㗊2 小时前
C与C++---类型转换
c语言·开发语言
say_fall2 小时前
精通C语言(3. 自定义类型:联合体和枚举)
c语言·开发语言
郝学胜-神的一滴2 小时前
Effective Python 第43条:自定义容器类型为什么应该从 `collections.abc` 继承?
开发语言·python
jndingxin3 小时前
c++多线程(6)------ 条件变量
开发语言·c++
共享家95273 小时前
QT-常用控件(二)
开发语言·qt