利用QT画图像的直方图

1.什么是直方图

直方图是一种图形化展示数据频率分布的方式。它将样本数据分成一系列相邻的区间,统计每个区间内数据所占比例或数量,并用矩形条形图表现出来。直方图可以反映样本数据的分布情况,例如它们的集中趋势、对称性和离散程度等。

直方图在数据分析和处理过程中有广泛的应用,例如:

  1. 可以用于检查数据是否符合正态分布,从而判断使用什么类型的统计方法。

  2. 可以用于比较多组数据的分布情况,从而寻找它们的异同点。

  3. 可以用于数字图像处理中,对图像像素的亮度、对比度等特征进行定量描述。

  4. 可以用于形态学图像处理中,比如分割和重建等。

总的来说,直方图的作用是通过直观的图形展示方式,帮助分析者快速理解和评估数据的统计特征,从而更准确和全面地进行数据分析和处理。

2.qt编写灰度图像直方图

   // 统计灰度级别的像素数量 + 绘制直方图
    QVector<int> histogram(256, 0); // 存储每个灰度级别的像素数量
    for (int i = 0; i < image.width(); i++)
    {
        for (int j = 0; j < image.height(); j++)
        {
            QColor color(image.pixel(i, j));
            int gray = qGray(color.rgb()); // 获取灰度级别
            if((gray >= 30) &&( gray <= 225))
            {
                histogram[gray]++;
            }
        }
    }
    QImage histogramImage(256, 256, QImage::Format_RGB32); // 创建直方图图像
    histogramImage.fill(Qt::black); // 设置背景颜色
    QPainter painter(&histogramImage);
    painter.setPen(Qt::white);
    int maxCount = *std::max_element(histogram.begin(), histogram.end()); // 获取像素数量最大值
    for (int i = 0; i < 256; i++)
    {
        int count = histogram[i];
        int x = i;
        int y = histogramImage.height() - (static_cast<double>(count) / maxCount) * histogramImage.height();
        painter.drawLine(x, histogramImage.height(), x, y);
    }

    QPixmap pixmap_hisinput;
    pixmap_hisinput = pixmap_hisinput.fromImage(histogramImage);
    ui->lb_histir->setPixmap(pixmap_hisinput.scaled(ui->lb_histir->size(), Qt::KeepAspectRatio, Qt::FastTransformation));

 

3.qt绘制rgb图像直方图

   QVector<int> redHistogram(256, 0); // 存储红色通道灰度级别的像素数量
    QVector<int> greenHistogram(256, 0); // 存储绿色通道灰度级别的像素数量
    QVector<int> blueHistogram(256, 0); // 存储蓝色通道灰度级别的像素数量

    for (int i = 0; i < image.width(); i++)
    {
        for (int j = 0; j < image.height(); j++)
        {
            QColor color(image.pixel(i, j));
            int red = color.red(); // 获取红色通道的灰度级别
            int green = color.green(); // 获取绿色通道的灰度级别
            int blue = color.blue(); // 获取蓝色通道的灰度级别

            //if ((red >= 30) && (red <= 225) && (green >= 30) && (green <= 225) && (blue >= 30) && (blue <= 225))
            {
                redHistogram[red]++;
                greenHistogram[green]++;
                blueHistogram[blue]++;
            }
        }
    }

    // 绘制红色通道直方图
    QImage redHistogramImage(256, 256, QImage::Format_RGB32); // 创建红色通道直方图图像
    redHistogramImage.fill(Qt::black); // 设置背景颜色
    QPainter redHistogramPainter(&redHistogramImage);
    redHistogramPainter.setPen(Qt::red);
    int maxRedCount = *std::max_element(redHistogram.begin(), redHistogram.end());
    for (int i = 0; i < 256; i++)
    {
        int count = redHistogram[i];
        int x = i;
        int y = redHistogramImage.height() - (static_cast<double>(count) / maxRedCount) * redHistogramImage.height();
        redHistogramPainter.drawLine(x, redHistogramImage.height(), x, y);
    }

    // 绘制绿色通道直方图
    QImage greenHistogramImage(256, 256, QImage::Format_RGB32); // 创建绿色通道直方图图像
    greenHistogramImage.fill(Qt::black); // 设置背景颜色
    QPainter greenHistogramPainter(&greenHistogramImage);
    greenHistogramPainter.setPen(Qt::green);
    int maxGreenCount = *std::max_element(greenHistogram.begin(), greenHistogram.end());
    for (int i = 0; i < 256; i++)
    {
        int count = greenHistogram[i];
        int x = i;
        int y = greenHistogramImage.height() - (static_cast<double>(count) / maxGreenCount) * greenHistogramImage.height();
        greenHistogramPainter.drawLine(x, greenHistogramImage.height(), x, y);
    }

    // 绘制蓝色通道直方图
    QImage blueHistogramImage(256, 256, QImage::Format_RGB32); // 创建蓝色通道直方图图像
    blueHistogramImage.fill(Qt::black); // 设置背景颜色
    QPainter blueHistogramPainter(&blueHistogramImage);
    blueHistogramPainter.setPen(Qt::blue);
    int maxBlueCount = *std::max_element(blueHistogram.begin(), blueHistogram.end());
    for (int i = 0; i < 256; i++)
    {
        int count = blueHistogram[i];
        int x = i;
        int y = blueHistogramImage.height() - (static_cast<double>(count) / maxBlueCount) * blueHistogramImage.height();
        blueHistogramPainter.drawLine(x, blueHistogramImage.height(), x, y);
    }

    // 将直方图图像显示到三个控件中
    QPixmap redPixmap;
    redPixmap = redPixmap.fromImage(redHistogramImage);
    ui->lb_histrgb_red->setPixmap(redPixmap.scaled(ui->lb_histrgb_red->size(), Qt::KeepAspectRatio, Qt::FastTransformation));

    QPixmap greenPixmap;
    greenPixmap = greenPixmap.fromImage(greenHistogramImage);
    ui->lb_histrgb_green->setPixmap(greenPixmap.scaled(ui->lb_histrgb_green->size(), Qt::KeepAspectRatio, Qt::FastTransformation));

    QPixmap bluePixmap;
    bluePixmap = bluePixmap.fromImage(blueHistogramImage);
    ui->lb_histrgb_blue->setPixmap(bluePixmap.scaled(ui->lb_histrgb_blue->size(), Qt::KeepAspectRatio, Qt::FastTransformation));

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/124535.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

