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;
}
相关推荐
你是狒狒吗6 分钟前
TM中,return new TransactionManagerImpl(raf, fc);为什么返回是new了一个新的实例
java·开发语言·数据库
勤奋的知更鸟17 分钟前
Java编程之组合模式
java·开发语言·设计模式·组合模式
虾球xz24 分钟前
CppCon 2015 学习:3D Face Tracking and Reconstruction using Modern C++
开发语言·c++·学习·3d
林鸿群26 分钟前
C#子线程更新主线程UI及委托回调使用示例
开发语言·c#
奥修的灵魂39 分钟前
QT进阶之路:带命名空间的自定义控件在Qt设计器与qss中的使用技巧
qt·命名空间
时间之里2 小时前
【图像处理3D】:世界坐标系
图像处理·数码相机·3d
SteveDraw3 小时前
C++动态链接库封装,供C#/C++ 等编程语言使用——C++动态链接库概述(总)
开发语言·c++·c#·封装·动态链接库
十五年专注C++开发3 小时前
设计模式之单例模式(二): 心得体会
开发语言·c++·单例模式·设计模式
flyair_China4 小时前
【云架构】
开发语言·php