%% Tutorial on leaf recognition % In this tutorial, we shall explain the basics of leaf recognition based on its shape and color statistics. % The dataset is availabe at . %% Preprocessing % Before we start, let's add necessary toolboxes to the search path of MATLAB: addpath d:/users/jang/matlab/toolbox/utility addpath d:/users/jang/matlab/toolbox/machineLearning %% % For compatibility, here we list the platform and MATLAB version that we used to run this script: fprintf('Platform: %s\n', computer); fprintf('MATLAB version: %s\n', version); scriptStartTime=tic; %% Dataset construction % First of all, we shall collect all the image data from the image directory: imDir='D:\users\jang\books\dcpr\appNote\leafId\Leaves'; opt=mmDataCollect('defaultOpt'); opt.extName='jpg'; imageData=mmDataCollect(imDir, opt); for i=1:length(imageData) [~, imageData(i).mainName]=fileparts(imageData(i).name); imageData(i).mainName=eval(imageData(i).mainName); end %% % Now we can read the class information from the file class.txt, which is collected from the % website of the dataset. classFile='class.txt'; classData=tableRead(classFile, 2); for i=1:length(classData) items=split(classData(i).filename, '-'); classData(i).range=[eval(items{1}), eval(items{2})]; end fprintf('%d classes are collected from %s.\n', length(classData), classFile); %% % We can print the common names of these plants (or classes of leaves): for i=1:length(classData) fprintf('id=%d, common name=%s\n', i, classData(i).commonName); end %% % (Of course, the above 3 statements can be omited if we want to use all % classes of leaves for further exploration.) %% % Based on the class information, we need to assign the class label for each leaf (or image): allMainName=[imageData.mainName]; for i=1:length(imageData) imageData(i).classId=0; % Default value, indicating no class being assigned yet end for i=1:length(classData) range=classData(i).range; for j=range(1):range(2); index=find(allMainName==j); if length(index)~=1, keyboard; end imageData(index).classId=i; imageData(index).class=classData(i).commonName; end end imageData([imageData.classId]==0)=[]; % Delete image with no class info fprintf('%d images are retained for further analysis.\n', length(imageData)); %% % Dispatch to sub-folder outputDir='leafSorted'; if ~exist(outputDir, 'dir'), fprintf('Creating %s...\n', outputDir); mkdir(outputDir); end for i=1:length(imageData) leafDir=[outputDir, '/', imageData(i).class]; if ~exist(leafDir, 'dir'), fprintf('Creating %s...\n', leafDir); mkdir(leafDir); end copyfile(imageData(i).path, leafDir); end