2023年亚太杯数学建模A题水果采摘机器人的图像识别功能(基于yolov5的苹果分割)

注:.题中附录并没有给出苹果的标签集,所以需要我们自己通过前4问得到训练的标签集,采用的是yolov5 7.0 版本,该版本带分割功能

一:关于数据集的制作:

clc;
close all;
clear;
%-----这个是生成yolov5 数据集的--------
% 图像文件夹路径
folder_path = 'E:/新建文件夹/yatai/Attachment/Apple/';
% 图像文件列表
image_files = dir(fullfile(folder_path, '*.jpg')); % 假设所有图片都是jpg格式% 解析文件名中的数字,并转换为数值类型
numbers = cellfun(@(x) sscanf(x, '%d.jpg'), {image_files.name});% 根据解析出的数字对文件列表进行排序
[~, sorted_idx] = sort(numbers);
image_files = image_files(sorted_idx);
% 存储每张图片苹果数量的数组
apple_counts = zeros(length(image_files), 1);% 存储每张图片的平均成熟度评分
average_red_intensity_ratio_per_image = zeros(length(image_files), 1);% 确保输出文件夹存在
output_folder = 'E:\新建文件夹\yatai\Attachment\Attachment 2\APPlemasktxt';
if ~exist(output_folder, 'dir')mkdir(output_folder);
end% 存储每张图片的平均苹果质量评分
average_quality_scores_per_image = zeros(length(image_files), 1);
% 遍历每张图片
for i = 1: length(image_files) %2  % 读取图像img = imread(fullfile(folder_path, image_files(i).name));·······省略了部分代码% 给分割的对象标记不同的标签labelled_img = bwlabel(binary_img);
%     figure;
%     在原始图像上绘制分割结果
%     imshow(img);
%     hold on;colors=['b' 'g' 'r' 'c' 'm' 'y'];for k = 1:length(unique(labelled_img)) - 1boundary = bwboundaries(labelled_img == k);for b = 1:length(boundary)plot(boundary{b}(:,2), boundary{b}(:,1), colors(mod(k,length(colors))+1), 'LineWidth', 2);endend% title('Segmented Apples');% hold off;% 计数分割后的苹果number_of_apples = max(labelled_img(:));disp(['Number of segmented apples: ', num2str(number_of_apples)]);apple_counts(i) = number_of_apples;% 打印当前图片的苹果数量fprintf('Image %d (%s): %d apples detected.\n', i, image_files(i).name, number_of_apples);%下面是制作分割的数据集% 给分割的对象标记不同的标签labelled_img = bwlabel(binary_img);% 准备写入YOLOv5格式的分割轮廓点文件% 根据图像文件名创建对应的txt文件名baseFileName = sprintf('%d.txt', i);txt_filename = fullfile(output_folder, baseFileName);fileID = fopen(txt_filename, 'w');% 确保文件已成功打开if fileID == -1error('Cannot open file %s for writing.', txt_filename);end% 获取图像尺寸img_height = size(img, 1);img_width = size(img, 2);% 遍历每个苹果,写入轮廓点信息for k = 1:max(labelled_img(:))[B,~] = bwboundaries(labelled_img == k, 'noholes');contours = B{1}; % 取第一组轮廓点% 检查contours的尺寸if size(contours, 2) == 2 % 确保contours有两列% 转换为归一化坐标contours_normalized = contours ./ [img_height,  img_width];% 写入文件fprintf(fileID, '0 '); % 假设苹果的类别ID为0for p = 1:size(contours_normalized, 1)
%                 fprintf('Plotting point at (%f, %f)\n', contours_normalized(p, 2), contours_normalized(p, 1)); % 调试信息fprintf(fileID, '%f %f ', contours_normalized(p, 2), contours_normalized(p, 1));endfprintf(fileID, '\n');elsewarning('Contour for apple %d in image %d does not have correct dimensions.', k, i);endendfclose(fileID);end

二:关于yolov5 7.0 的训练:

我的电脑是3080 训练了20轮测试,下面就是部分测试的结果

下面是关于数据集的划分代码 

