根据YOLO数据集标签计算检测框内目标面积占比(YOLO7-10都适用)

news2025/5/24 19:03:17

程序:

路径改成自己的,阈值可以修改也可以默认

#zhouzhichao
#25年5月17日
#计算时频图中信号面积占检测框面积的比值


import os
import numpy as np
import pandas as pd
from PIL import Image

# Define the path to the directory containing the label txt files
label_dir = r'D:\实验室\论文\论文-多信号参数估计\JCR3\Principle\微调原理\diff_snr\snr_dataset\label_txt'
img_dir = r'D:\实验室\论文\论文-多信号参数估计\JCR3\Principle\微调原理\diff_snr\snr_dataset\6'
binary_output_dir = r'D:\实验室\论文\论文-多信号参数估计\JCR3\Principle\像素面积比值\binary img'

# Make sure the output directory exists
os.makedirs(binary_output_dir, exist_ok=True)

# Initialize lists for storing the results for each class
ratios_0 = []
ratios_1 = []
ratios_2 = []
ratios_3 = []

# Function to calculate the ratio of signal area to the bounding box area
def calculate_area_ratio(image_path, label_path, binary_output_dir):
    # Load the image and convert it to grayscale
    image = Image.open(image_path).convert('L')  # Convert to grayscale (L mode)
    image_array = np.array(image)

    # Read the label file
    with open(label_path, 'r') as file:
        labels = file.readlines()


    for label in labels:
        # Parse the label
        label_data = label.strip().split()
        class_id = int(label_data[0])  # Class ID
        x_center = float(label_data[1])  # x center normalized
        y_center = float(label_data[2])  # y center normalized
        box_width = float(label_data[3])  # box width normalized
        box_height = float(label_data[4])  # box height normalized

        # Convert the normalized values to pixel values based on image size
        img_width, img_height = image.size
        x_center_px = int(x_center * img_width)
        y_center_px = int(y_center * img_height)
        box_width_px = int(box_width * img_width)
        box_height_px = int(box_height * img_height)

        # Calculate the bounding box coordinates
        x_min = max(x_center_px - box_width_px // 2, 0)
        y_min = max(y_center_px - box_height_px // 2, 0)
        x_max = min(x_center_px + box_width_px // 2, img_width)
        y_max = min(y_center_px + box_height_px // 2, img_height)

        # Extract the bounding box region
        bbox_region = image_array[y_min:y_max, x_min:x_max]

        # Threshold the image (binary thresholding)
        binary_region = np.where(bbox_region > 128, 1, 0)  # 128 as threshold

        # Calculate the area of the signal inside the bounding box (sum of binary values)
        signal_area = np.sum(binary_region)

        # Calculate the area of the bounding box
        box_area = box_width_px * box_height_px

        # Save the binary image of the region to the output folder
        binary_image = Image.fromarray(binary_region.astype(np.uint8) * 255)  # Convert to 0-255 scale for saving
        binary_image.save(os.path.join(binary_output_dir, f'binary_{class_id}_{x_center_px}_{y_center_px}.png'))

        # Calculate the area ratio
        area_ratio = signal_area / box_area if box_area > 0 else 0

        if class_id==0:
            ratios_0.append(area_ratio)

        if class_id==1:
            ratios_1.append(area_ratio)

        if class_id == 2:
            ratios_2.append(area_ratio)

        if class_id == 3:
            ratios_3.append(area_ratio)





# Traverse all the label files and calculate area ratios
for label_file in os.listdir(label_dir):
    if label_file.endswith('.txt'):
        # Get corresponding image path
        label_path = os.path.join(label_dir, label_file)
        image_name = label_file.replace('.txt', '.jpg')  # Assuming image is in .jpg format
        image_path = os.path.join(img_dir, image_name)

        # Calculate area ratios for the current label file
        calculate_area_ratio(image_path, label_path, binary_output_dir)



data = {
    'ratios_0': ratios_0,
    'ratios_1': ratios_1,
    'ratios_2': ratios_2,
    'ratios_3': ratios_3
}

# 创建一个 DataFrame
df = pd.DataFrame(data)

# 保存到 Excel 文件
file_path = r'D:\实验室\论文\论文-多信号参数估计\JCR3\Principle\像素面积比值\area_ratios.xlsx'
df.to_excel(file_path, index=False)
print()

程序依照的公式:

生成结果:

可用于绘制论文实验图:

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

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

相关文章

LLM笔记(九)KV缓存(2)

文章目录 1. 背景与动机2. 不使用 KV Cache 的情形2.1 矩阵形式展开2.2 计算复杂度 3. 使用 KV Cache 的优化3.1 核心思想3.2 矩阵形式展开3.3 计算复杂度对比 4. 总结5. GPT-2 中 KV 缓存的实现分析5.1 缓存的数据结构与类型5.2 在注意力机制 (GPT2Attention) 中使用缓存5.3 缓…

LVS 负载均衡集群应用实战

前提:三台虚拟机,有nginx,要做负载 1. LVS-server 安装lvs管理软件 [root@lvs-server ~]# yum -y install ipvsadm 程序包:ipvsadm(LVS管理工具) 主程序:/usr/sbin/ipvsadm 规则保存工具:/usr/sbin/ipvsadm-save > /path/to/file 配置文件:/etc/sysconfig/ipvsad…

MySQL——基本查询内置函数

目录 CRUD Create Retrieve where order by limit Update Delete 去重操作 聚合函数 聚合统计 内置函数 日期函数 字符函数 数学函数 其它函数 实战OJ 批量插入数据 找出所有员工当前薪水salary情况 查找最晚入职员工的所有信息 查找入职员工时间升序排…

Day34打卡 @浙大疏锦行

知识点回归: CPU性能的查看:看架构代际、核心数、线程数GPU性能的查看:看显存、看级别、看架构代际GPU训练的方法:数据和模型移动到GPU device上类的call方法:为什么定义前向传播时可以直接写作self.fc1(x) 作业 计算资…

AdGuard解锁高级版(Nightly)_v4.10.36 安卓去除手机APP广告

AdGuard解锁高级版(Nightly)_v4.10.36 安卓去除手机APP广告 AdGuard Nightly是AdGuard团队为及时更新软件而推出的最新测试版本,适合追求最新功能和愿意尝试新版本的用户。但使用时需注意其潜在的不稳定性和风险。…

C++修炼:红黑树的模拟实现

Hello大家好&#xff01;很高兴我们又见面啦&#xff01;给生活添点passion&#xff0c;开始今天的编程之路&#xff01; 我的博客&#xff1a;<但凡. 我的专栏&#xff1a;《编程之路》、《数据结构与算法之美》、《题海拾贝》、《C修炼之路》 欢迎点赞&#xff0c;关注&am…

基于Python+YOLO模型的手势识别系统

本项目是一个基于Python、YOLO模型、PyQt5的实时手势识别系统&#xff0c;通过摄像头或导入图片、视频&#xff0c;能够实时识别并分类不同的手势动作。系统采用训练好的深度学习模型进行手势检测和识别&#xff0c;可应用于人机交互、智能控制等多种场景。 1、系统主要功能包…

自制操作系统day10叠加处理

day10叠加处理 叠加处理&#xff08;harib07b&#xff09; 现在是鼠标的叠加处理&#xff0c;以后还有窗口的叠加处理 涉及图层 最上面小图层是鼠标指针&#xff0c;最下面的一张图层用来存放桌面壁纸。移动图层的方法实现鼠标指针的移动以及窗口的移动。 struct SHEET { u…

鸿蒙Flutter实战:23-混合开发详解-3-源码模式引入

引言 在前面的文章混合开发详解-2-Har包模式引入中&#xff0c;我们介绍了如何将 Flutter 模块打包成 Har 包&#xff0c;并引入到原生鸿蒙工程中。本文中&#xff0c;我们将介绍如何通过源码依赖的方式&#xff0c;将 Flutter 模块引入到原生鸿蒙工程中。 创建工作 创建一个…

leetcode:2469. 温度转换(python3解法,数学相关算法题)

难度&#xff1a;简单 给你一个四舍五入到两位小数的非负浮点数 celsius 来表示温度&#xff0c;以 摄氏度&#xff08;Celsius&#xff09;为单位。 你需要将摄氏度转换为 开氏度&#xff08;Kelvin&#xff09;和 华氏度&#xff08;Fahrenheit&#xff09;&#xff0c;并以数…

【软件安装】Windows操作系统中安装mongodb数据库和mongo-shell工具

这篇文章&#xff0c;主要介绍Windows操作系统中如何安装mongodb数据库和mongo-shell工具。 目录 一、安装mongodb数据库 1.1、下载mongodb安装包 1.2、添加配置文件 1.3、编写启动脚本&#xff08;可选&#xff09; 1.4、启动服务 二、安装mongo-shell工具 2.1、下载mo…

记共享元素动画导致的内存泄露

最近在给项目的预览图片页增加共享元素动画的时候&#xff0c;发现了LeakCanary一直报内存泄露。 LeakCanary日志信息 ┬─── │ GC Root: Thread object │ ├─ java.lang.Thread instance │ Leaking: NO (the main thread always runs) │ Thread name: main │ …

Flyweight(享元)设计模式 软考 享元 和 代理属于结构型设计模式

1.目的&#xff1a;运用共享技术有效地支持大量细粒度的对象 Flyweight&#xff08;享元&#xff09;设计模式 是一种结构型设计模式&#xff0c;它的核心目的是通过共享对象来减少内存消耗&#xff0c;特别是在需要大量相似对象的场景中。Flyweight 模式通过将对象的共享细节与…

服务器网络配置 netplan一个网口配置两个ip(双ip、辅助ip、别名IP别名)

文章目录 问答 问 # This is the network config written by subiquity network:ethernets:enp125s0f0:dhcp4: noaddresses: [192.168.90.180/24]gateway4: 192.168.90.1nameservers:addresses:- 172.0.0.207- 172.0.0.208enp125s0f1:dhcp4: trueenp125s0f2:dhcp4: trueenp125…

响应面法(Response Surface Methodology ,RSM)

响应面法是一种结合统计学和数学建模的实验优化技术&#xff0c;通过有限的实验数据&#xff0c;建立输入变量与输出响应之间的数学模型&#xff0c;找到最优操作条件。 1.RSM定义 RSM通过设计实验、拟合数学模型&#xff08;如多项式方程&#xff09;和分析响应曲面&#xff…

Spring Boot 拦截器:解锁5大实用场景

一、Spring Boot中拦截器是什么 在Spring Boot中&#xff0c;拦截器&#xff08;Interceptor&#xff09;是一种基于AOP&#xff08;面向切面编程&#xff09;思想的组件&#xff0c;用于在请求处理前后插入自定义逻辑&#xff0c;实现权限校验、日志记录、性能监控等非业务功能…

有两个Python脚本都在虚拟环境下运行,怎么打包成一个系统服务,按照顺序启动?

环境&#xff1a; SEMCP searx.webapp python 问题描述&#xff1a; 有两个python脚本都在虚拟环境下运行&#xff0c;怎么打包成一个系统服务&#xff0c;按照顺序启动&#xff1f; 解决方案&#xff1a; 将这两个 Python 脚本打包成有启动顺序的系统服务&#xff0c;最…

Python 脚本执行命令的深度探索:方法、示例与最佳实践

在现代软件开发过程中&#xff0c;Python 脚本常常需要与其他工具和命令进行交互&#xff0c;以实现自动化任务、跨工具数据处理等功能。Python 提供了多种方式来执行外部命令&#xff0c;并获取其输出&#xff0c;重定向到文件&#xff0c;而不是直接在终端中显示。这种能力使…

PotPlayer 4K 本地万能影音播放器

今日分享一款来自吾爱论坛大佬分享的啥都能播的的本地播放器&#xff0c;不管是不管是普通视频、4K超清、蓝光3D&#xff0c;还是冷门格式&#xff0c;它基本都能搞定。而且运行流畅不卡顿&#xff0c;电脑配置低也能靠硬件加速&#xff0c;让你根本停不下来。 自带解码器&…

2025年电工杯A题第一版本Q1-Q4详细思路求解+代码运行

A题 光伏电站发电功率日前预测问题 问题背景 光伏发电是通过半导体材料的光电效应&#xff0c;将太阳能直接转化为电能的技术。光伏电站是由众多光伏发电单元组成的规模化发电设施。 光伏电站的发电功率主要由光伏板表面接收到的太阳辐射总量决定&#xff0c;不同季节太阳光…