-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMiniPatch.cc
151 lines (124 loc) · 3.51 KB
/
MiniPatch.cc
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
// Copyright 2008 Isis Innovation Limited
#include "MiniPatch.h"
#include "ATANCamera.h"
using namespace CVD;
using namespace std;
// Scoring function
inline int MiniPatch::SSDAtPoint(CVD::BasicImage<CVD::byte> &im, const CVD::ImageRef &ir)
{
if(!im.in_image_with_border(ir, mnHalfPatchSize))
return mnMaxSSD + 1;
ImageRef irImgBase = ir - ImageRef(mnHalfPatchSize, mnHalfPatchSize);
int nRows = mimOrigPatch.size().y;
int nCols = mimOrigPatch.size().x;
byte *imagepointer;
byte *templatepointer;
int nDiff;
int nSumSqDiff = 0;
for(int nRow = 0; nRow < nRows; nRow++)
{
imagepointer = &im[irImgBase + ImageRef(0,nRow)];
templatepointer = &mimOrigPatch[ImageRef(0,nRow)];
for(int nCol = 0; nCol < nCols; nCol++)
{
nDiff = imagepointer[nCol] - templatepointer[nCol];
nSumSqDiff += nDiff * nDiff;
};
};
//cout << "nSumSqDiff is " << nSumSqDiff << endl;
return nSumSqDiff;
}
// Find a patch by searching at FAST corners in an input image
// If available, a row-corner LUT is used to speed up search through the
// FAST corners
bool MiniPatch::FindPatch(CVD::ImageRef &irPos,
CVD::BasicImage<CVD::byte> &im,
int nxRangeTL, int nyRangeTL,
int nxRangeBR, int nyRangeBR,
vector<ImageRef> &vCorners,
std::vector<int> *pvRowLUT)
{
ImageRef irCenter = irPos;
ImageRef irBest;
int nBestSSD = mnMaxSSD + 1;
ImageRef irBBoxTL = irPos - ImageRef(nxRangeTL, nyRangeTL);
ImageRef irBBoxBR = irPos + ImageRef(nxRangeBR, nyRangeBR);
vector<ImageRef>::iterator i;
if(!pvRowLUT)
{
for(i = vCorners.begin(); i!=vCorners.end(); i++)
if(i->y >= irBBoxTL.y) break;
}
else
{
int nTopRow = irBBoxTL.y;
if(nTopRow < 0)
nTopRow = 0;
if(nTopRow >= (int) pvRowLUT->size())
nTopRow = (int) pvRowLUT->size() - 1;
i = vCorners.begin() + (*pvRowLUT)[nTopRow];
}
for(; i!=vCorners.end(); i++)
{
if(i->x < irBBoxTL.x || i->x > irBBoxBR.x)
continue;
if(i->y > irBBoxBR.y)
break;
int nSSD = SSDAtPoint(im, *i);
if(nSSD < nBestSSD)
{
irBest = *i;
nBestSSD = nSSD;
}
}
if(nBestSSD < mnMaxSSD)
{
irPos = irBest;
return true;
}
else
return false;
}
bool MiniPatch::FindPatchEpipolar(CVD::ImageRef &irPos,
CVD::BasicImage<CVD::byte> &im,
Vector<3> EpipolarLine,
vector<ImageRef> &vCorners)
{
ImageRef irCenter = irPos;
ImageRef irBest;
int nBestSSD = mnMaxSSD + 1;
vector<ImageRef>::iterator i;
for (i = vCorners.begin(); i!=vCorners.end(); i++)
{
if((unproject(vec(*i))*EpipolarLine)*(unproject(vec(*i))*EpipolarLine) > 0.05 || sqrt((vec(irPos) - vec(*i))*(vec(irPos) - vec(*i))) > 100)
continue;
int nSSD = SSDAtPoint(im, *i);
if(nSSD < nBestSSD)
{
irBest = *i;
nBestSSD = nSSD;
}
}
if(nBestSSD < mnMaxSSD)
{
irPos = irBest;
return true;
}
else
return false;
}
// Define the patch from an input image
void MiniPatch::SampleFromImage(ImageRef irPos, BasicImage<byte> &im)
{
if(!im.in_image_with_border(irPos, mnHalfPatchSize)){
cout << irPos << "was not in image of size " << im.size() << endl;
assert(im.in_image_with_border(irPos, mnHalfPatchSize));
}
CVD::ImageRef irPatchSize( 2 * mnHalfPatchSize + 1 , 2 * mnHalfPatchSize + 1);
mimOrigPatch.resize(irPatchSize);
copy(im, mimOrigPatch, mimOrigPatch.size(), irPos - mimOrigPatch.size() / 2);
}
// Static members
int MiniPatch::mnHalfPatchSize = 4;
int MiniPatch::mnRange = 10;
int MiniPatch::mnMaxSSD = 9999;