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对苹果的分割检测

相关推荐
拳里剑气7 小时前
C++算法:BFS解决FloodFill算法
c++·算法·bfs·宽度优先
大鹏的NLP博客7 小时前
机器学习中的统计波动控制与表征几何优化原则:从函数稳定性到隐空间结构学习
人工智能·深度学习·机器学习
wanderist.8 小时前
Lambda表达式在算法竞赛中的应用
java·开发语言·算法
稚南城才子,乌衣巷风流9 小时前
支配树(Dominator Tree)详解:概念、算法与应用
算法
稚南城才子,乌衣巷风流9 小时前
动态开点:原理、实现与应用场景
数据结构·算法
yyds_yyd_100869 小时前
1464. 数组中两元素的最大乘积(2026.07.27)
数据结构·c++·算法·leetcode
KaMeidebaby10 小时前
卡梅德生物技术快报 | 核酸适配体文库测序:核酸适配体文库测序的技术原理、实验流程与数据解析
前端·网络·数据库·人工智能·算法
geovindu11 小时前
CSharp: Breadth First Search Algorithm and Depth First Search Algorithm
开发语言·后端·算法·c#·.net·搜索算法
ekkoalex12 小时前
训练框架以及算法实现的框架
算法