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
| function [featureMatrix, bandwidths, featureNames] = calculateColourFeatures(img, selectedFeatures)
% calculateColourFeatures Calculates the color features used as input for the classifier
% calculateColourFeatures(img, selectedFeatures) calculates the color
% features for the classifier using the image supplied in img using the
% features selected by index in selectedFeatures.
% If you wish to add new color features this is the only file you need to
% edit besides adding your new feature to the selectedFeatures vector
% Define bandwidths
scale = 1;
bandwidths = [10/scale, 150/scale, 5/scale, ... %R, G, B
0.01, 0.005, 150/scale, ... % H, S, V
100/scale, 150/scale, 0.02, ... % ExG, ToRef, Ratio
0.005, 0.00005, 0.0001, 0.0002, ... % gNDVI, r, g, b
0.2/scale, 0.005, 500/scale, 0.005, 30/scale]; %ExR, n, N, NDVI, ExGN
drgb = single(img(:, :, 1:3));
dhsv = double(rgb2hsv(img(:, :, 1:3)));
%dhsv = drgb;
% Extract colour values
R = drgb(:, :, 1);
G = drgb(:, :, 2);
B = drgb(:, :, 3);
H = dhsv(:, :, 1);
S = dhsv(:, :, 2);
V = dhsv(:, :, 3);
% Convert to list form
R = R(:);
G = G(:);
B = B(:);
H = H(:);
S = S(:);
V = V(:);
% Calculate derivative features.
eg = 2*G - R - B;
er = 1.4*R-G;
rr = 90;
rg = 112;
rb = 72;
toref = sqrt((R - rr).^2 + (G - rg).^2 + (B - rb).^2);
ratio = G./(R.^0.6.*B.^0.4 + 0.01);
id = ratio > 7;
ratio(id) = 7;
% Calculate gNDVI and ensure that NAN do not occur.
gNDVI = (G - R)./(G + R + 0.00000001);
% Calculate intensity for nomalization
intensity = R + G + B + 0.00000001;
r = R ./ intensity;
g = G ./ intensity;
b = B ./ intensity;
% Arrange features in a feature matrix
featureMatrix = [R, G, B, H, S, V, eg, toref, ratio, gNDVI, r, g, b, er];
if(size(img, 3) == 4)
% Nir is present
N = double(img(:, :, 4))*0.5318;
N = N(:);
ndvi = (N - R) ./ (N + R + 0.00000001);
egn = N+G-R-B;
% Change in definition of chromacities
intensity = intensity + N;
% r = R ./ intensity;
% g = G ./ intensity;
% b = B ./ intensity;
n = N ./ intensity;
featureMatrix = [R, G, B, H, S, V, eg, toref, ratio, gNDVI, r, g, b, er, n, N, ndvi, egn];
end
% Define feature names
featureNames = cell(18, 1);
%[1, 2, 3, 16, 17, 7, 14, 11, 12, 13, 15, 18];
featureNames{1} = 'R';
featureNames{2} = 'G';
featureNames{3} = 'B';
featureNames{4} = 'H';
featureNames{5} = 'S';
featureNames{6} = 'V';
featureNames{7} = 'ExG';
featureNames{8} = 'ToRef';
featureNames{9} = 'Ratio';
featureNames{10} = 'gNDVI';
featureNames{11} = 'r';
featureNames{12} = 'g';
featureNames{13} = 'b';
featureNames{14} = 'ExR';
featureNames{15} = 'n';
featureNames{16} = 'N';
featureNames{17} = 'NDVI';
featureNames{18} = 'ExGN';
if(nargin == 2)
% Only use the selected features
featureMatrix = featureMatrix(:, selectedFeatures);
bandwidths = bandwidths(selectedFeatures);
featureNames = featureNames(selectedFeatures);
end
end |
Partager