2011年03月17日 Go生态洞察:探索Go与C的交互——Cgo

&#x1f337;&#x1f341; 博主猫头虎&#xff08;&#x1f405;&#x1f43e;&#xff09;带您 Go to New World✨&#x1f341; &#x1f984; 博客首页——&#x1f405;&#x1f43e;猫头虎的博客&#x1f390; &#x1f433; 《面试题大全专栏》 &#x1f995; 文章图文…

【深度学习】手写数字识别

文章目录 一、步骤1.导包2.查看数据集图片3.多层感知器4.结果 总结 一、步骤 1.导包 #导入需要的包 import numpy as np import paddle as paddle import paddle.fluid as fluid from PIL import Image import matplotlib.pyplot as plt import os from paddle.fluid.dygraph…

Linux awk命令

除了使用 sed 命令&#xff0c;Linux 系统中还有一个功能更加强大的文本数据处理工具&#xff0c;就是 awk。 曾有人推测 awk 命令的名字来源于 awkward 这个单词。其实不然&#xff0c;此命令的设计者有 3 位&#xff0c;他们的姓分别是 Aho、Weingberger 和 Kernighan&#x…

第十八章Swing程序设计总结

例题18.1&#xff1a;第一个窗体程序 例题18.2&#xff1a;在窗体中弹出对话框 例题18.3&#xff1a;弹出会话框&#xff0c;问用户准备好了吗&#xff1f; 例题18.4&#xff1a;弹出会话框&#xff0c;询问用户是否离开 例题18.5&#xff1a;弹出会话框&#xff0c;让用户输入…

[LeetCode]-160. 相交链表-141. 环形链表-142.环形链表II

目录 160.相交链表 题目 思路 代码 141.环形链表 题目 思路 代码 142.环形链表II 题目 思路 代码 160.相交链表 160. 相交链表 - 力扣&#xff08;LeetCode&#xff09;https://leetcode.cn/problems/intersection-of-two-linked-lists/description/ 题目 给你两个…

开源七轴myArm协作机械臂正逆运动学技术讲解

引言&#xff1a; 在本文中&#xff0c;我们将深入探讨机器人学的两个核心概念&#xff1a;正运动学和逆运动学。这两个概念是理解和控制机械臂运动的基础。通过一个具体的7轴机械臂实例&#xff0c;我们将详细介绍如何计算机械臂的正运动学和逆运动学。我们首先会解释正运动学…

[设计模式] 建造者模式