'''
Descripttion: split_img.py
version: 1.0
Author: UniDome
Date: 2022-04-20 16:28:45
LastEditors: UniDome
LastEditTime: 2022-04-20 16:39:56
'''
import os, shutil, random
from tqdm import tqdmdef split_img(img_path, label_path, split_list):try:  # 创建数据集文件夹Data = 'E:/新建文件夹/yatai/Attachment/Attachment 2/output'os.mkdir(Data)train_img_dir = Data + '/images/train'val_img_dir = Data + '/images/val'test_img_dir = Data + '/images/test'train_label_dir = Data + '/labels/train'val_label_dir = Data + '/labels/val'test_label_dir = Data + '/labels/test'# 创建文件夹os.makedirs(train_img_dir)os.makedirs(train_label_dir)os.makedirs(val_img_dir)os.makedirs(val_label_dir)os.makedirs(test_img_dir)os.makedirs(test_label_dir)except:print('文件目录已存在')train, val, test = split_listall_img = os.listdir(img_path)all_img_path = [os.path.join(img_path, img) for img in all_img]# all_label = os.listdir(label_path)# all_label_path = [os.path.join(label_path, label) for label in all_label]train_img = random.sample(all_img_path, int(train * len(all_img_path)))train_img_copy = [os.path.join(train_img_dir, img.split('\\')[-1]) for img in train_img]train_label = [toLabelPath(img, label_path) for img in train_img]train_label_copy = [os.path.join(train_label_dir, label.split('\\')[-1]) for label in train_label]for i in tqdm(range(len(train_img)), desc='train ', ncols=80, unit='img'):_copy(train_img[i], train_img_dir)_copy(train_label[i], train_label_dir)all_img_path.remove(train_img[i])val_img = random.sample(all_img_path, int(val / (val + test) * len(all_img_path)))val_label = [toLabelPath(img, label_path) for img in val_img]for i in tqdm(range(len(val_img)), desc='val ', ncols=80, unit='img'):_copy(val_img[i], val_img_dir)_copy(val_label[i], val_label_dir)all_img_path.remove(val_img[i])test_img = all_img_pathtest_label = [toLabelPath(img, label_path) for img in test_img]for i in tqdm(range(len(test_img)), desc='test ', ncols=80, unit='img'):_copy(test_img[i], test_img_dir)_copy(test_label[i], test_label_dir)def _copy(from_path, to_path):shutil.copy(from_path, to_path)def toLabelPath(img_path, label_path):img = img_path.split('\\')[-1]label = img.split('.jpg')[0] + '.txt'return os.path.join(label_path, label)def main():img_path = r'E:\新建文件夹\yatai\Attachment\Apple'label_path = r'E:\新建文件夹\yatai\Attachment\Attachment 2\APPlemasktxt'split_list = [0.7, 0.2, 0.1]  # 数据集划分比例[train:val:test]split_img(img_path, label_path, split_list)if __name__ == '__main__':main()

A题详细代码数据集
https://docs.qq.com/doc/DZHh5ckNrWlNybFNs

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

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

相关文章

任务4-绘制图形

python字典的使用方法 !echo $(date)‘开始下载并解压’ && curl -o Task4.zip https://zyenv-1302342904.cos.ap-guangzhou.myqcloud.com/datas/TianJin/Task4_TJ_ZZ.zip && unzip -o Task4.zip > /dev/null 2>&1 && echo $(date)‘解压完…

学习课题:逐步构建开发播放器【QT5 + FFmpeg6 + SDL2】

