178 |
kaklik |
1 |
namespace mimas { |
|
|
2 |
|
|
|
3 |
template< typename T > |
|
|
4 |
T _mul2( const T &x ); |
|
|
5 |
|
|
|
6 |
template <typename T > |
|
|
7 |
void _dxvec( T gx, T gy, short int w, signed char &dx, short int &dy ) |
|
|
8 |
{ |
|
|
9 |
T |
|
|
10 |
agx = gx < 0 ? -gx : gx, |
|
|
11 |
agy = gy < 0 ? -gy : gy; |
|
|
12 |
if ( agy < agx ) { |
|
|
13 |
if ( _mul2( agy ) < agx ) |
|
|
14 |
dy = 0; |
|
|
15 |
else |
|
|
16 |
dy = gy < 0 ? -w : w; |
|
|
17 |
dx = gx < 0 ? -1 : 1; |
|
|
18 |
} else { |
|
|
19 |
if ( _mul2( agx ) < agy ) |
|
|
20 |
dx = 0; |
|
|
21 |
else |
|
|
22 |
dx = gx < 0 ? -1 : 1; |
|
|
23 |
dy = gy < 0 ? -w : w; |
|
|
24 |
}; |
|
|
25 |
} |
|
|
26 |
|
|
|
27 |
template< |
|
|
28 |
typename T1, typename T2, |
|
|
29 |
typename TAPtr, typename TBPtr, typename TCPtr |
|
|
30 |
> |
|
|
31 |
image< bool > nonMaximaSuppression |
|
|
32 |
( const const_image_ref< T1, TAPtr > &gradientX, |
|
|
33 |
const const_image_ref< T1, TBPtr > &gradientY, |
|
|
34 |
const const_image_ref< T2, TCPtr > &gradientSqr, |
|
|
35 |
T2 threshold ) |
|
|
36 |
{ |
|
|
37 |
T2 thresholdSqr = threshold * threshold; |
|
|
38 |
|
|
|
39 |
image< bool > retVal; retVal.init( gradientSqr.getWidth(), |
|
|
40 |
gradientSqr.getHeight() ); |
|
|
41 |
|
|
|
42 |
short int w = (short)gradientSqr.getWidth(); |
|
|
43 |
const T2 *pgSqr = gradientSqr.rawData() + w + 1; |
|
|
44 |
const T1 |
|
|
45 |
*pgX = gradientX.rawData() + w + 1, |
|
|
46 |
*pgY = gradientY.rawData() + w + 1; |
|
|
47 |
bool *pr = retVal.rawData() + w + 1; |
|
|
48 |
|
|
|
49 |
for ( int y=1; y<gradientSqr.getHeight()-1; y++ ) { |
|
|
50 |
for ( int x=1; x<gradientSqr.getWidth()-1; x++ ) { |
|
|
51 |
|
|
|
52 |
signed char dx; |
|
|
53 |
short int dy; |
|
|
54 |
|
|
|
55 |
_dxvec< T1 >( *pgX, *pgY, w, dx, dy ); |
|
|
56 |
|
|
|
57 |
T2 g = *pgSqr; |
|
|
58 |
if ( g >= thresholdSqr && |
|
|
59 |
g >= pgSqr[ - ( dy + dx ) ] && |
|
|
60 |
g >= pgSqr[ + ( dy + dx ) ] ) |
|
|
61 |
*pr = true; |
|
|
62 |
|
|
|
63 |
pgSqr++; |
|
|
64 |
pgX++; |
|
|
65 |
pgY++; |
|
|
66 |
pr++; |
|
|
67 |
}; |
|
|
68 |
pgSqr += 2; |
|
|
69 |
pgX += 2; |
|
|
70 |
pgY += 2; |
|
|
71 |
pr += 2; |
|
|
72 |
}; |
|
|
73 |
|
|
|
74 |
return retVal; |
|
|
75 |
} |
|
|
76 |
|
|
|
77 |
}; |