1 construct the dataset
%% load imds
svmimds= imageDatastore('F:\machine learning\moistureDamage\SVMfeature\pic', 'IncludeSubfolders', true, 'LabelSource', 'foldernames');
%% random the dataset
imdsrand = shuffle(svmimds);
countEachLabel(imdsrand)
2 About Harr
Please see the detail in http://www.willberger.org/cascade-haar-explained/ or wiki
function: haart2 in matlab
3 Classify method :SVM
hint:
for SVM and image classifier: see
https://medium.com/@dataturks/understanding-svms-for-image-classification-cf4f01232700
or you can look the exmaple in matlab:https://www.mathworks.com/help/vision/examples/digit-classification-using-hog-features.html
SVM: (http://web.mit.edu/6.034/wwwbob/svm-notes-long-08.pdf )
use level 1 of haar feature
Train the model
% use svm and LLorig to train the SVM model
numImages = numel(imdsrand.Files);
p=0.2
numImages=3000;
imgsz=[128,128];
for i = 1:numImages%(numImages*(1-p))
img = readimage(imdsrand, i);
% img=imresize(img,imgsvmsize);
img = rgb2gray(img);
img = imresize(im2double(img),[128 128]);
[ LLorig ,LHorig,HLorig,HHorig] = haart2(img,1);
trainingFeatures(i,:)=reshape(LLorig,[64*64,1]);
end
% Get labels for each image.
trainingLabels = imdsrand.Labels(1:int16(numImages*(1-p)));
% AllLabel=imdsrand.Labels
% fitcecoc uses SVM learners and a 'One-vs-One' encoding scheme.
classifier = fitcecoc(trainingFeatures(1:numImages*(1-p),:), trainingLabels,'Learners','svm');
test the accuracy
predictedLabels = predict(classifier, trainingFeatures(int16(numImages*0.8+1):numImages,:))
% imdsrand.Labels(801)
% Tabulate the results using a confusion matrix.
confMat = confusionmat( imdsrand.Labels(int16(numImages*0.8+1):numImages), predictedLabels);
% helperDisplayConfusionMatrix(confMat)
confMat = bsxfun(@rdivide,confMat,sum(confMat,2));
helperDisplayConfusionMatrix(confMat)
Test Results:
the result is just so-so, more data should be added to traint he model.
4 Test on test dataset (use sliding windows)
% Read the original data
num=30;
% img1=rgb2gray(imread(MoisImageset.imageFilename{num}));
img1=(imread(MoisImageset.imageFilename{num}));
% figure
% imagesc(img1(40:167,40:155,:))
% % figure
% imagesc(img1)
% figure
% img = rgb2gray(img1);
% imshow(imbinarizeimg1(40:167,40:155))
% img = imbinarize(img1);
A=zeros(1090,300)';
inum=0;
% w1=20;h1=100;
anchorsvm=[64 64 ;127 115;20 34;26 46;59 43;127 50];
sz1=size(anchorsvm,1);
for isz1=1:sz1
w1= anchorsvm(isz1,1);h1=anchorsvm(isz1,2);
for h2=40:5:100%10:150 % height
for wid=30:5:1020%20:1020%:w1:1020 %width
% wid=580
%
testarea=img1(h2:h2+h1,wid:min(wid+w1,1020),:);
% Apply pre-processing steps
% testarea = imbinarize(testarea);
% figure
% imshow(testarea)
bbox=[wid,h2,min(w1,max(1020-wid,0)),h1];
imtestareag=imresize(testarea,[128 128]);
img = rgb2gray(imtestareag);
img = imresize(im2double(img),[128 128]);
[ LLorig,LHorig,HLorig,HHorig] = haart2(img,1);
testFeatures=reshape(LLorig,[64*64,1]);
predictedLabels = predict(classifier, testFeatures');
% testFeat = extractHOGFeatures(imtestareag, 'CellSize', cellSize);
% predictedLabel = predict(classifier, testFeat);
% imtestareag = colorspace('yuv<-rgb',imtestareag);
% [count,x] = imhist(imtestareag(:,:,1),16);
% if sum(count(6:8))/sum(count)<=0.42
if predictedLabels=='Moisture'
A=A+ChangeBox2Arr(bbox,1090,300);
inum=inum+1;
end
end
end
end
I=img1;
% ground truth
Mbbox=MoisImageset.Moisture{num};Jbbox=MoisImageset.Joint{num};
if ~isempty(Mbbox)
I = insertShape(I, 'Rectangle', Mbbox,'LineWidth',3,'Color','green');
end
if ~isempty(Jbbox)
I = insertShape(I, 'Rectangle', Jbbox,'LineWidth',3,'Color','red');
end
figure
imshow(I)
hold on;
maxi=max(max(A));
% % % A=(A-ones(300,1090)*maxi*0.6);
B=A;
B(A<0.8*maxi)=0;
alpha = (~(B==0))*0.3;%% adjust the color
OverlayImage =imshow(B);
caxis auto
colormap( OverlayImage.Parent, jet );
colorbar( OverlayImage.Parent );
% Set the AlphaData to be the transparency matrix created earlier
set( OverlayImage, 'AlphaData', alpha );
filter:80% of result
The method has some intesting results, but it doesn't meet my requirement. Large erros.