用matlab实现8种树叶分类大概框架——数字图像处理作业

终于弄完了,之前搜遍各个网站,想抄都没的抄,不是要钱就是要订阅,只能东拼西凑了。这个是大概框架,目标是实现8种不同叶子的分类,每种叶子各有8张,利用svmClassifier函数实现分类,然后对新读取的图片进行类似的特征提取,然后再预测。

至于提取什么特征,我是找到了一篇半吊子不全的论文,照着提取的,比如说偏心率,圆形度,球状度,实度什么的,还有其它的搜一下就知道了,主要就是预处理要弄好,二值化和灰度化非常重要,有问题可以私信博主,看到就会回复,如有更好建议也可以一起探讨!

leafTypes = {'Quercussuber', 'Salixatrocinerea', 'Populusnigra', 'Alnussp', 'Quercusrobur', 'Crataegusmonogyna', 'Ilexaquifolium', 'Neriumoleander'};
numImagesPerType = 8;
% 初始化数据集
dataset = struct();%原始图像
%data2bw=struct();%二值图像
%datargbgray = struct();%灰度图像
%datargbhsi = struct();%HSI图像

% 读取图片并添加到数据集中
for i = 1:length(leafTypes)
for j = 1:numImagesPerType
imagePath = ['C:\Users\灰太狼\OneDrive\桌面\图片训练集\', leafTypes{i}, '\iPAD2_C0_EX0', num2str(j), '.jpg'];
imageData = imread(imagePath);
dataset.(leafTypes{i}){j} = imageData;
%datargbgray.(leafTypes{i}){j}=imageData;
%datargbhsi.(leafTypes{i}){j}=imageData;
%data2bw.(leafTypes{i}){j}=imageData;
end
end

%特征提取
featuredataset=struct();
pi=3.14159;

for leafType = fieldnames(dataset)'
    featureMatrix=[];
for imageIndex = 1:numImagesPerType
        imagegrayData = dataset.(leafType{1}){imageIndex};
       
        %预处理
        灰度化,二值处理

        %提取特征
        area=sum(greenpicture(:)>0);
        area=area/100000;
        提取其它特征
       
        % 将特征存储在特征数据集
         featureMatrix =[featureMatrix; area,%存放特征%];
    end
            featuredataset.(leafType{1}) = featureMatrix;
end

% 提取所有特征矩阵
allFeatureMatrices = [];
for leafType = fieldnames(featuredataset)'
    allFeatureMatrices = [allFeatureMatrices; featuredataset.(leafType{1})];
end

labels = [];
for i = 1:length(leafTypes)
    labels = [labels; repmat(i, numImagesPerType, 1)];
end

% 使用特征和标签训练分类器
svmClassifier = fitcecoc(allFeatureMatrices, labels);

% 显示分类器
disp(svmClassifier);

% 对新输入图片进行特征提取
newImage = imread('C:\Users\灰太狼\OneDrive\桌面\图片训练集\two\iPAD2_C0_EX06.JPG');

% 对新输入图片进行特征提取
图片预处理,提取特征,同上。
newarea=sum(newgreenpicture(:)>0);
newArea=newArea/100000;
newImageFeatures = [newArea,newRectangularity,newCircularity];

predictedLabel = predict(svmClassifier, newImageFeatures);
predictedLeafType = leafTypes{predictedLabel};
disp(['Predicted leaf type: ', predictedLeafType]);

我树叶存放的文件夹是这样的

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值