利用QT画图像的直方图

news2024/10/6 12:35:07

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.coloradmin.cn/o/1185214.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

几种解决mfc140.dll文件缺失的方法,电脑提示mfc140.dll怎么办

电脑提示mfc140.dll缺失&#xff0c;如果你不去处理的话&#xff0c;那么你的程序游戏什么都是启动不了的&#xff0c;如果你想知道有什么方法可以解决那么可以参考这篇文章进行解决&#xff0c;今天给大家几种解决mfc140.dll文件缺失的方法。电脑提示mfc140.dll也不用担心解决…

shell的for循环

列表for循环 列表for循环的语法结构如下: for variablein list #每一次循环&#xff0c;依次把列表list 中的一个值赋给循环变量 do #循环体开始的标志commands #循环变量每取一次值&#xff0c;循环体就执行一遍commands done #循环结束的标志&#xff0c;返回循环顶…

腾讯待办关停,导出的数据怎么恢复到手机上面?

相信有不少腾讯待办的用户都发现了其“业务关停通知”&#xff0c;确实如此&#xff0c;由于业务调整&#xff0c;腾讯待办将于2023年的12月20日全面停止运营并下架&#xff0c;这就表示以后我们无法继续使用它了。在腾讯待办关停之前&#xff0c;绝大多数用户需要做的就是及时…

推荐一款功能强大的在线文件预览工具-kkFileView

程序员的公众号&#xff1a;源1024&#xff0c;获取更多资料&#xff0c;无加密无套路&#xff01; 最近整理了一波电子书籍资料&#xff0c;包含《Effective Java中文版 第2版》《深入JAVA虚拟机》&#xff0c;《重构改善既有代码设计》&#xff0c;《MySQL高性能-第3版》&…

【论文笔记】UniPAD: A Universal Pre-training Paradigm for Autonomous Driving

原文链接&#xff1a;https://arxiv.org/pdf/2310.08370.pdf 1. 引言 过去的3D场景理解预训练方法多采用2D图像领域中的想法&#xff0c;可大致分为基于对比的方法和基于MAE的方法。 基于对比的方法通过对比损失&#xff0c;在特征空间中将相似的3D点拉进而将不相似的点分开…

Oracle获取执行计划的6种方法

一、什么是执行计划&#xff1f; 执行计划是一条查询语句在Oracle中的执行过程或访问路径的描述。 执行计划描述了SQL引擎为执行SQL语句进行的操作&#xff0c;分析SQL语句相关的性能问题或仅仅质疑查询优化器的决定时&#xff0c;必须知道执行计划&#xff1b;所以执行计划常用…

Django框架的推导

文章目录 Web应用简介什么是Web框架&#xff1f;什么是Web&#xff1f;应用程序的两种模式Web应用程序的优缺点 手写Web框架HTTP协议的相关知识1.四大特性2.请求数据格式3.响应数据格式 手写框架 使用wsgiref模块基于wsgiref模块搭建Web框架(最初版)基于wsgiref模块搭建Web框架…

口袋参谋:像我这样做买家秀,日销翻一番!

​为什么别人的买家秀那么好看&#xff1f;而你家的买家秀却平平无奇&#xff01;想知道原因的&#xff0c;请往下看。 01 买家秀的作用 我们先简单说说买家秀的作用 首先就是可以表明产品和实物的真实性&#xff0c;与打开的详情页相符&#xff0c;加强详情页描述的信任度。…

Android transform旋转rotate圆角矩形图roundedCorners,Kotlin

Android transform旋转rotate圆角矩形图roundedCorners&#xff0c;Kotlin import android.graphics.Bitmap import android.os.Bundle import android.util.Log import android.widget.ImageView import androidx.appcompat.app.AppCompatActivity import com.bumptech.glide.…

YOLOv4: Optimal Speed and Accuracy of Object Detection(2020.4)

文章目录 AbstractIntroductionRelated workObject detection modelsBag of freebiesBag of specials MethodologySelection of architectureSelection of BoF and BoSAdditional improvementsYOLOv4 ExperimentsResults表8列出了使用Maxwell GPU的帧率对比结果表9列出了使用Pa…

一图掌握PMP49个过程组

一图掌握PMP 49个过程组

nigix安装以及遇到的问题

Nginx配置 nginx双击闪退如何解决 修改配置文件 端口冲突&#xff0c;将端口改为90 Nginx 动静分离&#xff08;前端的代码单独运行&#xff09; 将html文件夹里面的东西放到nginx里面的HTMl文件夹里面 负载均衡&#xff08;轮询&#xff0c;权重&#xff0c;哈希&#xff…

Android 13.0 Settings主页面去掉FocusRecyclerView相关功能

1.前言 在13.0的系统rom产品定制化开发中,在系统Settings主页面的主菜单中,在测试某些功能的时候,比如开启护眼模式和改变系统密度会在主菜单第一项的网络菜单头部增加 自定义您的设备和设置护眼模式时间安排 等等相关的设置模块 这对于菜单布局显示相当不美观,所以根据系…

nginx反向代理报错合集

本文汇集了最近在使用nginx反向代理过程中遇到的一系列错误及其解决办法。 1缺乏支持项导致nginx配置错误 在利用sudo ./configure --with-http_ssl_module --with-http_stub_status_module进行配置时&#xff0c;往往会遇到以下类型的错误 error: the HTTP rewrite module …

字符加密A--E,B-F,W--A

文章目录 前言一、题目描述 二、题目分析 三、解题 程序运行代码 前言 本系列为选择结构编程题&#xff0c;点滴成长&#xff0c;一起逆袭。 一、题目描述 二、题目分析 三、解题 程序运行代码 #include<stdio.h> int main(){char c;cgetchar();if(c>a&&…

【C/PTA——7.数组1】

C/PTA——7.数组1 7-1 计算最大值出现的次数1.题目要求2.代码实现 7-2 求一批整数中出现最多的个位数字1.题目要求2.代码实现 7-3 装箱问题1.题目要求2.代码实现 7-4 数组-值钱的微信号1.题目要求2.代码实现 7-5 数组-吹泡泡1.题目要求2.代码实现 7-6 数组-数学鬼才1.题目要求2…

基于工业无线DTU的空气污染监测防治方案

​秋冬季是我国大气污染天气的高发、频发期&#xff0c;也是大气污染防治的关键期、敏感期。针对空气质量的监测和防治&#xff0c;可以利用佰马工业无线DTU&#xff0c;搭建分布式大气传感器监测网络&#xff0c;实现对广域空气质量、成分、变化的实时监测&#xff0c;从而实现…

汽车工业生产线数字孪生可视化管理平台,赋予工厂车间数字化智慧化管理

在工业4.0 的时代背景下&#xff0c;随着企业数字化进程的推进&#xff0c;数字孪生可视化技术逐渐在汽车行业得到广泛应用&#xff0c;数字孪生智慧工厂的建设也成为了汽车行业数字化转型的趋势之一。汽车制造业属于典型的离散制造行业&#xff0c;汽车生产包含冲压、焊接、涂…

flutter显示出底部控件的引导页

需求&#xff1a;同一个页面的两个不同的入口&#xff0c;同一个控件的位置有变化&#xff0c;显示引导页时对应这个控件的引导内容的位置也需要改变&#xff1b;同时半透明底部显示出真实的页面内容。 这样的需要如果切图然后再往页面上贴位置无法精确的对准。 思路&#xff1…