一、引言 起因是学习okhttp过程中遇到的这段代码 Request request original.newBuilder().url(original.url()).header("Authorization", "Bearer " BearerTokenUtils.getToken(configuration.getApiKey(), configuration.getApiSecret())).header(&quo…

《第三期(先导课)》之《Python 开发环境搭建》

文章目录 《第 1 节 初始Python》《第 6 节 pip包管理工具》 《第 1 节 初始Python》 。。。 《第 6 节 pip包管理工具》 pip是Python的包管理工具,用于安装、升级和管理Python包。 pip是Python标准库之外的一个第三方工具,可以从Python Package Index(PyPI)下载和安装各种P…

在PostgreSQL中创建和管理数据库

PostgreSQL是一个强大、开源的关系型数据库管理系统&#xff0c;它提供了丰富的功能和灵活的配置选项&#xff0c;使得它成为许多开发者和组织的首选数据库之一&#xff0c;接下来我会介绍如何在PostgreSQL中创建和管理数据库。 一、安装和配置PostgreSQL 第一步&#xff0c;…

嵌软工程师要掌握的硬件知识2:一文看懂什么是开漏和推挽电路(open-drain / push-pull)

文 / 黑猫学长 本文根据笔者个人工作/学习经验整理而成,如有错误请留言。 文章为付费内容,已加入原创侵权保护,禁止私自转载及抄袭。 文章所在专栏: 嵌软工程师要掌握的硬件知识 1 推挽(push pull)电路 1.1 理解什么是推挽电路 - 详细介绍 如图所示,Q3是个NPN型三极管…

【mysql】CommunicationsException: Communications link failure

CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. 通信异常&#xff1a;通信链路故障 最后一个成功发送到服务器的数据包是0毫秒前…

【Unity实战】实现强大通用易扩展的对话系统(附项目源码)

先看看实现的最终效果 前言 之前的对话系统因为存在一些错误和原作者不允许我分享&#xff0c;所以被我下架了&#xff0c;而且之前对话系统确实少了一些功能&#xff0c;比如最基本的逐字打印功能&#xff0c;原本来是打算后面补充的。 对话系统在游戏中实现太常见了&#x…

基于 Python 的课程助教智能聊天机器人

温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长 Wechat / QQ 名片 :) 1. 项目简介 课程助教是高校中一种常见的教学模式,其在学生理论知识的掌握与实践能力的提高方面起到关键性的作用,已经成为高校日常教育环节中不可或缺的一环。然而,传统的人力助教有若干关键问题亟待…

Leangoo领歌免费Scrum管理工具中如何看到关于自己的所有任务?

个人工作台 个人工作台是个人最新待办工作的展示区域&#xff0c;它展示了个人所有的待办任务&#xff0c;最新访问的项目和工作动态&#xff0c;当一个人在多个项目和看板上工作时&#xff0c;它可以帮助个人快速看到个人在各个项目的工作&#xff0c;快速进入任务看板处理任…

Flink集群的搭建

1、Flink独立集群模式 1、首先Flink的独立集群模式是不依赖于Hadoop集群。 2、上传压缩包&#xff0c;配置环境&#xff1a; 1、解压&#xff1a; tar -zxvf flink-1.15.2-bin-scala_2.12.tgz2、配置环境变量&#xff1a;vim /etc/profileexport FLINK_HOME/usr/local/soft/fl…

3、FFmpeg基础

1、FFmpeg 介绍 FFmpeg是一套可以用来记录、转换数字音频、视频&#xff0c;并能将其转化为流的开源计算机程序。采用LGPL或GPL许可证。它提供了录制、转换以及流化音视频的完整解决方案。它包含了非常先进的音频/视频编解码库。 2、FFmpeg 组成 - libavformat&#xff1a;用于…

【理解链表指针赋值】链表中cur->next = cur->next->next->next与cur =cur->next->next的区别

最近在做链表的题目的时候&#xff0c;对于所定义的cur链表指针产生了一些疑惑&#xff0c;查阅资料后整理一下我的理解&#xff1a; /*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(n…

AI创作系统ChatGPT商业运营系统源码+支持GPT4/支持ai绘画

一、AI创作系统 SparkAi创作系统是基于OpenAI很火的ChatGPT进行开发的Ai智能问答系统和Midjourney绘画系统&#xff0c;支持OpenAI-GPT全模型国内AI全模型。本期针对源码系统整体测试下来非常完美&#xff0c;可以说SparkAi是目前国内一款的ChatGPT对接OpenAI软件系统。那么如…

工程(十二)Ubuntu20.04LSD_SLAM运行

博主创建了一个科研互助群Q&#xff1a;772356582&#xff0c;欢迎大家加入讨论。这是一个科研互助群&#xff0c;主要围绕机器人&#xff0c;无人驾驶&#xff0c;无人机方面的感知定位&#xff0c;决策规划&#xff0c;以及论文发表经验&#xff0c;以方便大家很好很快的科研…

CSS 显示、定位、布局、浮动

一、CSS 显示&#xff1a; CSS display属性设置元素应如何显示&#xff1b;CSS visibility属性指定元素应可见还是隐藏。隐藏元素可以通过display属性设置为“none”&#xff0c;也可以通过visibility属性设置为“hidden”。两者的区别&#xff1a;visibility:hidden可以隐藏某…