1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
| clear all;close all;clc;
X=[];Y=[];
addpath(pwd) %adds current directory to PATH to fetch funtion
%% Folders
d = dir();
isub = [d(:).isdir];
nameFolds = {d(isub).name}'; %to get only folders
fprintf('Number of folders: %d \n',size(nameFolds,1)-2);
for n_folder=1:size(nameFolds,1)-2 %for each folder
cd(char(nameFolds(n_folder+2))) %enters folder
fprintf('Analyzing folder: %s \n',char(nameFolds(n_folder+2)));
%% auto selection of images
list=dir('*.bmp');
for nimag=1:size(list,1) %for each image
im_name=list(nimag).name;
a= imread(im_name);
%% image tratement
a=im2bw(a, 0.5);
a = ~a;
[L n]=bwlabel(a); %number of objects in image
rp=regionprops(a,'Area','EulerNumber','Centroid'); %characteristics of objects
%% variables
n_char=1;
n_pos=0;
n_dots=0;
n_holes=0;
%% n_dots & n_pos processing
if n>1 % only processes if dots exist
for nn=2:n
if rp(nn).Area>rp(nn-1).Area %comparasion between areas
n_char=nn; %index of the main character
end
end
%number of dots
for nn=1:n
if 2<rp(nn).Area && rp(nn).Area<20 %individual dots
n_dots=n_dots+1;
elseif 20<rp(nn).Area && rp(nn).Area<40 %dots that are conected by a pixel
n_dots=n_dots+2;
end
end
% position of dots
if n_dots>0 %only if dots existe
for nd=1:n ;
if rp(n_char).Centroid(2)<rp(n).Centroid(2)
n_pos=-1; %on the bottom
else
n_pos=1; %on top
end
end
end
end
%% number of holes
n_obj_big=n;
for ii=1:n
if rp(ii).Area<3
n_obj_big=n_obj_big-1; %so individual pixels aren't counted as objects
end
end
if rp(n_char).EulerNumber<1
n_holes=n_obj_big-rp(n_char).EulerNumber-n_dots;
end
%% MOMENT OF HU
% First Moment
n20=cent_moment(2,0,a);
n02=cent_moment(0,2,a);
M1=n20+n02;
% Second Moment
n20=cent_moment(2,0,a);
n02=cent_moment(0,2,a);
n11=cent_moment(1,1,a);
M2=(n20-n02)^2+4*n11^2;
% Third Moment
n30=cent_moment(3,0,a);
n12=cent_moment(1,2,a);
n21=cent_moment(2,1,a);
n03=cent_moment(0,3,a);
M3=(n30-3*n12)^2+(3*n21-n03)^2;
% Fourth Moment
n30=cent_moment(3,0,a);
n12=cent_moment(1,2,a);
n21=cent_moment(2,1,a);
n03=cent_moment(0,3,a);
M4=(n30+n12)^2+(n21+n03)^2;
% Fifth Moment
n30=cent_moment(3,0,a);
n12=cent_moment(1,2,a);
n21=cent_moment(2,1,a);
n03=cent_moment(0,3,a);
M5=(n30-3*n21)*(n30+n12)*[(n30+n12)^2-3*(n21+n03)^2]+(3*n21-n03)*(n21+n03)*[3*(n30+n12)^2-(n21+n03)^2];
% Sixth Moment
n20=cent_moment(2,0,a);
n02=cent_moment(0,2,a);
n30=cent_moment(3,0,a);
n12=cent_moment(1,2,a);
n21=cent_moment(2,1,a);
n03=cent_moment(0,3,a);
n11=cent_moment(1,1,a);
M6=(n20-n02)*[(n30+n12)^2-(n21+n03)^2]+4*n11*(n30+n12)*(n21+n03);
% Seventh Moment
n30=cent_moment(3,0,a);
n12=cent_moment(1,2,a);
n21=cent_moment(2,1,a);
n03=cent_moment(0,3,a);
M7=(3*n21-n03)*(n30+n12)*[(n30+n12)^2-3*(n21+n03)^2]-(n30+3*n12)*(n21+n03)*[3*(n30+n12)^2-(n21+n03)^2];
% The vector M is a column vector containing M1,M2,....M7
M=[M1 M2 M3 M4 M5 M6 M7]';
%and this is the Feature vector
% Results
X=[X; M' n_holes n_dots n_pos];
Y=[Y; n_folder];
end
cd .. % return to superior folder
end
save data.mat X Y %to save data in .mat fi |
Partager