-
Notifications
You must be signed in to change notification settings - Fork 0
/
doFirstStep.m
62 lines (47 loc) · 2.31 KB
/
doFirstStep.m
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
function [trainedModel,trainingModel,normalizeValues,objectImage,bset,oset,bsetaux,deleted] = doFirstStep(I,rectangleContenidor,numberBlocksY,numberBlocksX,trainingModel,secondAproximationType)
[nr,nc]= size(I(:,:,1));
windowSizeY = floor(nr/numberBlocksY);
windowSizeX = floor(nc/numberBlocksX);
stuffY = windowSizeY - mod(nr,windowSizeY);
stuffX = windowSizeX - mod(nc,windowSizeX);
I = padarray(I, [stuffY stuffX], 'replicate','post');
[nr,nc]= size(I(:,:,1));
numberBlocks = nr/windowSizeY * nc/windowSizeX;
trainingDataset = zeros([numberBlocks 17]);
charBackground = 'B';
charObject = 'O';
prediction = charBackground;
%HISTOGRAMA
bset = zeros([1 17]);
cont = 0;
for i=1:windowSizeY:nr
for j=1:windowSizeX:nc
cont = cont + 1;
B = I(i:i+windowSizeY-1,j:j+windowSizeX-1,:);
featureVector=computeFeatures(B);
trainingDataset(cont,:)=featureVector;
if blockInsideRectangle([j i windowSizeX windowSizeY],rectangleContenidor,0.8)
prediction = [prediction; charObject];
else
bset = [bset;featureVector];
prediction = [prediction; charBackground];
end
end
end
prediction = prediction(2:(numberBlocks+1));
[normalizeValues,normalizedTrainingDataset,deleted] = normalizeColumns(trainingDataset);
%Train model to differentiate between the two type of blocks
trainedModel = trainModel(normalizedTrainingDataset,prediction,trainingModel);
%Predict model for the blocks inside the rectangle
[oset,bsetaux,predictedBlocks] = predictModel(I,trainedModel,rectangleContenidor,windowSizeX,windowSizeY,trainingModel,normalizeValues,deleted);
firstCleaning = predictedBlocks == 1;
firstCleaning = deleteInteriorHoles(firstCleaning);
firstCleaning = deleteLittleRegions(firstCleaning,windowSizeY,windowSizeX);
[expandedEdges,interior] = expandEdges(firstCleaning,windowSizeY,windowSizeX);
predictedPoints = predictPoints(I,expandedEdges,trainedModel,trainingModel,normalizeValues,secondAproximationType,deleted);
aux = or(predictedPoints,interior);
aux = deleteInteriorHoles(aux);
figure;imshow(aux,[]);
[finalImage, objectImage] = doFinalImage(I,aux);
figure;imshow(finalImage,[]);
end