2023年亚太杯数学建模A题水果采摘机器人的图像识别功能(matlab 部分代码)

对于1-4问针对的是附录1 中的数据

复制代码
clc;
close all;
clear;
% 图像文件夹路径
folder_path = 'E:/新建文件夹/yatai/Attachment/Attachment 1/';
% 图像文件列表
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);

1,需要对原始的数据预操作,进行数据增强增强

复制代码
% 应用Retinex算法
    sigma = 150; % 高斯滤波器的标准差,可以调整
    enhanced_img = singleScaleRetinex(img, sigma);

2.转换色彩空间 进行直方图均值化

复制代码
 % 转换到YCbCr色彩空间进行直方图均衡化
img_ycbcr = rgb2ycbcr(enhanced_img);
Y_channel = img_ycbcr(:,:,1); % Y通道
img_ycbcr(:,:,1) = histeq(Y_channel); % 对Y通道进行直方图均衡化

3.LAb色彩空间

复制代码
% 将处理后的图像转换回RGB色彩空间
    img_eq = ycbcr2rgb(img_ycbcr);
    
    % 转换到LAB色彩空间
    img_lab = rgb2lab(img);
    % 分别获取L*, a*, b*通道
    L_channel = img_lab(:,:,1);  % L* 亮度通道
    a_channel = img_lab(:,:,2);  % a* 通道,从绿色到红色
    b_channel = img_lab(:,:,3);  % b* 通道,从蓝色到黄色

4.k-means聚类

复制代码
% 使用k-means算法在a_channel进行颜色聚类
    numOfClusters = 2; % 你想要的聚类数量
    [cluster_idx, cluster_center] = kmeans(a_channel_reshape, numOfClusters, 'Distance', 'sqEuclidean', 'Replicates', 3);
    
    % 将聚类索引重塑回图像的大小
    clustered_img = reshape(cluster_idx, rows, cols);
    
    clustered_img_color = label2rgb(clustered_img);

更多参考资料见下方:

​​​​​​​建模忠哥

基于1-4问生成的附录2 中的训练集标签用于后续yolov5对苹果的分割检测

相关推荐
weixin_307779134 小时前
C++代码实现MATLAB中的dlarray函数功能
开发语言·c++·算法·matlab
liron718 小时前
智能实体演化系统的统一性概念
人工智能·深度学习·神经网络
Yolanda_20228 小时前
19.神经网络-最大池化的使用
人工智能·深度学习·神经网络
呆萌很9 小时前
消融实验对比方法
人工智能·深度学习
hetao17338379 小时前
2026-10-03~04 hetao1733837 的刷题记录
c++·算法
Zootopia62610 小时前
飞行力学知识梳理1|飞行性能与稳定性
人工智能·python·算法·机器学习·无人机·学习方法·信息与通信
longlongzihan10 小时前
从LeetCode 70. 爬楼梯看动态规划:如何精准推导状态转移方程?
c++·算法·leetcode·动态规划
xx_xxxxx_11 小时前
论文阅读-SAR
人工智能·深度学习·机器学习
2601_9603563811 小时前
2027校招采购岗解析:数学基础如何迁移到需求预测、成本与供应商分析
人工智能·算法
h_a_o777oah11 小时前
【树论】树链剖分:将树形结构转化为线性结构以处理树上路径问题(洛谷 P3384 附C++代码模板)
数据结构·算法·线段树·acm·树链剖分·树论·重链剖分