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;
}
相关推荐
qianbo_insist23 分钟前
c++ python 共享内存
开发语言·c++·python
今天背单词了吗98038 分钟前
算法学习笔记:8.Bellman-Ford 算法——从原理到实战,涵盖 LeetCode 与考研 408 例题
java·开发语言·后端·算法·最短路径问题
CoderPractice44 分钟前
C#控制台小项目-飞行棋
开发语言·c#·小游戏·飞行棋
Coding小公仔1 小时前
LeetCode 151. 反转字符串中的单词
开发语言·c++·算法
程序猿阿伟1 小时前
《声音的变形记:Web Audio API的实时特效法则》
开发语言·前端·php
Humbunklung2 小时前
Rust方法语法:赋予结构体行为的力量
开发语言·后端·rust
萧曵 丶2 小时前
Rust 内存结构:深入解析
开发语言·后端·rust
算法练习生2 小时前
Qt核心类QWidget及其派生类详解
开发语言·c++·qt
1024小神2 小时前
tauri项目在windows上的c盘没有权限写入文件
c语言·开发语言·windows