目录 一、播放器开发(一):播放器组成大致结构与代码流程设计 二、播放器开发(二):了解FFmpeg与SDL常用对象和函数 三、播放器开发(三):FFmpeg与SDL环境配置 四、播放器开发(四):多线程解复用与解码模块实现 五、播放器开发(五…

Linux应用开发基础知识——I2C应用编程(十三)

一、无需编写驱动程序即可访问 I2C 设备 APP 访问硬件肯定是需要驱动程序的,对于 I2C 设备,内核提供了驱动程序 drivers/i2c/i2c-dev.c,通过它可以直接使用下面的 I2C 控制器驱动程序来访问 I2C 设备。 i2c-tools 是一套好用的工具&#xff0…

虚拟机系列:Oracle VM VirtualBox虚拟机的使用教程和使用体验情况反馈

Oracle VM VirtualBox虚拟机的使用教程和使用体验情况反馈 一. 简述:二. 下载三. 安装解压后选择需要的版本点击安装1:第一步,点击安装,点击下一步2. 这里直接点击下一步,3. 网络警告选择:是4. 准备好以后,点击安装5. 点击完成即可四. 打开五. 创建虚拟机1. 输入虚拟机名…

H5(uniapp)中使用echarts

1,安装echarts npm install echarts 2&#xff0c;具体页面 <template><view class"container notice-list"><view><view class"aa" id"main" style"width: 500px; height: 400px;"></view></v…

MySQL 中的 JSON_CONTAINS 函数详解

在处理 MySQL 中的 JSON 数据时&#xff0c;我们经常需要检查一个 JSON 文档是否包含特定的值。这时&#xff0c;JSON_CONTAINS 函数就显得非常有用。 JSON_CONTAINS函数介绍 JSON_CONTAINS 是 MySQL 提供的一个 JSON 函数&#xff0c;用于测试一个 JSON 文档是否包含特定的值…

SQLite 和 SQLiteDatabase 的使用

实验七&#xff1a;SQLite 和 SQLiteDatabase 的使用 7.1 实验目的 本次实验的目的是让大家熟悉 Android 中对数据库进行操作的相关的接口、类等。SQLiteDatabase 这个是在 android 中数据库操作使用最频繁的一个类。通过它可以实现数据库的创建或打开、创建表、插入数据、删…

22、什么是中间件和权限拦截中间件实操

新建中间件 middleware\auth.js // 定义权限判断中间件&#xff0c;中间件的第一个参数是context export default ({store, redirect}) > {console.log("中间件被调用")// if (!store || !store.state.userinfo) {// redirect("/")// } }页面使用…

CF -- Educational Codeforces Round 158 (Rated for Div. 2) -- D 补题记录

Yet Another Monster Fight Problem - D - Codeforces 题目大意&#xff1a; 现在给你一堆怪物&#xff0c;你拥有法术&#xff08;一个法术可以连续攻击这n个所有怪物&#xff09;&#xff0c;你可以选择任意一个怪物作为法术的第一个攻击目标&#xff08;伤害为x&#xff…

【MySQL】索引与事务

&#x1f451;专栏内容&#xff1a;MySQL⛪个人主页&#xff1a;子夜的星的主页&#x1f495;座右铭&#xff1a;前路未远&#xff0c;步履不停 目录 一、索引1、使用场景2、使用索引创建索引查看索引删除索引 3、底层数据结构&#xff08;非常重要&#xff09; 二、事务1、概念…

Android设计模式--享元模式

水不激不跃&#xff0c;人不激不奋 一&#xff0c;定义 使用共享对象可有效地支持大量的细粒度的对象 享元模式是对象池的一种实现&#xff0c;用来尽可能减少内存使用量&#xff0c;它适合用于可能存在大量重复对象的场景&#xff0c;来缓存可共享的对象&#xff0c;达到对象…

腾讯云CVM标准型SA5云服务器AMD EPYC Bergamo处理器

腾讯云服务器标准型SA5实例是最新一代的标准型实例&#xff0c;CPU采用AMD EPYC™ Bergamo全新处理器&#xff0c;采用最新DDR5内存&#xff0c;默认网络优化&#xff0c;最高内网收发能力达4500万pps。腾讯云百科txybk.com分享腾讯云标准型SA5云服务器CPU、内存、网络、性能、…

Qt项目打包发布超详细教程

https://blog.csdn.net/qq_45491628/article/details/129091320

用苹果签名免费获取Xcode

使用苹果企业签名免费获取Xcode&#xff1a; 打开Xcode。连接iOS设备到Mac。选择Window→Devices and Simulators。选择该设备。将IPA文件拖到“Installed Apps”的列表框中即可安装。使用Cydia Impactor&#xff08;可以在网上找到相关下载链接&#xff09;&#xff1a; 打开…

HTML网站稳定性状态监控平台源码

这是一款网站稳定性状态监控平台源码&#xff0c;它基于UptimeRobot接口进行开发。当您的网站遇到故障时&#xff0c;该平台能够通过邮件或短信通知您。下面是对安装过程的详细说明&#xff1a; 安装步骤 将源码上传至您的主机或服务器&#xff0c;并进行解压操作。 在Uptim…

自动化测试中几种常见验证码的处理方式及如何实现?

UI自动化测试时&#xff0c;需要对验证码进行识别处理&#xff0c;有很多方式&#xff0c;每种方式都有自己的特点&#xff0c;以下是一些常用处理方法&#xff0c;仅供参考。 1 去掉验证码 从自动化的本质上来讲&#xff0c;主要是提升测试效率等&#xff0c;但是为了去研究验…

【点云surface】 修剪B样条曲线拟合

1 介绍 Fitting trimmed B-splines&#xff08;修剪B样条曲线拟合&#xff09;是一种用于对给定的点云数据进行曲线拟合的算法。该算法使用B样条曲线模型来逼近给定的点云数据&#xff0c;并通过对模型进行修剪来提高拟合的精度和准确性。 B样条曲线是一种常用的曲线表示方法…

【element优化经验】el-dialog修改title样式

目录 前言 解决之路 1.把默认的这个图标隐藏&#xff0c;官方的api有这个属性&#xff1a;showClose值设置false. 2.title插槽定制&#xff1a;左边定制标题&#xff0c;右边定制按钮区域。 3.背景颜色修改&#xff1a;默认title是有padding的需要把它重写调&#xff0c;然…

基于卷积神经网络CNN开发构建HAR人类行为识别Human Activity Recognition【完整代码实践】

行为识别相关的开发实践在我们之前的博文中也有过相关的实践了,感兴趣的话可以自行移步阅读即可:《python实现基于TNDADATASET的人体行为识别》 《UCI行为识别——Activity recognition with healthy older people using a batteryless wearable sensor Data Set》《人体行为…

基于 STM32Cube.AI 的嵌入式人脸识别算法实现

本文介绍了如何使用 STM32Cube.AI 工具开发嵌入式人脸识别算法。首先&#xff0c;我们将简要介绍 STM32Cube.AI 工具和 STM32F系列单片机的特点。接下来&#xff0c;我们将详细讨论如何使用 STM32Cube.AI 工具链和相关库来进行人脸识别算法的开发和优化。最后&#xff0c;我们提…